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 static int find_natgw(struct ctdb_context
*ctdb
,
1127 struct ctdb_node_map
*nodemap
, uint32_t flags
,
1128 uint32_t *pnn
, const char **ip
)
1131 uint32_t capabilities
;
1134 for (i
=0;i
<nodemap
->num
;i
++) {
1135 if (!(nodemap
->nodes
[i
].flags
& flags
)) {
1136 ret
= ctdb_ctrl_getcapabilities(ctdb
, TIMELIMIT(),
1137 nodemap
->nodes
[i
].pnn
,
1140 DEBUG(DEBUG_ERR
, ("Unable to get capabilities from node %u\n",
1141 nodemap
->nodes
[i
].pnn
));
1144 if (!(capabilities
&CTDB_CAP_NATGW
)) {
1147 *pnn
= nodemap
->nodes
[i
].pnn
;
1148 *ip
= ctdb_addr_to_str(&nodemap
->nodes
[i
].addr
);
1153 return 2; /* matches ENOENT */
1157 display the list of nodes belonging to this natgw configuration
1159 static int control_natgwlist(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1161 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1163 const char *natgw_list
;
1166 struct natgw_node
*natgw_nodes
= NULL
;
1167 struct natgw_node
*natgw_node
;
1168 struct ctdb_node_map
*nodemap
=NULL
;
1169 uint32_t mypnn
, pnn
;
1172 /* When we have some nodes that could be the NATGW, make a
1173 * series of attempts to find the first node that doesn't have
1174 * certain status flags set.
1176 uint32_t exclude_flags
[] = {
1177 /* Look for a nice healthy node */
1178 NODE_FLAGS_DISCONNECTED
|NODE_FLAGS_STOPPED
|NODE_FLAGS_DELETED
|NODE_FLAGS_BANNED
|NODE_FLAGS_UNHEALTHY
,
1179 /* If not found, an UNHEALTHY/BANNED node will do */
1180 NODE_FLAGS_DISCONNECTED
|NODE_FLAGS_STOPPED
|NODE_FLAGS_DELETED
,
1181 /* If not found, a STOPPED node will do */
1182 NODE_FLAGS_DISCONNECTED
|NODE_FLAGS_DELETED
,
1186 /* read the natgw nodes file into a linked list */
1187 natgw_list
= getenv("CTDB_NATGW_NODES");
1188 if (natgw_list
== NULL
) {
1189 natgw_list
= talloc_asprintf(tmp_ctx
, "%s/natgw_nodes",
1190 getenv("CTDB_BASE"));
1191 if (natgw_list
== NULL
) {
1192 DEBUG(DEBUG_ALERT
,(__location__
" Out of memory\n"));
1196 lines
= file_lines_load(natgw_list
, &nlines
, ctdb
);
1197 if (lines
== NULL
) {
1198 ctdb_set_error(ctdb
, "Failed to load natgw node list '%s'\n", natgw_list
);
1199 talloc_free(tmp_ctx
);
1202 for (i
=0;i
<nlines
;i
++) {
1206 /* strip leading spaces */
1207 while((*node
== ' ') || (*node
== '\t')) {
1213 if (strcmp(node
, "") == 0) {
1216 natgw_node
= talloc(ctdb
, struct natgw_node
);
1217 natgw_node
->addr
= talloc_strdup(natgw_node
, node
);
1218 CTDB_NO_MEMORY(ctdb
, natgw_node
->addr
);
1219 natgw_node
->next
= natgw_nodes
;
1220 natgw_nodes
= natgw_node
;
1223 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, tmp_ctx
, &nodemap
);
1225 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from local node.\n"));
1226 talloc_free(tmp_ctx
);
1230 /* Trim the nodemap so it only includes connected nodes in the
1231 * current natgw group.
1234 while(i
<nodemap
->num
) {
1235 for(natgw_node
=natgw_nodes
;natgw_node
;natgw_node
=natgw_node
->next
) {
1236 if (!strcmp(natgw_node
->addr
, ctdb_addr_to_str(&nodemap
->nodes
[i
].addr
))) {
1241 /* this node was not in the natgw so we just remove it from
1244 if ((natgw_node
== NULL
)
1245 || (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DISCONNECTED
) ) {
1248 for (j
=i
+1; j
<nodemap
->num
; j
++) {
1249 nodemap
->nodes
[j
-1] = nodemap
->nodes
[j
];
1258 ret
= 2; /* matches ENOENT */
1261 for (i
= 0; exclude_flags
[i
] != 0; i
++) {
1262 ret
= find_natgw(ctdb
, nodemap
,
1273 if (options
.machinereadable
) {
1274 printf(":Node:IP:\n");
1275 printf(":%d:%s:\n", pnn
, ip
);
1277 printf("%d %s\n", pnn
, ip
);
1280 /* print the pruned list of nodes belonging to this natgw list */
1281 mypnn
= getpnn(ctdb
);
1282 if (options
.machinereadable
) {
1283 control_status_header_machine();
1285 printf("Number of nodes:%d\n", nodemap
->num
);
1287 for(i
=0;i
<nodemap
->num
;i
++){
1288 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DELETED
) {
1291 if (options
.machinereadable
) {
1292 control_status_1_machine(ctdb
, mypnn
, &(nodemap
->nodes
[i
]));
1294 control_status_1_human(ctdb
, mypnn
, &(nodemap
->nodes
[i
]));
1299 talloc_free(tmp_ctx
);
1304 display the status of the scripts for monitoring (or other events)
1306 static int control_one_scriptstatus(struct ctdb_context
*ctdb
,
1307 enum ctdb_eventscript_call type
)
1309 struct ctdb_scripts_wire
*script_status
;
1312 ret
= ctdb_ctrl_getscriptstatus(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, type
, &script_status
);
1314 DEBUG(DEBUG_ERR
, ("Unable to get script status from node %u\n", options
.pnn
));
1318 if (script_status
== NULL
) {
1319 if (!options
.machinereadable
) {
1320 printf("%s cycle never run\n",
1321 ctdb_eventscript_call_names
[type
]);
1326 if (!options
.machinereadable
) {
1327 printf("%d scripts were executed last %s cycle\n",
1328 script_status
->num_scripts
,
1329 ctdb_eventscript_call_names
[type
]);
1331 for (i
=0; i
<script_status
->num_scripts
; i
++) {
1332 const char *status
= NULL
;
1334 switch (script_status
->scripts
[i
].status
) {
1336 status
= "TIMEDOUT";
1339 status
= "DISABLED";
1345 if (script_status
->scripts
[i
].status
> 0)
1349 if (options
.machinereadable
) {
1350 printf(":%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
1351 ctdb_eventscript_call_names
[type
],
1352 script_status
->scripts
[i
].name
,
1353 script_status
->scripts
[i
].status
,
1355 (long)script_status
->scripts
[i
].start
.tv_sec
,
1356 (long)script_status
->scripts
[i
].start
.tv_usec
,
1357 (long)script_status
->scripts
[i
].finished
.tv_sec
,
1358 (long)script_status
->scripts
[i
].finished
.tv_usec
,
1359 script_status
->scripts
[i
].output
);
1363 printf("%-20s Status:%s ",
1364 script_status
->scripts
[i
].name
, status
);
1366 /* Some other error, eg from stat. */
1367 printf("%-20s Status:CANNOT RUN (%s)",
1368 script_status
->scripts
[i
].name
,
1369 strerror(-script_status
->scripts
[i
].status
));
1371 if (script_status
->scripts
[i
].status
>= 0) {
1372 printf("Duration:%.3lf ",
1373 timeval_delta(&script_status
->scripts
[i
].finished
,
1374 &script_status
->scripts
[i
].start
));
1376 if (script_status
->scripts
[i
].status
!= -ENOEXEC
) {
1378 ctime(&script_status
->scripts
[i
].start
.tv_sec
));
1379 if (script_status
->scripts
[i
].status
!= 0) {
1380 printf(" OUTPUT:%s\n",
1381 script_status
->scripts
[i
].output
);
1391 static int control_scriptstatus(struct ctdb_context
*ctdb
,
1392 int argc
, const char **argv
)
1395 enum ctdb_eventscript_call type
, min
, max
;
1399 DEBUG(DEBUG_ERR
, ("Unknown arguments to scriptstatus\n"));
1404 arg
= ctdb_eventscript_call_names
[CTDB_EVENT_MONITOR
];
1408 for (type
= 0; type
< CTDB_EVENT_MAX
; type
++) {
1409 if (strcmp(arg
, ctdb_eventscript_call_names
[type
]) == 0) {
1415 if (type
== CTDB_EVENT_MAX
) {
1416 if (strcmp(arg
, "all") == 0) {
1418 max
= CTDB_EVENT_MAX
;
1420 DEBUG(DEBUG_ERR
, ("Unknown event type %s\n", argv
[0]));
1425 if (options
.machinereadable
) {
1426 printf(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1429 for (type
= min
; type
< max
; type
++) {
1430 ret
= control_one_scriptstatus(ctdb
, type
);
1440 enable an eventscript
1442 static int control_enablescript(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1450 ret
= ctdb_ctrl_enablescript(ctdb
, TIMELIMIT(), options
.pnn
, argv
[0]);
1452 DEBUG(DEBUG_ERR
, ("Unable to enable script %s on node %u\n", argv
[0], options
.pnn
));
1460 disable an eventscript
1462 static int control_disablescript(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1470 ret
= ctdb_ctrl_disablescript(ctdb
, TIMELIMIT(), options
.pnn
, argv
[0]);
1472 DEBUG(DEBUG_ERR
, ("Unable to disable script %s on node %u\n", argv
[0], options
.pnn
));
1480 display the pnn of the recovery master
1482 static int control_recmaster(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1487 ret
= ctdb_ctrl_getrecmaster(ctdb
, ctdb
, TIMELIMIT(), options
.pnn
, &recmaster
);
1489 DEBUG(DEBUG_ERR
, ("Unable to get recmaster from node %u\n", options
.pnn
));
1492 printf("%d\n",recmaster
);
1498 add a tickle to a public address
1500 static int control_add_tickle(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1502 struct ctdb_tcp_connection t
;
1506 assert_single_node_only();
1512 if (parse_ip_port(argv
[0], &t
.src_addr
) == 0) {
1513 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
1516 if (parse_ip_port(argv
[1], &t
.dst_addr
) == 0) {
1517 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[1]));
1521 data
.dptr
= (uint8_t *)&t
;
1522 data
.dsize
= sizeof(t
);
1524 /* tell all nodes about this tcp connection */
1525 ret
= ctdb_control(ctdb
, options
.pnn
, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE
,
1526 0, data
, ctdb
, NULL
, NULL
, NULL
, NULL
);
1528 DEBUG(DEBUG_ERR
,("Failed to add tickle\n"));
1537 delete a tickle from a node
1539 static int control_del_tickle(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1541 struct ctdb_tcp_connection t
;
1545 assert_single_node_only();
1551 if (parse_ip_port(argv
[0], &t
.src_addr
) == 0) {
1552 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
1555 if (parse_ip_port(argv
[1], &t
.dst_addr
) == 0) {
1556 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[1]));
1560 data
.dptr
= (uint8_t *)&t
;
1561 data
.dsize
= sizeof(t
);
1563 /* tell all nodes about this tcp connection */
1564 ret
= ctdb_control(ctdb
, options
.pnn
, 0, CTDB_CONTROL_TCP_REMOVE
,
1565 0, data
, ctdb
, NULL
, NULL
, NULL
, NULL
);
1567 DEBUG(DEBUG_ERR
,("Failed to remove tickle\n"));
1576 get a list of all tickles for this pnn
1578 static int control_get_tickles(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1580 struct ctdb_control_tcp_tickle_list
*list
;
1581 ctdb_sock_addr addr
;
1585 assert_single_node_only();
1592 port
= atoi(argv
[1]);
1595 if (parse_ip(argv
[0], NULL
, 0, &addr
) == 0) {
1596 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
1600 ret
= ctdb_ctrl_get_tcp_tickles(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &addr
, &list
);
1602 DEBUG(DEBUG_ERR
, ("Unable to list tickles\n"));
1606 if (options
.machinereadable
){
1607 printf(":source ip:port:destination ip:port:\n");
1608 for (i
=0;i
<list
->tickles
.num
;i
++) {
1609 if (port
&& port
!= ntohs(list
->tickles
.connections
[i
].dst_addr
.ip
.sin_port
)) {
1612 printf(":%s:%u", ctdb_addr_to_str(&list
->tickles
.connections
[i
].src_addr
), ntohs(list
->tickles
.connections
[i
].src_addr
.ip
.sin_port
));
1613 printf(":%s:%u:\n", ctdb_addr_to_str(&list
->tickles
.connections
[i
].dst_addr
), ntohs(list
->tickles
.connections
[i
].dst_addr
.ip
.sin_port
));
1616 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list
->addr
));
1617 printf("Num tickles:%u\n", list
->tickles
.num
);
1618 for (i
=0;i
<list
->tickles
.num
;i
++) {
1619 if (port
&& port
!= ntohs(list
->tickles
.connections
[i
].dst_addr
.ip
.sin_port
)) {
1622 printf("SRC: %s:%u ", ctdb_addr_to_str(&list
->tickles
.connections
[i
].src_addr
), ntohs(list
->tickles
.connections
[i
].src_addr
.ip
.sin_port
));
1623 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
));
1633 static int move_ip(struct ctdb_context
*ctdb
, ctdb_sock_addr
*addr
, uint32_t pnn
)
1635 struct ctdb_all_public_ips
*ips
;
1636 struct ctdb_public_ip ip
;
1639 uint32_t disable_time
;
1641 struct ctdb_node_map
*nodemap
=NULL
;
1642 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1645 data
.dptr
= (uint8_t*)&disable_time
;
1646 data
.dsize
= sizeof(disable_time
);
1647 ret
= ctdb_client_send_message(ctdb
, CTDB_BROADCAST_CONNECTED
, CTDB_SRVID_DISABLE_IP_CHECK
, data
);
1649 DEBUG(DEBUG_ERR
,("Failed to send message to disable ipcheck\n"));
1655 /* read the public ip list from the node */
1656 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), pnn
, ctdb
, &ips
);
1658 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from node %u\n", pnn
));
1659 talloc_free(tmp_ctx
);
1663 for (i
=0;i
<ips
->num
;i
++) {
1664 if (ctdb_same_ip(addr
, &ips
->ips
[i
].addr
)) {
1669 DEBUG(DEBUG_ERR
, ("Node %u can not host ip address '%s'\n",
1670 pnn
, ctdb_addr_to_str(addr
)));
1671 talloc_free(tmp_ctx
);
1678 data
.dptr
= (uint8_t *)&ip
;
1679 data
.dsize
= sizeof(ip
);
1681 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &nodemap
);
1683 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
1684 talloc_free(tmp_ctx
);
1688 nodes
= list_of_nodes(ctdb
, nodemap
, tmp_ctx
, NODE_FLAGS_INACTIVE
, pnn
);
1689 ret
= ctdb_client_async_control(ctdb
, CTDB_CONTROL_RELEASE_IP
,
1696 DEBUG(DEBUG_ERR
,("Failed to release IP on nodes\n"));
1697 talloc_free(tmp_ctx
);
1701 ret
= ctdb_ctrl_takeover_ip(ctdb
, LONGTIMELIMIT(), pnn
, &ip
);
1703 DEBUG(DEBUG_ERR
,("Failed to take over IP on node %d\n", pnn
));
1704 talloc_free(tmp_ctx
);
1708 /* update the recovery daemon so it now knows to expect the new
1709 node assignment for this ip.
1711 ret
= ctdb_client_send_message(ctdb
, CTDB_BROADCAST_CONNECTED
, CTDB_SRVID_RECD_UPDATE_IP
, data
);
1713 DEBUG(DEBUG_ERR
,("Failed to send message to update the ip on the recovery master.\n"));
1717 talloc_free(tmp_ctx
);
1723 * scans all other nodes and returns a pnn for another node that can host this
1727 find_other_host_for_public_ip(struct ctdb_context
*ctdb
, ctdb_sock_addr
*addr
)
1729 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1730 struct ctdb_all_public_ips
*ips
;
1731 struct ctdb_node_map
*nodemap
=NULL
;
1734 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, tmp_ctx
, &nodemap
);
1736 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
1737 talloc_free(tmp_ctx
);
1741 for(i
=0;i
<nodemap
->num
;i
++){
1742 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_INACTIVE
) {
1745 if (nodemap
->nodes
[i
].pnn
== options
.pnn
) {
1749 /* read the public ip list from this node */
1750 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), nodemap
->nodes
[i
].pnn
, tmp_ctx
, &ips
);
1752 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from node %u\n", nodemap
->nodes
[i
].pnn
));
1756 for (j
=0;j
<ips
->num
;j
++) {
1757 if (ctdb_same_ip(addr
, &ips
->ips
[j
].addr
)) {
1758 talloc_free(tmp_ctx
);
1759 return nodemap
->nodes
[i
].pnn
;
1765 talloc_free(tmp_ctx
);
1769 /* If pnn is -1 then try to find a node to move IP to... */
1770 static bool try_moveip(struct ctdb_context
*ctdb
, ctdb_sock_addr
*addr
, uint32_t pnn
)
1772 bool pnn_specified
= (pnn
== -1 ? false : true);
1775 while (retries
< 5) {
1776 if (!pnn_specified
) {
1777 pnn
= find_other_host_for_public_ip(ctdb
, addr
);
1782 ("Trying to move public IP to node %u\n", pnn
));
1785 if (move_ip(ctdb
, addr
, pnn
) == 0) {
1798 move/failover an ip address to a specific node
1800 static int control_moveip(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1803 ctdb_sock_addr addr
;
1805 assert_single_node_only();
1812 if (parse_ip(argv
[0], NULL
, 0, &addr
) == 0) {
1813 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
1818 if (sscanf(argv
[1], "%u", &pnn
) != 1) {
1819 DEBUG(DEBUG_ERR
, ("Badly formed pnn\n"));
1823 if (!try_moveip(ctdb
, &addr
, pnn
)) {
1824 DEBUG(DEBUG_ERR
,("Failed to move IP to node %d.\n", pnn
));
1831 static int rebalance_node(struct ctdb_context
*ctdb
, uint32_t pnn
)
1835 data
.dptr
= (uint8_t *)&pnn
;
1836 data
.dsize
= sizeof(uint32_t);
1837 if (ctdb_client_send_message(ctdb
, CTDB_BROADCAST_CONNECTED
, CTDB_SRVID_REBALANCE_NODE
, data
) != 0) {
1839 ("Failed to send message to force node %u to be a rebalancing target\n",
1849 rebalance a node by setting it to allow failback and triggering a
1852 static int control_rebalancenode(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1854 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1859 assert_single_node_only();
1865 /* Determine the nodes where IPs need to be reloaded */
1866 if (!parse_nodestring(ctdb
, tmp_ctx
, argc
== 1 ? argv
[0] : NULL
,
1867 options
.pnn
, true, &nodes
, &pnn_mode
)) {
1872 for (i
= 0; i
< talloc_array_length(nodes
); i
++) {
1873 if (!rebalance_node(ctdb
, nodes
[i
])) {
1879 talloc_free(tmp_ctx
);
1883 static int rebalance_ip(struct ctdb_context
*ctdb
, ctdb_sock_addr
*addr
)
1885 struct ctdb_public_ip ip
;
1888 uint32_t disable_time
;
1890 struct ctdb_node_map
*nodemap
=NULL
;
1891 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1894 data
.dptr
= (uint8_t*)&disable_time
;
1895 data
.dsize
= sizeof(disable_time
);
1896 ret
= ctdb_client_send_message(ctdb
, CTDB_BROADCAST_CONNECTED
, CTDB_SRVID_DISABLE_IP_CHECK
, data
);
1898 DEBUG(DEBUG_ERR
,("Failed to send message to disable ipcheck\n"));
1905 data
.dptr
= (uint8_t *)&ip
;
1906 data
.dsize
= sizeof(ip
);
1908 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &nodemap
);
1910 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
1911 talloc_free(tmp_ctx
);
1915 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
1916 ret
= ctdb_client_async_control(ctdb
, CTDB_CONTROL_RELEASE_IP
,
1923 DEBUG(DEBUG_ERR
,("Failed to release IP on nodes\n"));
1924 talloc_free(tmp_ctx
);
1928 talloc_free(tmp_ctx
);
1933 release an ip form all nodes and have it re-assigned by recd
1935 static int control_rebalanceip(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1937 ctdb_sock_addr addr
;
1939 assert_single_node_only();
1946 if (parse_ip(argv
[0], NULL
, 0, &addr
) == 0) {
1947 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
1951 if (rebalance_ip(ctdb
, &addr
) != 0) {
1952 DEBUG(DEBUG_ERR
,("Error when trying to reassign ip\n"));
1959 static int getips_store_callback(void *param
, void *data
)
1961 struct ctdb_public_ip
*node_ip
= (struct ctdb_public_ip
*)data
;
1962 struct ctdb_all_public_ips
*ips
= param
;
1966 ips
->ips
[i
].pnn
= node_ip
->pnn
;
1967 ips
->ips
[i
].addr
= node_ip
->addr
;
1971 static int getips_count_callback(void *param
, void *data
)
1973 uint32_t *count
= param
;
1980 static uint32_t *ip_key(ctdb_sock_addr
*ip
)
1982 static uint32_t key
[IP_KEYLEN
];
1984 bzero(key
, sizeof(key
));
1986 switch (ip
->sa
.sa_family
) {
1988 key
[0] = ip
->ip
.sin_addr
.s_addr
;
1991 uint32_t *s6_a32
= (uint32_t *)&(ip
->ip6
.sin6_addr
.s6_addr
);
1999 DEBUG(DEBUG_ERR
, (__location__
" ERROR, unknown family passed :%u\n", ip
->sa
.sa_family
));
2006 static void *add_ip_callback(void *parm
, void *data
)
2012 control_get_all_public_ips(struct ctdb_context
*ctdb
, TALLOC_CTX
*tmp_ctx
, struct ctdb_all_public_ips
**ips
)
2014 struct ctdb_all_public_ips
*tmp_ips
;
2015 struct ctdb_node_map
*nodemap
=NULL
;
2016 trbt_tree_t
*ip_tree
;
2020 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, tmp_ctx
, &nodemap
);
2022 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
2026 ip_tree
= trbt_create(tmp_ctx
, 0);
2028 for(i
=0;i
<nodemap
->num
;i
++){
2029 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DELETED
) {
2032 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DISCONNECTED
) {
2036 /* read the public ip list from this node */
2037 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), nodemap
->nodes
[i
].pnn
, tmp_ctx
, &tmp_ips
);
2039 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from node %u\n", nodemap
->nodes
[i
].pnn
));
2043 for (j
=0; j
<tmp_ips
->num
;j
++) {
2044 struct ctdb_public_ip
*node_ip
;
2046 node_ip
= talloc(tmp_ctx
, struct ctdb_public_ip
);
2047 node_ip
->pnn
= tmp_ips
->ips
[j
].pnn
;
2048 node_ip
->addr
= tmp_ips
->ips
[j
].addr
;
2050 trbt_insertarray32_callback(ip_tree
,
2051 IP_KEYLEN
, ip_key(&tmp_ips
->ips
[j
].addr
),
2055 talloc_free(tmp_ips
);
2060 trbt_traversearray32(ip_tree
, IP_KEYLEN
, getips_count_callback
, &count
);
2062 len
= offsetof(struct ctdb_all_public_ips
, ips
) +
2063 count
*sizeof(struct ctdb_public_ip
);
2064 tmp_ips
= talloc_zero_size(tmp_ctx
, len
);
2065 trbt_traversearray32(ip_tree
, IP_KEYLEN
, getips_store_callback
, tmp_ips
);
2073 static void ctdb_every_second(struct event_context
*ev
, struct timed_event
*te
, struct timeval t
, void *p
)
2075 struct ctdb_context
*ctdb
= talloc_get_type(p
, struct ctdb_context
);
2077 event_add_timed(ctdb
->ev
, ctdb
,
2078 timeval_current_ofs(1, 0),
2079 ctdb_every_second
, ctdb
);
2082 struct srvid_reply_handler_data
{
2086 const char *srvid_str
;
2089 static void srvid_broadcast_reply_handler(struct ctdb_context
*ctdb
,
2094 struct srvid_reply_handler_data
*d
=
2095 (struct srvid_reply_handler_data
*)private_data
;
2099 if (data
.dsize
!= sizeof(ret
)) {
2100 DEBUG(DEBUG_ERR
, (__location__
" Wrong reply size\n"));
2104 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
2105 ret
= *(int32_t *)data
.dptr
;
2108 ("%s failed with result %d\n", d
->srvid_str
, ret
));
2112 if (!d
->wait_for_all
) {
2117 /* Wait for all replies */
2119 for (i
= 0; i
< talloc_array_length(d
->nodes
); i
++) {
2120 if (d
->nodes
[i
] == ret
) {
2122 ("%s reply received from node %u\n",
2123 d
->srvid_str
, ret
));
2126 if (d
->nodes
[i
] != -1) {
2127 /* Found a node that hasn't yet replied */
2133 /* Broadcast the given SRVID to all connected nodes. Wait for 1 reply
2134 * or replies from all connected nodes. arg is the data argument to
2135 * pass in the srvid_request structure - pass 0 if this isn't needed.
2137 static int srvid_broadcast(struct ctdb_context
*ctdb
,
2138 uint64_t srvid
, uint32_t *arg
,
2139 const char *srvid_str
, bool wait_for_all
)
2144 uint64_t reply_srvid
;
2145 struct srvid_request request
;
2146 struct srvid_request_data request_data
;
2147 struct srvid_reply_handler_data reply_data
;
2150 ZERO_STRUCT(request
);
2152 /* Time ticks to enable timeouts to be processed */
2153 event_add_timed(ctdb
->ev
, ctdb
,
2154 timeval_current_ofs(1, 0),
2155 ctdb_every_second
, ctdb
);
2157 pnn
= ctdb_get_pnn(ctdb
);
2158 reply_srvid
= getpid();
2162 request
.srvid
= reply_srvid
;
2164 data
.dptr
= (uint8_t *)&request
;
2165 data
.dsize
= sizeof(request
);
2167 request_data
.pnn
= pnn
;
2168 request_data
.srvid
= reply_srvid
;
2169 request_data
.data
= *arg
;
2171 data
.dptr
= (uint8_t *)&request_data
;
2172 data
.dsize
= sizeof(request_data
);
2175 /* Register message port for reply from recovery master */
2176 ctdb_client_set_message_handler(ctdb
, reply_srvid
,
2177 srvid_broadcast_reply_handler
,
2180 reply_data
.wait_for_all
= wait_for_all
;
2181 reply_data
.nodes
= NULL
;
2182 reply_data
.srvid_str
= srvid_str
;
2185 reply_data
.done
= false;
2188 struct ctdb_node_map
*nodemap
;
2190 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(),
2191 CTDB_CURRENT_NODE
, ctdb
, &nodemap
);
2194 ("Unable to get nodemap from current node, try again\n"));
2199 if (reply_data
.nodes
!= NULL
) {
2200 talloc_free(reply_data
.nodes
);
2202 reply_data
.nodes
= list_of_connected_nodes(ctdb
, nodemap
,
2205 talloc_free(nodemap
);
2208 /* Send to all connected nodes. Only recmaster replies */
2209 ret
= ctdb_client_send_message(ctdb
, CTDB_BROADCAST_CONNECTED
,
2212 /* This can only happen if the socket is closed and
2213 * there's no way to recover from that, so don't try
2217 ("Failed to send %s request to connected nodes\n",
2222 tv
= timeval_current();
2223 /* This loop terminates the reply is received */
2224 while (timeval_elapsed(&tv
) < 5.0 && !reply_data
.done
) {
2225 event_loop_once(ctdb
->ev
);
2228 if (!reply_data
.done
) {
2230 ("Still waiting for confirmation of %s\n", srvid_str
));
2235 ctdb_client_remove_message_handler(ctdb
, reply_srvid
, &reply_data
);
2237 talloc_free(reply_data
.nodes
);
2242 static int ipreallocate(struct ctdb_context
*ctdb
)
2244 return srvid_broadcast(ctdb
, CTDB_SRVID_TAKEOVER_RUN
, NULL
,
2245 "IP reallocation", false);
2249 static int control_ipreallocate(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2251 return ipreallocate(ctdb
);
2255 add a public ip address to a node
2257 static int control_addip(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2260 int len
, retries
= 0;
2262 ctdb_sock_addr addr
;
2263 struct ctdb_control_ip_iface
*pub
;
2264 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
2265 struct ctdb_all_public_ips
*ips
;
2269 talloc_free(tmp_ctx
);
2273 if (!parse_ip_mask(argv
[0], argv
[1], &addr
, &mask
)) {
2274 DEBUG(DEBUG_ERR
, ("Badly formed ip/mask : %s\n", argv
[0]));
2275 talloc_free(tmp_ctx
);
2279 /* read the public ip list from the node */
2280 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &ips
);
2282 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from node %u\n", options
.pnn
));
2283 talloc_free(tmp_ctx
);
2286 for (i
=0;i
<ips
->num
;i
++) {
2287 if (ctdb_same_ip(&addr
, &ips
->ips
[i
].addr
)) {
2288 DEBUG(DEBUG_ERR
,("Can not add ip to node. Node already hosts this ip\n"));
2295 /* Dont timeout. This command waits for an ip reallocation
2296 which sometimes can take wuite a while if there has
2297 been a recent recovery
2301 len
= offsetof(struct ctdb_control_ip_iface
, iface
) + strlen(argv
[1]) + 1;
2302 pub
= talloc_size(tmp_ctx
, len
);
2303 CTDB_NO_MEMORY(ctdb
, pub
);
2307 pub
->len
= strlen(argv
[1])+1;
2308 memcpy(&pub
->iface
[0], argv
[1], strlen(argv
[1])+1);
2311 ret
= ctdb_ctrl_add_public_ip(ctdb
, TIMELIMIT(), options
.pnn
, pub
);
2313 DEBUG(DEBUG_ERR
, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options
.pnn
));
2317 } while (retries
< 5 && ret
!= 0);
2319 DEBUG(DEBUG_ERR
, ("Unable to add public ip to node %u. Giving up.\n", options
.pnn
));
2320 talloc_free(tmp_ctx
);
2324 if (rebalance_node(ctdb
, options
.pnn
) != 0) {
2325 DEBUG(DEBUG_ERR
,("Error when trying to rebalance node\n"));
2329 talloc_free(tmp_ctx
);
2334 add a public ip address to a node
2336 static int control_ipiface(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2338 ctdb_sock_addr addr
;
2344 if (!parse_ip(argv
[0], NULL
, 0, &addr
)) {
2345 printf("Badly formed ip : %s\n", argv
[0]);
2349 printf("IP on interface %s\n", ctdb_sys_find_ifname(&addr
));
2354 static int control_delip(struct ctdb_context
*ctdb
, int argc
, const char **argv
);
2356 static int control_delip_all(struct ctdb_context
*ctdb
, int argc
, const char **argv
, ctdb_sock_addr
*addr
)
2358 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
2359 struct ctdb_node_map
*nodemap
=NULL
;
2360 struct ctdb_all_public_ips
*ips
;
2363 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, tmp_ctx
, &nodemap
);
2365 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from current node\n"));
2369 /* remove it from the nodes that are not hosting the ip currently */
2370 for(i
=0;i
<nodemap
->num
;i
++){
2371 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_INACTIVE
) {
2374 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), nodemap
->nodes
[i
].pnn
, tmp_ctx
, &ips
);
2376 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from node %d\n", nodemap
->nodes
[i
].pnn
));
2380 for (j
=0;j
<ips
->num
;j
++) {
2381 if (ctdb_same_ip(addr
, &ips
->ips
[j
].addr
)) {
2389 if (ips
->ips
[j
].pnn
== nodemap
->nodes
[i
].pnn
) {
2393 options
.pnn
= nodemap
->nodes
[i
].pnn
;
2394 control_delip(ctdb
, argc
, argv
);
2398 /* remove it from every node (also the one hosting it) */
2399 for(i
=0;i
<nodemap
->num
;i
++){
2400 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_INACTIVE
) {
2403 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), nodemap
->nodes
[i
].pnn
, tmp_ctx
, &ips
);
2405 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from node %d\n", nodemap
->nodes
[i
].pnn
));
2409 for (j
=0;j
<ips
->num
;j
++) {
2410 if (ctdb_same_ip(addr
, &ips
->ips
[j
].addr
)) {
2418 options
.pnn
= nodemap
->nodes
[i
].pnn
;
2419 control_delip(ctdb
, argc
, argv
);
2422 talloc_free(tmp_ctx
);
2427 delete a public ip address from a node
2429 static int control_delip(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2432 ctdb_sock_addr addr
;
2433 struct ctdb_control_ip_iface pub
;
2434 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
2435 struct ctdb_all_public_ips
*ips
;
2438 talloc_free(tmp_ctx
);
2442 if (parse_ip(argv
[0], NULL
, 0, &addr
) == 0) {
2443 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
2447 if (options
.pnn
== CTDB_BROADCAST_ALL
) {
2448 return control_delip_all(ctdb
, argc
, argv
, &addr
);
2455 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &ips
);
2457 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from cluster\n"));
2458 talloc_free(tmp_ctx
);
2462 for (i
=0;i
<ips
->num
;i
++) {
2463 if (ctdb_same_ip(&addr
, &ips
->ips
[i
].addr
)) {
2469 DEBUG(DEBUG_ERR
, ("This node does not support this public address '%s'\n",
2470 ctdb_addr_to_str(&addr
)));
2471 talloc_free(tmp_ctx
);
2475 /* This is an optimisation. If this node is hosting the IP
2476 * then try to move it somewhere else without invoking a full
2477 * takeover run. We don't care if this doesn't work!
2479 if (ips
->ips
[i
].pnn
== options
.pnn
) {
2480 (void) try_moveip(ctdb
, &addr
, -1);
2483 ret
= ctdb_ctrl_del_public_ip(ctdb
, TIMELIMIT(), options
.pnn
, &pub
);
2485 DEBUG(DEBUG_ERR
, ("Unable to del public ip from node %u\n", options
.pnn
));
2486 talloc_free(tmp_ctx
);
2490 talloc_free(tmp_ctx
);
2494 static int kill_tcp_from_file(struct ctdb_context
*ctdb
,
2495 int argc
, const char **argv
)
2497 struct ctdb_control_killtcp
*killtcp
;
2498 int max_entries
, current
, i
;
2499 struct timeval timeout
;
2500 char line
[128], src
[128], dst
[128];
2503 struct client_async_data
*async_data
;
2504 struct ctdb_client_control_state
*state
;
2514 while (!feof(stdin
)) {
2515 if (fgets(line
, sizeof(line
), stdin
) == NULL
) {
2519 /* Silently skip empty lines */
2520 if (line
[0] == '\n') {
2524 if (sscanf(line
, "%s %s\n", src
, dst
) != 2) {
2525 DEBUG(DEBUG_ERR
, ("Bad line [%d]: '%s'\n",
2527 talloc_free(killtcp
);
2531 if (current
>= max_entries
) {
2532 max_entries
+= 1024;
2533 killtcp
= talloc_realloc(ctdb
, killtcp
,
2534 struct ctdb_control_killtcp
,
2536 CTDB_NO_MEMORY(ctdb
, killtcp
);
2539 if (!parse_ip_port(src
, &killtcp
[current
].src_addr
)) {
2540 DEBUG(DEBUG_ERR
, ("Bad IP:port on line [%d]: '%s'\n",
2542 talloc_free(killtcp
);
2546 if (!parse_ip_port(dst
, &killtcp
[current
].dst_addr
)) {
2547 DEBUG(DEBUG_ERR
, ("Bad IP:port on line [%d]: '%s'\n",
2549 talloc_free(killtcp
);
2556 async_data
= talloc_zero(ctdb
, struct client_async_data
);
2557 if (async_data
== NULL
) {
2558 talloc_free(killtcp
);
2562 for (i
= 0; i
< current
; i
++) {
2564 data
.dsize
= sizeof(struct ctdb_control_killtcp
);
2565 data
.dptr
= (unsigned char *)&killtcp
[i
];
2567 timeout
= TIMELIMIT();
2568 state
= ctdb_control_send(ctdb
, options
.pnn
, 0,
2569 CTDB_CONTROL_KILL_TCP
, 0, data
,
2570 async_data
, &timeout
, NULL
);
2572 if (state
== NULL
) {
2574 ("Failed to call async killtcp control to node %u\n",
2576 talloc_free(killtcp
);
2580 ctdb_client_async_add(async_data
, state
);
2583 if (ctdb_client_async_wait(ctdb
, async_data
) != 0) {
2584 DEBUG(DEBUG_ERR
,("killtcp failed\n"));
2585 talloc_free(killtcp
);
2589 talloc_free(killtcp
);
2595 kill a tcp connection
2597 static int kill_tcp(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2600 struct ctdb_control_killtcp killtcp
;
2602 assert_single_node_only();
2605 return kill_tcp_from_file(ctdb
, argc
, argv
);
2612 if (!parse_ip_port(argv
[0], &killtcp
.src_addr
)) {
2613 DEBUG(DEBUG_ERR
, ("Bad IP:port '%s'\n", argv
[0]));
2617 if (!parse_ip_port(argv
[1], &killtcp
.dst_addr
)) {
2618 DEBUG(DEBUG_ERR
, ("Bad IP:port '%s'\n", argv
[1]));
2622 ret
= ctdb_ctrl_killtcp(ctdb
, TIMELIMIT(), options
.pnn
, &killtcp
);
2624 DEBUG(DEBUG_ERR
, ("Unable to killtcp from node %u\n", options
.pnn
));
2635 static int control_gratious_arp(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2638 ctdb_sock_addr addr
;
2640 assert_single_node_only();
2646 if (!parse_ip(argv
[0], NULL
, 0, &addr
)) {
2647 DEBUG(DEBUG_ERR
, ("Bad IP '%s'\n", argv
[0]));
2651 ret
= ctdb_ctrl_gratious_arp(ctdb
, TIMELIMIT(), options
.pnn
, &addr
, argv
[1]);
2653 DEBUG(DEBUG_ERR
, ("Unable to send gratious_arp from node %u\n", options
.pnn
));
2661 register a server id
2663 static int regsrvid(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2666 struct ctdb_server_id server_id
;
2672 server_id
.pnn
= strtoul(argv
[0], NULL
, 0);
2673 server_id
.type
= strtoul(argv
[1], NULL
, 0);
2674 server_id
.server_id
= strtoul(argv
[2], NULL
, 0);
2676 ret
= ctdb_ctrl_register_server_id(ctdb
, TIMELIMIT(), &server_id
);
2678 DEBUG(DEBUG_ERR
, ("Unable to register server_id from node %u\n", options
.pnn
));
2681 DEBUG(DEBUG_ERR
,("Srvid registered. Sleeping for 999 seconds\n"));
2687 unregister a server id
2689 static int unregsrvid(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2692 struct ctdb_server_id server_id
;
2698 server_id
.pnn
= strtoul(argv
[0], NULL
, 0);
2699 server_id
.type
= strtoul(argv
[1], NULL
, 0);
2700 server_id
.server_id
= strtoul(argv
[2], NULL
, 0);
2702 ret
= ctdb_ctrl_unregister_server_id(ctdb
, TIMELIMIT(), &server_id
);
2704 DEBUG(DEBUG_ERR
, ("Unable to unregister server_id from node %u\n", options
.pnn
));
2711 check if a server id exists
2713 static int chksrvid(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2717 struct ctdb_server_id server_id
;
2723 server_id
.pnn
= strtoul(argv
[0], NULL
, 0);
2724 server_id
.type
= strtoul(argv
[1], NULL
, 0);
2725 server_id
.server_id
= strtoul(argv
[2], NULL
, 0);
2727 ret
= ctdb_ctrl_check_server_id(ctdb
, TIMELIMIT(), options
.pnn
, &server_id
, &status
);
2729 DEBUG(DEBUG_ERR
, ("Unable to check server_id from node %u\n", options
.pnn
));
2734 printf("Server id %d:%d:%d EXISTS\n", server_id
.pnn
, server_id
.type
, server_id
.server_id
);
2736 printf("Server id %d:%d:%d does NOT exist\n", server_id
.pnn
, server_id
.type
, server_id
.server_id
);
2742 get a list of all server ids that are registered on a node
2744 static int getsrvids(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2747 struct ctdb_server_id_list
*server_ids
;
2749 ret
= ctdb_ctrl_get_server_id_list(ctdb
, ctdb
, TIMELIMIT(), options
.pnn
, &server_ids
);
2751 DEBUG(DEBUG_ERR
, ("Unable to get server_id list from node %u\n", options
.pnn
));
2755 for (i
=0; i
<server_ids
->num
; i
++) {
2756 printf("Server id %d:%d:%d\n",
2757 server_ids
->server_ids
[i
].pnn
,
2758 server_ids
->server_ids
[i
].type
,
2759 server_ids
->server_ids
[i
].server_id
);
2766 check if a server id exists
2768 static int check_srvids(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2770 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
2776 talloc_free(tmp_ctx
);
2780 ids
= talloc_array(tmp_ctx
, uint64_t, argc
);
2781 result
= talloc_array(tmp_ctx
, uint8_t, argc
);
2783 for (i
= 0; i
< argc
; i
++) {
2784 ids
[i
] = strtoull(argv
[i
], NULL
, 0);
2787 if (!ctdb_client_check_message_handlers(ctdb
, ids
, argc
, result
)) {
2788 DEBUG(DEBUG_ERR
, ("Unable to check server_id from node %u\n",
2790 talloc_free(tmp_ctx
);
2794 for (i
=0; i
< argc
; i
++) {
2795 printf("Server id %d:%llu %s\n", options
.pnn
, (long long)ids
[i
],
2796 result
[i
] ? "exists" : "does not exist");
2799 talloc_free(tmp_ctx
);
2804 send a tcp tickle ack
2806 static int tickle_tcp(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2809 ctdb_sock_addr src
, dst
;
2815 if (!parse_ip_port(argv
[0], &src
)) {
2816 DEBUG(DEBUG_ERR
, ("Bad IP:port '%s'\n", argv
[0]));
2820 if (!parse_ip_port(argv
[1], &dst
)) {
2821 DEBUG(DEBUG_ERR
, ("Bad IP:port '%s'\n", argv
[1]));
2825 ret
= ctdb_sys_send_tcp(&src
, &dst
, 0, 0, 0);
2829 DEBUG(DEBUG_ERR
, ("Error while sending tickle ack\n"));
2836 display public ip status
2838 static int control_ip(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2841 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
2842 struct ctdb_all_public_ips
*ips
;
2844 if (options
.pnn
== CTDB_BROADCAST_ALL
) {
2845 /* read the list of public ips from all nodes */
2846 ret
= control_get_all_public_ips(ctdb
, tmp_ctx
, &ips
);
2848 /* read the public ip list from this node */
2849 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &ips
);
2852 DEBUG(DEBUG_ERR
, ("Unable to get public ips from node %u\n", options
.pnn
));
2853 talloc_free(tmp_ctx
);
2857 if (options
.machinereadable
){
2858 printf(":Public IP:Node:");
2859 if (options
.verbose
){
2860 printf("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2864 if (options
.pnn
== CTDB_BROADCAST_ALL
) {
2865 printf("Public IPs on ALL nodes\n");
2867 printf("Public IPs on node %u\n", options
.pnn
);
2871 for (i
=1;i
<=ips
->num
;i
++) {
2872 struct ctdb_control_public_ip_info
*info
= NULL
;
2874 char *aciface
= NULL
;
2875 char *avifaces
= NULL
;
2876 char *cifaces
= NULL
;
2878 if (options
.pnn
== CTDB_BROADCAST_ALL
) {
2879 pnn
= ips
->ips
[ips
->num
-i
].pnn
;
2885 ret
= ctdb_ctrl_get_public_ip_info(ctdb
, TIMELIMIT(), pnn
, ctdb
,
2886 &ips
->ips
[ips
->num
-i
].addr
, &info
);
2893 for (j
=0; j
< info
->num
; j
++) {
2894 if (cifaces
== NULL
) {
2895 cifaces
= talloc_strdup(info
,
2896 info
->ifaces
[j
].name
);
2898 cifaces
= talloc_asprintf_append(cifaces
,
2900 info
->ifaces
[j
].name
);
2903 if (info
->active_idx
== j
) {
2904 aciface
= info
->ifaces
[j
].name
;
2907 if (info
->ifaces
[j
].link_state
== 0) {
2911 if (avifaces
== NULL
) {
2912 avifaces
= talloc_strdup(info
, info
->ifaces
[j
].name
);
2914 avifaces
= talloc_asprintf_append(avifaces
,
2916 info
->ifaces
[j
].name
);
2921 if (options
.machinereadable
){
2923 ctdb_addr_to_str(&ips
->ips
[ips
->num
-i
].addr
),
2924 ips
->ips
[ips
->num
-i
].pnn
);
2925 if (options
.verbose
){
2928 avifaces
?avifaces
:"",
2929 cifaces
?cifaces
:"");
2933 if (options
.verbose
) {
2934 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
2935 ctdb_addr_to_str(&ips
->ips
[ips
->num
-i
].addr
),
2936 ips
->ips
[ips
->num
-i
].pnn
,
2938 avifaces
?avifaces
:"",
2939 cifaces
?cifaces
:"");
2942 ctdb_addr_to_str(&ips
->ips
[ips
->num
-i
].addr
),
2943 ips
->ips
[ips
->num
-i
].pnn
);
2949 talloc_free(tmp_ctx
);
2956 static int control_ipinfo(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2959 ctdb_sock_addr addr
;
2960 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
2961 struct ctdb_control_public_ip_info
*info
;
2964 talloc_free(tmp_ctx
);
2968 if (parse_ip(argv
[0], NULL
, 0, &addr
) == 0) {
2969 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
2973 /* read the public ip info from this node */
2974 ret
= ctdb_ctrl_get_public_ip_info(ctdb
, TIMELIMIT(), options
.pnn
,
2975 tmp_ctx
, &addr
, &info
);
2977 DEBUG(DEBUG_ERR
, ("Unable to get public ip[%s]info from node %u\n",
2978 argv
[0], options
.pnn
));
2979 talloc_free(tmp_ctx
);
2983 printf("Public IP[%s] info on node %u\n",
2984 ctdb_addr_to_str(&info
->ip
.addr
),
2987 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
2988 ctdb_addr_to_str(&info
->ip
.addr
),
2989 info
->ip
.pnn
, info
->num
);
2991 for (i
=0; i
<info
->num
; i
++) {
2992 info
->ifaces
[i
].name
[CTDB_IFACE_SIZE
] = '\0';
2994 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
2995 i
+1, info
->ifaces
[i
].name
,
2996 info
->ifaces
[i
].link_state
?"up":"down",
2997 (unsigned int)info
->ifaces
[i
].references
,
2998 (i
==info
->active_idx
)?" (active)":"");
3001 talloc_free(tmp_ctx
);
3006 display interfaces status
3008 static int control_ifaces(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3010 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3012 struct ctdb_control_get_ifaces
*ifaces
;
3015 /* read the public ip list from this node */
3016 ret
= ctdb_ctrl_get_ifaces(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &ifaces
);
3018 DEBUG(DEBUG_ERR
, ("Unable to get interfaces from node %u\n",
3020 talloc_free(tmp_ctx
);
3024 if (options
.machinereadable
){
3025 printf(":Name:LinkStatus:References:\n");
3027 printf("Interfaces on node %u\n", options
.pnn
);
3030 for (i
=0; i
<ifaces
->num
; i
++) {
3031 if (options
.machinereadable
){
3032 printf(":%s:%s:%u\n",
3033 ifaces
->ifaces
[i
].name
,
3034 ifaces
->ifaces
[i
].link_state
?"1":"0",
3035 (unsigned int)ifaces
->ifaces
[i
].references
);
3037 printf("name:%s link:%s references:%u\n",
3038 ifaces
->ifaces
[i
].name
,
3039 ifaces
->ifaces
[i
].link_state
?"up":"down",
3040 (unsigned int)ifaces
->ifaces
[i
].references
);
3044 talloc_free(tmp_ctx
);
3050 set link status of an interface
3052 static int control_setifacelink(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3055 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3056 struct ctdb_control_iface_info info
;
3064 if (strlen(argv
[0]) > CTDB_IFACE_SIZE
) {
3065 DEBUG(DEBUG_ERR
, ("interfaces name '%s' too long\n",
3067 talloc_free(tmp_ctx
);
3070 strcpy(info
.name
, argv
[0]);
3072 if (strcmp(argv
[1], "up") == 0) {
3073 info
.link_state
= 1;
3074 } else if (strcmp(argv
[1], "down") == 0) {
3075 info
.link_state
= 0;
3077 DEBUG(DEBUG_ERR
, ("link state invalid '%s' should be 'up' or 'down'\n",
3079 talloc_free(tmp_ctx
);
3083 /* read the public ip list from this node */
3084 ret
= ctdb_ctrl_set_iface_link(ctdb
, TIMELIMIT(), options
.pnn
,
3087 DEBUG(DEBUG_ERR
, ("Unable to set link state for interfaces %s node %u\n",
3088 argv
[0], options
.pnn
));
3089 talloc_free(tmp_ctx
);
3093 talloc_free(tmp_ctx
);
3098 display pid of a ctdb daemon
3100 static int control_getpid(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3105 ret
= ctdb_ctrl_getpid(ctdb
, TIMELIMIT(), options
.pnn
, &pid
);
3107 DEBUG(DEBUG_ERR
, ("Unable to get daemon pid from node %u\n", options
.pnn
));
3110 printf("Pid:%d\n", pid
);
3115 typedef bool update_flags_handler_t(struct ctdb_context
*ctdb
, void *data
);
3117 static int update_flags_and_ipreallocate(struct ctdb_context
*ctdb
,
3119 update_flags_handler_t handler
,
3124 struct ctdb_node_map
*nodemap
= NULL
;
3128 /* Check if the node is already in the desired state */
3129 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, ctdb
, &nodemap
);
3131 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from local node\n"));
3134 flag_is_set
= nodemap
->nodes
[options
.pnn
].flags
& flag
;
3135 if (set_flag
== flag_is_set
) {
3136 DEBUG(DEBUG_NOTICE
, ("Node %d is %s %s\n", options
.pnn
,
3137 (set_flag
? "already" : "not"), desc
));
3142 if (!handler(ctdb
, data
)) {
3143 DEBUG(DEBUG_WARNING
,
3144 ("Failed to send control to set state %s on node %u, try again\n",
3145 desc
, options
.pnn
));
3150 /* Read the nodemap and verify the change took effect.
3151 * Even if the above control/hanlder timed out then it
3152 * could still have worked!
3154 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
,
3157 DEBUG(DEBUG_WARNING
,
3158 ("Unable to get nodemap from local node, try again\n"));
3160 flag_is_set
= nodemap
->nodes
[options
.pnn
].flags
& flag
;
3161 } while (nodemap
== NULL
|| (set_flag
!= flag_is_set
));
3163 return ipreallocate(ctdb
);
3166 /* Administratively disable a node */
3167 static bool update_flags_disabled(struct ctdb_context
*ctdb
, void *data
)
3171 ret
= ctdb_ctrl_modflags(ctdb
, TIMELIMIT(), options
.pnn
,
3172 NODE_FLAGS_PERMANENTLY_DISABLED
, 0);
3176 static int control_disable(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3178 return update_flags_and_ipreallocate(ctdb
, NULL
,
3179 update_flags_disabled
,
3180 NODE_FLAGS_PERMANENTLY_DISABLED
,
3182 true /* set_flag*/);
3185 /* Administratively re-enable a node */
3186 static bool update_flags_not_disabled(struct ctdb_context
*ctdb
, void *data
)
3190 ret
= ctdb_ctrl_modflags(ctdb
, TIMELIMIT(), options
.pnn
,
3191 0, NODE_FLAGS_PERMANENTLY_DISABLED
);
3195 static int control_enable(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3197 return update_flags_and_ipreallocate(ctdb
, NULL
,
3198 update_flags_not_disabled
,
3199 NODE_FLAGS_PERMANENTLY_DISABLED
,
3201 false /* set_flag*/);
3205 static bool update_flags_stopped(struct ctdb_context
*ctdb
, void *data
)
3209 ret
= ctdb_ctrl_stop_node(ctdb
, TIMELIMIT(), options
.pnn
);
3214 static int control_stop(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3216 return update_flags_and_ipreallocate(ctdb
, NULL
,
3217 update_flags_stopped
,
3220 true /* set_flag*/);
3223 /* Continue a stopped node */
3224 static bool update_flags_not_stopped(struct ctdb_context
*ctdb
, void *data
)
3228 ret
= ctdb_ctrl_continue_node(ctdb
, TIMELIMIT(), options
.pnn
);
3233 static int control_continue(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3235 return update_flags_and_ipreallocate(ctdb
, NULL
,
3236 update_flags_not_stopped
,
3239 false /* set_flag */);
3242 static uint32_t get_generation(struct ctdb_context
*ctdb
)
3244 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3245 struct ctdb_vnn_map
*vnnmap
=NULL
;
3247 uint32_t generation
;
3249 /* wait until the recmaster is not in recovery mode */
3251 uint32_t recmode
, recmaster
;
3253 if (vnnmap
!= NULL
) {
3254 talloc_free(vnnmap
);
3258 /* get the recmaster */
3259 ret
= ctdb_ctrl_getrecmaster(ctdb
, tmp_ctx
, TIMELIMIT(), CTDB_CURRENT_NODE
, &recmaster
);
3261 DEBUG(DEBUG_ERR
, ("Unable to get recmaster from node %u\n", options
.pnn
));
3262 talloc_free(tmp_ctx
);
3266 /* get recovery mode */
3267 ret
= ctdb_ctrl_getrecmode(ctdb
, tmp_ctx
, TIMELIMIT(), recmaster
, &recmode
);
3269 DEBUG(DEBUG_ERR
, ("Unable to get recmode from node %u\n", options
.pnn
));
3270 talloc_free(tmp_ctx
);
3274 /* get the current generation number */
3275 ret
= ctdb_ctrl_getvnnmap(ctdb
, TIMELIMIT(), recmaster
, tmp_ctx
, &vnnmap
);
3277 DEBUG(DEBUG_ERR
, ("Unable to get vnnmap from recmaster (%u)\n", recmaster
));
3278 talloc_free(tmp_ctx
);
3282 if ((recmode
== CTDB_RECOVERY_NORMAL
) && (vnnmap
->generation
!= 1)) {
3283 generation
= vnnmap
->generation
;
3284 talloc_free(tmp_ctx
);
3292 static bool update_state_banned(struct ctdb_context
*ctdb
, void *data
)
3294 struct ctdb_ban_time
*bantime
= (struct ctdb_ban_time
*)data
;
3297 ret
= ctdb_ctrl_set_ban(ctdb
, TIMELIMIT(), options
.pnn
, bantime
);
3302 static int control_ban(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3304 struct ctdb_ban_time bantime
;
3310 bantime
.pnn
= options
.pnn
;
3311 bantime
.time
= strtoul(argv
[0], NULL
, 0);
3313 if (bantime
.time
== 0) {
3314 DEBUG(DEBUG_ERR
, ("Invalid ban time specified - must be >0\n"));
3318 return update_flags_and_ipreallocate(ctdb
, &bantime
,
3319 update_state_banned
,
3322 true /* set_flag*/);
3327 static int control_unban(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3329 struct ctdb_ban_time bantime
;
3331 bantime
.pnn
= options
.pnn
;
3334 return update_flags_and_ipreallocate(ctdb
, &bantime
,
3335 update_state_banned
,
3338 false /* set_flag*/);
3342 show ban information for a node
3344 static int control_showban(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3347 struct ctdb_node_map
*nodemap
=NULL
;
3348 struct ctdb_ban_time
*bantime
;
3350 /* verify the node exists */
3351 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, ctdb
, &nodemap
);
3353 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from local node\n"));
3357 ret
= ctdb_ctrl_get_ban(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &bantime
);
3359 DEBUG(DEBUG_ERR
,("Showing ban info for node %d failed.\n", options
.pnn
));
3363 if (bantime
->time
== 0) {
3364 printf("Node %u is not banned\n", bantime
->pnn
);
3366 printf("Node %u is banned, %d seconds remaining\n",
3367 bantime
->pnn
, bantime
->time
);
3376 static int control_shutdown(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3380 ret
= ctdb_ctrl_shutdown(ctdb
, TIMELIMIT(), options
.pnn
);
3382 DEBUG(DEBUG_ERR
, ("Unable to shutdown node %u\n", options
.pnn
));
3392 static int control_recover(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3395 uint32_t generation
, next_generation
;
3398 /* "force" option ignores freeze failure and forces recovery */
3399 force
= (argc
== 1) && (strcasecmp(argv
[0], "force") == 0);
3401 /* record the current generation number */
3402 generation
= get_generation(ctdb
);
3404 ret
= ctdb_ctrl_freeze_priority(ctdb
, TIMELIMIT(), options
.pnn
, 1);
3407 DEBUG(DEBUG_ERR
, ("Unable to freeze node\n"));
3410 DEBUG(DEBUG_WARNING
, ("Unable to freeze node but proceeding because \"force\" option given\n"));
3413 ret
= ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
3415 DEBUG(DEBUG_ERR
, ("Unable to set recovery mode\n"));
3419 /* wait until we are in a new generation */
3421 next_generation
= get_generation(ctdb
);
3422 if (next_generation
!= generation
) {
3433 display monitoring mode of a remote node
3435 static int control_getmonmode(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3440 ret
= ctdb_ctrl_getmonmode(ctdb
, TIMELIMIT(), options
.pnn
, &monmode
);
3442 DEBUG(DEBUG_ERR
, ("Unable to get monmode from node %u\n", options
.pnn
));
3445 if (!options
.machinereadable
){
3446 printf("Monitoring mode:%s (%d)\n",monmode
==CTDB_MONITORING_ACTIVE
?"ACTIVE":"DISABLED",monmode
);
3449 printf(":%d:\n",monmode
);
3456 display capabilities of a remote node
3458 static int control_getcapabilities(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3460 uint32_t capabilities
;
3463 ret
= ctdb_ctrl_getcapabilities(ctdb
, TIMELIMIT(), options
.pnn
, &capabilities
);
3465 DEBUG(DEBUG_ERR
, ("Unable to get capabilities from node %u\n", options
.pnn
));
3469 if (!options
.machinereadable
){
3470 printf("RECMASTER: %s\n", (capabilities
&CTDB_CAP_RECMASTER
)?"YES":"NO");
3471 printf("LMASTER: %s\n", (capabilities
&CTDB_CAP_LMASTER
)?"YES":"NO");
3472 printf("LVS: %s\n", (capabilities
&CTDB_CAP_LVS
)?"YES":"NO");
3473 printf("NATGW: %s\n", (capabilities
&CTDB_CAP_NATGW
)?"YES":"NO");
3475 printf(":RECMASTER:LMASTER:LVS:NATGW:\n");
3476 printf(":%d:%d:%d:%d:\n",
3477 !!(capabilities
&CTDB_CAP_RECMASTER
),
3478 !!(capabilities
&CTDB_CAP_LMASTER
),
3479 !!(capabilities
&CTDB_CAP_LVS
),
3480 !!(capabilities
&CTDB_CAP_NATGW
));
3486 display lvs configuration
3488 static int control_lvs(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3490 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3491 uint32_t *capabilities
;
3492 struct ctdb_node_map
*nodemap
=NULL
;
3494 int healthy_count
= 0;
3496 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &nodemap
);
3498 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
3499 talloc_free(tmp_ctx
);
3503 capabilities
= talloc_array(ctdb
, uint32_t, nodemap
->num
);
3504 CTDB_NO_MEMORY(ctdb
, capabilities
);
3508 /* collect capabilities for all connected nodes */
3509 for (i
=0; i
<nodemap
->num
; i
++) {
3510 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_INACTIVE
) {
3513 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_PERMANENTLY_DISABLED
) {
3517 ret
= ctdb_ctrl_getcapabilities(ctdb
, TIMELIMIT(), i
, &capabilities
[i
]);
3519 DEBUG(DEBUG_ERR
, ("Unable to get capabilities from node %u\n", i
));
3524 if (!(capabilities
[i
] & CTDB_CAP_LVS
)) {
3528 if (!(nodemap
->nodes
[i
].flags
& NODE_FLAGS_UNHEALTHY
)) {
3533 /* Print all LVS nodes */
3534 for (i
=0; i
<nodemap
->num
; i
++) {
3535 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_INACTIVE
) {
3538 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_PERMANENTLY_DISABLED
) {
3541 if (!(capabilities
[i
] & CTDB_CAP_LVS
)) {
3545 if (healthy_count
!= 0) {
3546 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_UNHEALTHY
) {
3551 printf("%d:%s\n", i
,
3552 ctdb_addr_to_str(&nodemap
->nodes
[i
].addr
));
3556 talloc_free(tmp_ctx
);
3561 display who is the lvs master
3563 static int control_lvsmaster(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3565 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3566 uint32_t *capabilities
;
3567 struct ctdb_node_map
*nodemap
=NULL
;
3569 int healthy_count
= 0;
3571 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &nodemap
);
3573 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
3574 talloc_free(tmp_ctx
);
3578 capabilities
= talloc_array(tmp_ctx
, uint32_t, nodemap
->num
);
3579 if (capabilities
== NULL
) {
3580 talloc_free(tmp_ctx
);
3581 CTDB_NO_MEMORY(ctdb
, capabilities
);
3584 /* collect capabilities for all connected nodes */
3585 for (i
=0; i
<nodemap
->num
; i
++) {
3586 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_INACTIVE
) {
3589 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_PERMANENTLY_DISABLED
) {
3593 ret
= ctdb_ctrl_getcapabilities(ctdb
, TIMELIMIT(), i
, &capabilities
[i
]);
3595 DEBUG(DEBUG_ERR
, ("Unable to get capabilities from node %u\n", i
));
3600 if (!(capabilities
[i
] & CTDB_CAP_LVS
)) {
3604 if (!(nodemap
->nodes
[i
].flags
& NODE_FLAGS_UNHEALTHY
)) {
3611 /* find and show the lvsmaster */
3612 for (i
=0; i
<nodemap
->num
; i
++) {
3613 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_INACTIVE
) {
3616 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_PERMANENTLY_DISABLED
) {
3619 if (!(capabilities
[i
] & CTDB_CAP_LVS
)) {
3623 if (healthy_count
!= 0) {
3624 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_UNHEALTHY
) {
3629 if (options
.machinereadable
){
3632 printf("Node %d is LVS master\n", i
);
3638 printf("There is no LVS master\n");
3640 talloc_free(tmp_ctx
);
3645 disable monitoring on a node
3647 static int control_disable_monmode(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3652 ret
= ctdb_ctrl_disable_monmode(ctdb
, TIMELIMIT(), options
.pnn
);
3654 DEBUG(DEBUG_ERR
, ("Unable to disable monmode on node %u\n", options
.pnn
));
3657 printf("Monitoring mode:%s\n","DISABLED");
3663 enable monitoring on a node
3665 static int control_enable_monmode(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3670 ret
= ctdb_ctrl_enable_monmode(ctdb
, TIMELIMIT(), options
.pnn
);
3672 DEBUG(DEBUG_ERR
, ("Unable to enable monmode on node %u\n", options
.pnn
));
3675 printf("Monitoring mode:%s\n","ACTIVE");
3681 display remote list of keys/data for a db
3683 static int control_catdb(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3685 const char *db_name
;
3686 struct ctdb_db_context
*ctdb_db
;
3688 struct ctdb_dump_db_context c
;
3695 if (!db_exists(ctdb
, argv
[0], NULL
, &db_name
, &flags
)) {
3699 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, flags
& CTDB_DB_FLAGS_PERSISTENT
, 0);
3700 if (ctdb_db
== NULL
) {
3701 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
3705 if (options
.printlmaster
) {
3706 ret
= ctdb_ctrl_getvnnmap(ctdb
, TIMELIMIT(), options
.pnn
,
3707 ctdb
, &ctdb
->vnn_map
);
3709 DEBUG(DEBUG_ERR
, ("Unable to get vnnmap from node %u\n",
3717 c
.printemptyrecords
= (bool)options
.printemptyrecords
;
3718 c
.printdatasize
= (bool)options
.printdatasize
;
3719 c
.printlmaster
= (bool)options
.printlmaster
;
3720 c
.printhash
= (bool)options
.printhash
;
3721 c
.printrecordflags
= (bool)options
.printrecordflags
;
3723 /* traverse and dump the cluster tdb */
3724 ret
= ctdb_dump_db(ctdb_db
, &c
);
3726 DEBUG(DEBUG_ERR
, ("Unable to dump database\n"));
3727 DEBUG(DEBUG_ERR
, ("Maybe try 'ctdb getdbstatus %s'"
3728 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3732 talloc_free(ctdb_db
);
3734 printf("Dumped %d records\n", ret
);
3738 struct cattdb_data
{
3739 struct ctdb_context
*ctdb
;
3743 static int cattdb_traverse(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA data
, void *private_data
)
3745 struct cattdb_data
*d
= private_data
;
3746 struct ctdb_dump_db_context c
;
3752 c
.printemptyrecords
= (bool)options
.printemptyrecords
;
3753 c
.printdatasize
= (bool)options
.printdatasize
;
3754 c
.printlmaster
= false;
3755 c
.printhash
= (bool)options
.printhash
;
3756 c
.printrecordflags
= true;
3758 return ctdb_dumpdb_record(d
->ctdb
, key
, data
, &c
);
3762 cat the local tdb database using same format as catdb
3764 static int control_cattdb(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3766 const char *db_name
;
3767 struct ctdb_db_context
*ctdb_db
;
3768 struct cattdb_data d
;
3775 if (!db_exists(ctdb
, argv
[0], NULL
, &db_name
, &flags
)) {
3779 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, flags
& CTDB_DB_FLAGS_PERSISTENT
, 0);
3780 if (ctdb_db
== NULL
) {
3781 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
3785 /* traverse the local tdb */
3788 if (tdb_traverse_read(ctdb_db
->ltdb
->tdb
, cattdb_traverse
, &d
) == -1) {
3789 printf("Failed to cattdb data\n");
3792 talloc_free(ctdb_db
);
3794 printf("Dumped %d records\n", d
.count
);
3799 display the content of a database key
3801 static int control_readkey(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3803 const char *db_name
;
3804 struct ctdb_db_context
*ctdb_db
;
3805 struct ctdb_record_handle
*h
;
3806 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3814 if (!db_exists(ctdb
, argv
[0], NULL
, &db_name
, &flags
)) {
3818 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, flags
& CTDB_DB_FLAGS_PERSISTENT
, 0);
3819 if (ctdb_db
== NULL
) {
3820 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
3824 key
.dptr
= discard_const(argv
[1]);
3825 key
.dsize
= strlen((char *)key
.dptr
);
3827 h
= ctdb_fetch_lock(ctdb_db
, tmp_ctx
, key
, &data
);
3829 printf("Failed to fetch record '%s' on node %d\n",
3830 (const char *)key
.dptr
, ctdb_get_pnn(ctdb
));
3831 talloc_free(tmp_ctx
);
3835 printf("Data: size:%d ptr:[%.*s]\n", (int)data
.dsize
, (int)data
.dsize
, data
.dptr
);
3837 talloc_free(ctdb_db
);
3838 talloc_free(tmp_ctx
);
3843 display the content of a database key
3845 static int control_writekey(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3847 const char *db_name
;
3848 struct ctdb_db_context
*ctdb_db
;
3849 struct ctdb_record_handle
*h
;
3850 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3858 if (!db_exists(ctdb
, argv
[0], NULL
, &db_name
, &flags
)) {
3862 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, flags
& CTDB_DB_FLAGS_PERSISTENT
, 0);
3863 if (ctdb_db
== NULL
) {
3864 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
3868 key
.dptr
= discard_const(argv
[1]);
3869 key
.dsize
= strlen((char *)key
.dptr
);
3871 h
= ctdb_fetch_lock(ctdb_db
, tmp_ctx
, key
, &data
);
3873 printf("Failed to fetch record '%s' on node %d\n",
3874 (const char *)key
.dptr
, ctdb_get_pnn(ctdb
));
3875 talloc_free(tmp_ctx
);
3879 data
.dptr
= discard_const(argv
[2]);
3880 data
.dsize
= strlen((char *)data
.dptr
);
3882 if (ctdb_record_store(h
, data
) != 0) {
3883 printf("Failed to store record\n");
3887 talloc_free(ctdb_db
);
3888 talloc_free(tmp_ctx
);
3893 fetch a record from a persistent database
3895 static int control_pfetch(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3897 const char *db_name
;
3898 struct ctdb_db_context
*ctdb_db
;
3899 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3900 struct ctdb_transaction_handle
*h
;
3907 talloc_free(tmp_ctx
);
3911 if (!db_exists(ctdb
, argv
[0], NULL
, &db_name
, &flags
)) {
3912 talloc_free(tmp_ctx
);
3916 persistent
= flags
& CTDB_DB_FLAGS_PERSISTENT
;
3918 DEBUG(DEBUG_ERR
,("Database '%s' is not persistent\n", db_name
));
3919 talloc_free(tmp_ctx
);
3923 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, persistent
, 0);
3924 if (ctdb_db
== NULL
) {
3925 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
3926 talloc_free(tmp_ctx
);
3930 h
= ctdb_transaction_start(ctdb_db
, tmp_ctx
);
3932 DEBUG(DEBUG_ERR
,("Failed to start transaction on database %s\n", db_name
));
3933 talloc_free(tmp_ctx
);
3937 key
.dptr
= discard_const(argv
[1]);
3938 key
.dsize
= strlen(argv
[1]);
3939 ret
= ctdb_transaction_fetch(h
, tmp_ctx
, key
, &data
);
3941 DEBUG(DEBUG_ERR
,("Failed to fetch record\n"));
3942 talloc_free(tmp_ctx
);
3946 if (data
.dsize
== 0 || data
.dptr
== NULL
) {
3947 DEBUG(DEBUG_ERR
,("Record is empty\n"));
3948 talloc_free(tmp_ctx
);
3953 fd
= open(argv
[2], O_WRONLY
|O_CREAT
|O_TRUNC
, 0600);
3955 DEBUG(DEBUG_ERR
,("Failed to open output file %s\n", argv
[2]));
3956 talloc_free(tmp_ctx
);
3959 write(fd
, data
.dptr
, data
.dsize
);
3962 write(1, data
.dptr
, data
.dsize
);
3965 /* abort the transaction */
3969 talloc_free(tmp_ctx
);
3974 fetch a record from a tdb-file
3976 static int control_tfetch(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3978 const char *tdb_file
;
3981 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
3990 tdb
= tdb_open(tdb_file
, 0, 0, O_RDONLY
, 0);
3992 printf("Failed to open TDB file %s\n", tdb_file
);
3996 if (!strncmp(argv
[1], "0x", 2)) {
3997 key
= hextodata(tmp_ctx
, argv
[1] + 2);
3998 if (key
.dsize
== 0) {
3999 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv
[1]);
4003 key
.dptr
= discard_const(argv
[1]);
4004 key
.dsize
= strlen(argv
[1]);
4007 data
= tdb_fetch(tdb
, key
);
4008 if (data
.dptr
== NULL
|| data
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
4009 printf("Failed to read record %s from tdb %s\n", argv
[1], tdb_file
);
4017 fd
= open(argv
[2], O_WRONLY
|O_CREAT
|O_TRUNC
, 0600);
4019 printf("Failed to open output file %s\n", argv
[2]);
4022 if (options
.verbose
){
4023 write(fd
, data
.dptr
, data
.dsize
);
4025 write(fd
, data
.dptr
+sizeof(struct ctdb_ltdb_header
), data
.dsize
-sizeof(struct ctdb_ltdb_header
));
4029 if (options
.verbose
){
4030 write(1, data
.dptr
, data
.dsize
);
4032 write(1, data
.dptr
+sizeof(struct ctdb_ltdb_header
), data
.dsize
-sizeof(struct ctdb_ltdb_header
));
4036 talloc_free(tmp_ctx
);
4041 store a record and header to a tdb-file
4043 static int control_tstore(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4045 const char *tdb_file
;
4047 TDB_DATA key
, value
, data
;
4048 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
4049 struct ctdb_ltdb_header header
;
4057 tdb
= tdb_open(tdb_file
, 0, 0, O_RDWR
, 0);
4059 printf("Failed to open TDB file %s\n", tdb_file
);
4063 if (!strncmp(argv
[1], "0x", 2)) {
4064 key
= hextodata(tmp_ctx
, argv
[1] + 2);
4065 if (key
.dsize
== 0) {
4066 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv
[1]);
4070 key
.dptr
= discard_const(argv
[1]);
4071 key
.dsize
= strlen(argv
[1]);
4074 if (!strncmp(argv
[2], "0x", 2)) {
4075 value
= hextodata(tmp_ctx
, argv
[2] + 2);
4076 if (value
.dsize
== 0) {
4077 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv
[2]);
4081 value
.dptr
= discard_const(argv
[2]);
4082 value
.dsize
= strlen(argv
[2]);
4085 ZERO_STRUCT(header
);
4087 header
.rsn
= atoll(argv
[3]);
4090 header
.dmaster
= atoi(argv
[4]);
4093 header
.flags
= atoi(argv
[5]);
4096 data
.dsize
= sizeof(struct ctdb_ltdb_header
) + value
.dsize
;
4097 data
.dptr
= talloc_size(tmp_ctx
, data
.dsize
);
4098 if (data
.dptr
== NULL
) {
4099 printf("Failed to allocate header+value\n");
4103 *(struct ctdb_ltdb_header
*)data
.dptr
= header
;
4104 memcpy(data
.dptr
+ sizeof(struct ctdb_ltdb_header
), value
.dptr
, value
.dsize
);
4106 if (tdb_store(tdb
, key
, data
, TDB_REPLACE
) != 0) {
4107 printf("Failed to write record %s to tdb %s\n", argv
[1], tdb_file
);
4114 talloc_free(tmp_ctx
);
4119 write a record to a persistent database
4121 static int control_pstore(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4123 const char *db_name
;
4124 struct ctdb_db_context
*ctdb_db
;
4125 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
4126 struct ctdb_transaction_handle
*h
;
4132 talloc_free(tmp_ctx
);
4136 fd
= open(argv
[2], O_RDONLY
);
4138 DEBUG(DEBUG_ERR
,("Failed to open file containing record data : %s %s\n", argv
[2], strerror(errno
)));
4139 talloc_free(tmp_ctx
);
4143 ret
= fstat(fd
, &st
);
4145 DEBUG(DEBUG_ERR
,("fstat of file %s failed: %s\n", argv
[2], strerror(errno
)));
4147 talloc_free(tmp_ctx
);
4151 if (!S_ISREG(st
.st_mode
)) {
4152 DEBUG(DEBUG_ERR
,("Not a regular file %s\n", argv
[2]));
4154 talloc_free(tmp_ctx
);
4158 data
.dsize
= st
.st_size
;
4159 if (data
.dsize
== 0) {
4162 data
.dptr
= talloc_size(tmp_ctx
, data
.dsize
);
4163 if (data
.dptr
== NULL
) {
4164 DEBUG(DEBUG_ERR
,("Failed to talloc %d of memory to store record data\n", (int)data
.dsize
));
4166 talloc_free(tmp_ctx
);
4169 ret
= read(fd
, data
.dptr
, data
.dsize
);
4170 if (ret
!= data
.dsize
) {
4171 DEBUG(DEBUG_ERR
,("Failed to read %d bytes of record data\n", (int)data
.dsize
));
4173 talloc_free(tmp_ctx
);
4182 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, true, 0);
4183 if (ctdb_db
== NULL
) {
4184 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
4185 talloc_free(tmp_ctx
);
4189 h
= ctdb_transaction_start(ctdb_db
, tmp_ctx
);
4191 DEBUG(DEBUG_ERR
,("Failed to start transaction on database %s\n", db_name
));
4192 talloc_free(tmp_ctx
);
4196 key
.dptr
= discard_const(argv
[1]);
4197 key
.dsize
= strlen(argv
[1]);
4198 ret
= ctdb_transaction_store(h
, key
, data
);
4200 DEBUG(DEBUG_ERR
,("Failed to store record\n"));
4201 talloc_free(tmp_ctx
);
4205 ret
= ctdb_transaction_commit(h
);
4207 DEBUG(DEBUG_ERR
,("Failed to commit transaction\n"));
4208 talloc_free(tmp_ctx
);
4213 talloc_free(tmp_ctx
);
4218 * delete a record from a persistent database
4220 static int control_pdelete(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4222 const char *db_name
;
4223 struct ctdb_db_context
*ctdb_db
;
4224 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
4225 struct ctdb_transaction_handle
*h
;
4232 talloc_free(tmp_ctx
);
4236 if (!db_exists(ctdb
, argv
[0], NULL
, &db_name
, &flags
)) {
4237 talloc_free(tmp_ctx
);
4241 persistent
= flags
& CTDB_DB_FLAGS_PERSISTENT
;
4243 DEBUG(DEBUG_ERR
, ("Database '%s' is not persistent\n", db_name
));
4244 talloc_free(tmp_ctx
);
4248 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, persistent
, 0);
4249 if (ctdb_db
== NULL
) {
4250 DEBUG(DEBUG_ERR
, ("Unable to attach to database '%s'\n", db_name
));
4251 talloc_free(tmp_ctx
);
4255 h
= ctdb_transaction_start(ctdb_db
, tmp_ctx
);
4257 DEBUG(DEBUG_ERR
, ("Failed to start transaction on database %s\n", db_name
));
4258 talloc_free(tmp_ctx
);
4262 key
.dptr
= discard_const(argv
[1]);
4263 key
.dsize
= strlen(argv
[1]);
4264 ret
= ctdb_transaction_store(h
, key
, tdb_null
);
4266 DEBUG(DEBUG_ERR
, ("Failed to delete record\n"));
4267 talloc_free(tmp_ctx
);
4271 ret
= ctdb_transaction_commit(h
);
4273 DEBUG(DEBUG_ERR
, ("Failed to commit transaction\n"));
4274 talloc_free(tmp_ctx
);
4278 talloc_free(tmp_ctx
);
4282 static const char *ptrans_parse_string(TALLOC_CTX
*mem_ctx
, const char *s
,
4287 const char *ret
; /* Next byte after successfully parsed value */
4289 /* Error, unless someone says otherwise */
4291 /* Indicates no value to parse */
4294 /* Skip whitespace */
4295 n
= strspn(s
, " \t");
4299 /* Quoted ASCII string - no wide characters! */
4301 n
= strcspn(t
, "\"");
4305 data
->dptr
= talloc_memdup(mem_ctx
, t
, n
);
4306 CTDB_NOMEM_ABORT(data
->dptr
);
4310 DEBUG(DEBUG_WARNING
,("Unmatched \" in input %s\n", s
));
4313 DEBUG(DEBUG_WARNING
,("Unsupported input format in %s\n", s
));
4319 static bool ptrans_get_key_value(TALLOC_CTX
*mem_ctx
, FILE *file
,
4320 TDB_DATA
*key
, TDB_DATA
*value
)
4322 char line
[1024]; /* FIXME: make this more flexible? */
4326 ptr
= fgets(line
, sizeof(line
), file
);
4333 t
= ptrans_parse_string(mem_ctx
, line
, key
);
4334 if (t
== NULL
|| key
->dptr
== NULL
) {
4335 /* Line Ignored but not EOF */
4340 t
= ptrans_parse_string(mem_ctx
, t
, value
);
4342 /* Line Ignored but not EOF */
4343 talloc_free(key
->dptr
);
4352 * Update a persistent database as per file/stdin
4354 static int control_ptrans(struct ctdb_context
*ctdb
,
4355 int argc
, const char **argv
)
4357 const char *db_name
;
4358 struct ctdb_db_context
*ctdb_db
;
4359 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
4360 struct ctdb_transaction_handle
*h
;
4361 TDB_DATA key
, value
;
4366 talloc_free(tmp_ctx
);
4372 file
= fopen(argv
[1], "r");
4374 DEBUG(DEBUG_ERR
,("Unable to open file for reading '%s'\n", argv
[1]));
4375 talloc_free(tmp_ctx
);
4382 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, true, 0);
4383 if (ctdb_db
== NULL
) {
4384 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
4388 h
= ctdb_transaction_start(ctdb_db
, tmp_ctx
);
4390 DEBUG(DEBUG_ERR
,("Failed to start transaction on database %s\n", db_name
));
4394 while (ptrans_get_key_value(tmp_ctx
, file
, &key
, &value
)) {
4395 if (key
.dsize
!= 0) {
4396 ret
= ctdb_transaction_store(h
, key
, value
);
4397 /* Minimise memory use */
4398 talloc_free(key
.dptr
);
4399 if (value
.dptr
!= NULL
) {
4400 talloc_free(value
.dptr
);
4403 DEBUG(DEBUG_ERR
,("Failed to store record\n"));
4404 ctdb_transaction_cancel(h
);
4410 ret
= ctdb_transaction_commit(h
);
4412 DEBUG(DEBUG_ERR
,("Failed to commit transaction\n"));
4416 if (file
!= stdin
) {
4419 talloc_free(tmp_ctx
);
4423 if (file
!= stdin
) {
4427 talloc_free(tmp_ctx
);
4432 check if a service is bound to a port or not
4434 static int control_chktcpport(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4439 struct sockaddr_in sin
;
4442 printf("Use: ctdb chktcport <port>\n");
4446 port
= atoi(argv
[0]);
4448 s
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
4450 printf("Failed to open local socket\n");
4454 v
= fcntl(s
, F_GETFL
, 0);
4455 if (v
== -1 || fcntl(s
, F_SETFL
, v
| O_NONBLOCK
) != 0) {
4456 printf("Unable to set socket non-blocking: %s\n", strerror(errno
));
4459 bzero(&sin
, sizeof(sin
));
4460 sin
.sin_family
= PF_INET
;
4461 sin
.sin_port
= htons(port
);
4462 ret
= bind(s
, (struct sockaddr
*)&sin
, sizeof(sin
));
4465 printf("Failed to bind to local socket: %d %s\n", errno
, strerror(errno
));
4474 static void log_handler(struct ctdb_context
*ctdb
, uint64_t srvid
,
4475 TDB_DATA data
, void *private_data
)
4477 DEBUG(DEBUG_ERR
,("Log data received\n"));
4478 if (data
.dsize
> 0) {
4479 printf("%s", data
.dptr
);
4486 display a list of log messages from the in memory ringbuffer
4488 static int control_getlog(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4492 struct ctdb_get_log_addr log_addr
;
4496 /* Process options */
4498 log_addr
.pnn
= ctdb_get_pnn(ctdb
);
4499 log_addr
.level
= DEBUG_NOTICE
;
4500 for (i
= 0; i
< argc
; i
++) {
4501 if (strcmp(argv
[i
], "recoverd") == 0) {
4502 main_daemon
= false;
4504 if (isalpha(argv
[i
][0]) || argv
[i
][0] == '-') {
4505 log_addr
.level
= get_debug_by_desc(argv
[i
]);
4507 log_addr
.level
= strtol(argv
[i
], NULL
, 0);
4512 /* Our message port is our PID */
4513 log_addr
.srvid
= getpid();
4515 data
.dptr
= (unsigned char *)&log_addr
;
4516 data
.dsize
= sizeof(log_addr
);
4518 DEBUG(DEBUG_ERR
, ("Pulling logs from node %u\n", options
.pnn
));
4520 ctdb_client_set_message_handler(ctdb
, log_addr
.srvid
, log_handler
, NULL
);
4523 DEBUG(DEBUG_ERR
,("Listen for response on %d\n", (int)log_addr
.srvid
));
4528 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
4530 ret
= ctdb_control(ctdb
, options
.pnn
, 0, CTDB_CONTROL_GET_LOG
,
4531 0, data
, tmp_ctx
, NULL
, &res
, NULL
, &errmsg
);
4532 if (ret
!= 0 || res
!= 0) {
4533 DEBUG(DEBUG_ERR
,("Failed to get logs - %s\n", errmsg
));
4534 talloc_free(tmp_ctx
);
4537 talloc_free(tmp_ctx
);
4539 ret
= ctdb_client_send_message(ctdb
, options
.pnn
,
4540 CTDB_SRVID_GETLOG
, data
);
4542 DEBUG(DEBUG_ERR
,("Failed to send getlog request message to %u\n", options
.pnn
));
4547 tv
= timeval_current();
4548 /* this loop will terminate when we have received the reply */
4549 while (timeval_elapsed(&tv
) < (double)options
.timelimit
) {
4550 event_loop_once(ctdb
->ev
);
4553 DEBUG(DEBUG_INFO
,("Timed out waiting for log data.\n"));
4559 clear the in memory log area
4561 static int control_clearlog(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4565 if (argc
== 0 || (argc
>= 1 && strcmp(argv
[0], "recoverd") != 0)) {
4566 /* "recoverd" not given - get logs from main daemon */
4569 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
4571 ret
= ctdb_control(ctdb
, options
.pnn
, 0, CTDB_CONTROL_CLEAR_LOG
,
4572 0, tdb_null
, tmp_ctx
, NULL
, &res
, NULL
, &errmsg
);
4573 if (ret
!= 0 || res
!= 0) {
4574 DEBUG(DEBUG_ERR
,("Failed to clear logs\n"));
4575 talloc_free(tmp_ctx
);
4579 talloc_free(tmp_ctx
);
4581 TDB_DATA data
; /* unused in recoverd... */
4584 ret
= ctdb_client_send_message(ctdb
, options
.pnn
, CTDB_SRVID_CLEARLOG
, data
);
4586 DEBUG(DEBUG_ERR
,("Failed to send clearlog request message to %u\n", options
.pnn
));
4594 /* Reload public IPs on a specified nodes */
4595 static int control_reloadips(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4597 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
4603 assert_single_node_only();
4609 /* Determine the nodes where IPs need to be reloaded */
4610 if (!parse_nodestring(ctdb
, tmp_ctx
, argc
== 1 ? argv
[0] : NULL
,
4611 options
.pnn
, true, &nodes
, &pnn_mode
)) {
4617 /* Disable takeover runs on all connected nodes. A reply
4618 * indicating success is needed from each node so all nodes
4619 * will need to be active. This will retry until maxruntime
4620 * is exceeded, hence no error handling.
4622 * A check could be added to not allow reloading of IPs when
4623 * there are disconnected nodes. However, this should
4624 * probably be left up to the administrator.
4626 timeout
= LONGTIMEOUT
;
4627 srvid_broadcast(ctdb
, CTDB_SRVID_DISABLE_TAKEOVER_RUNS
, &timeout
,
4628 "Disable takeover runs", true);
4630 /* Now tell all the desired nodes to reload their public IPs.
4631 * Keep trying this until it succeeds. This assumes all
4632 * failures are transient, which might not be true...
4634 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_RELOAD_PUBLIC_IPS
,
4635 nodes
, 0, LONGTIMELIMIT(),
4637 NULL
, NULL
, NULL
) != 0) {
4639 ("Unable to reload IPs on some nodes, try again.\n"));
4643 /* It isn't strictly necessary to wait until takeover runs are
4644 * re-enabled but doing so can't hurt.
4647 srvid_broadcast(ctdb
, CTDB_SRVID_DISABLE_TAKEOVER_RUNS
, &timeout
,
4648 "Enable takeover runs", true);
4654 talloc_free(tmp_ctx
);
4659 display a list of the databases on a remote ctdb
4661 static int control_getdbmap(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4664 struct ctdb_dbid_map
*dbmap
=NULL
;
4666 ret
= ctdb_ctrl_getdbmap(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &dbmap
);
4668 DEBUG(DEBUG_ERR
, ("Unable to get dbids from node %u\n", options
.pnn
));
4672 if(options
.machinereadable
){
4673 printf(":ID:Name:Path:Persistent:Sticky:Unhealthy:ReadOnly:\n");
4674 for(i
=0;i
<dbmap
->num
;i
++){
4682 ctdb_ctrl_getdbpath(ctdb
, TIMELIMIT(), options
.pnn
,
4683 dbmap
->dbs
[i
].dbid
, ctdb
, &path
);
4684 ctdb_ctrl_getdbname(ctdb
, TIMELIMIT(), options
.pnn
,
4685 dbmap
->dbs
[i
].dbid
, ctdb
, &name
);
4686 ctdb_ctrl_getdbhealth(ctdb
, TIMELIMIT(), options
.pnn
,
4687 dbmap
->dbs
[i
].dbid
, ctdb
, &health
);
4688 persistent
= dbmap
->dbs
[i
].flags
& CTDB_DB_FLAGS_PERSISTENT
;
4689 readonly
= dbmap
->dbs
[i
].flags
& CTDB_DB_FLAGS_READONLY
;
4690 sticky
= dbmap
->dbs
[i
].flags
& CTDB_DB_FLAGS_STICKY
;
4691 printf(":0x%08X:%s:%s:%d:%d:%d:%d:\n",
4692 dbmap
->dbs
[i
].dbid
, name
, path
,
4693 !!(persistent
), !!(sticky
),
4694 !!(health
), !!(readonly
));
4699 printf("Number of databases:%d\n", dbmap
->num
);
4700 for(i
=0;i
<dbmap
->num
;i
++){
4708 ctdb_ctrl_getdbpath(ctdb
, TIMELIMIT(), options
.pnn
, dbmap
->dbs
[i
].dbid
, ctdb
, &path
);
4709 ctdb_ctrl_getdbname(ctdb
, TIMELIMIT(), options
.pnn
, dbmap
->dbs
[i
].dbid
, ctdb
, &name
);
4710 ctdb_ctrl_getdbhealth(ctdb
, TIMELIMIT(), options
.pnn
, dbmap
->dbs
[i
].dbid
, ctdb
, &health
);
4711 persistent
= dbmap
->dbs
[i
].flags
& CTDB_DB_FLAGS_PERSISTENT
;
4712 readonly
= dbmap
->dbs
[i
].flags
& CTDB_DB_FLAGS_READONLY
;
4713 sticky
= dbmap
->dbs
[i
].flags
& CTDB_DB_FLAGS_STICKY
;
4714 printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
4715 dbmap
->dbs
[i
].dbid
, name
, path
,
4716 persistent
?" PERSISTENT":"",
4717 sticky
?" STICKY":"",
4718 readonly
?" READONLY":"",
4719 health
?" UNHEALTHY":"");
4726 display the status of a database on a remote ctdb
4728 static int control_getdbstatus(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4730 const char *db_name
;
4740 if (!db_exists(ctdb
, argv
[0], &db_id
, &db_name
, &flags
)) {
4744 ctdb_ctrl_getdbpath(ctdb
, TIMELIMIT(), options
.pnn
, db_id
, ctdb
, &path
);
4745 ctdb_ctrl_getdbhealth(ctdb
, TIMELIMIT(), options
.pnn
, db_id
, ctdb
, &health
);
4746 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
4747 db_id
, db_name
, path
,
4748 (flags
& CTDB_DB_FLAGS_PERSISTENT
? "yes" : "no"),
4749 (flags
& CTDB_DB_FLAGS_STICKY
? "yes" : "no"),
4750 (flags
& CTDB_DB_FLAGS_READONLY
? "yes" : "no"),
4751 (health
? health
: "OK"));
4757 check if the local node is recmaster or not
4758 it will return 1 if this node is the recmaster and 0 if it is not
4759 or if the local ctdb daemon could not be contacted
4761 static int control_isnotrecmaster(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4763 uint32_t mypnn
, recmaster
;
4766 assert_single_node_only();
4768 mypnn
= getpnn(ctdb
);
4770 ret
= ctdb_ctrl_getrecmaster(ctdb
, ctdb
, TIMELIMIT(), options
.pnn
, &recmaster
);
4772 printf("Failed to get the recmaster\n");
4776 if (recmaster
!= mypnn
) {
4777 printf("this node is not the recmaster\n");
4781 printf("this node is the recmaster\n");
4788 static int control_ping(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4791 struct timeval tv
= timeval_current();
4792 ret
= ctdb_ctrl_ping(ctdb
, options
.pnn
);
4794 printf("Unable to get ping response from node %u\n", options
.pnn
);
4797 printf("response from %u time=%.6f sec (%d clients)\n",
4798 options
.pnn
, timeval_elapsed(&tv
), ret
);
4805 get a node's runstate
4807 static int control_runstate(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4810 enum ctdb_runstate runstate
;
4812 ret
= ctdb_ctrl_get_runstate(ctdb
, TIMELIMIT(), options
.pnn
, &runstate
);
4814 printf("Unable to get runstate response from node %u\n",
4819 enum ctdb_runstate t
;
4821 for (i
=0; i
<argc
; i
++) {
4823 t
= runstate_from_string(argv
[i
]);
4824 if (t
== CTDB_RUNSTATE_UNKNOWN
) {
4825 printf("Invalid run state (%s)\n", argv
[i
]);
4829 if (t
== runstate
) {
4836 printf("CTDB not in required run state (got %s)\n",
4837 runstate_to_string((enum ctdb_runstate
)runstate
));
4842 printf("%s\n", runstate_to_string(runstate
));
4850 static int control_getvar(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4861 ret
= ctdb_ctrl_get_tunable(ctdb
, TIMELIMIT(), options
.pnn
, name
, &value
);
4863 DEBUG(DEBUG_ERR
, ("Unable to get tunable variable '%s'\n", name
));
4867 printf("%-23s = %u\n", name
, value
);
4874 static int control_setvar(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4885 value
= strtoul(argv
[1], NULL
, 0);
4887 ret
= ctdb_ctrl_set_tunable(ctdb
, TIMELIMIT(), options
.pnn
, name
, value
);
4889 DEBUG(DEBUG_ERR
, ("Unable to set tunable variable '%s'\n", name
));
4898 static int control_listvars(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4904 ret
= ctdb_ctrl_list_tunables(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &list
, &count
);
4906 DEBUG(DEBUG_ERR
, ("Unable to list tunable variables\n"));
4910 for (i
=0;i
<count
;i
++) {
4911 control_getvar(ctdb
, 1, &list
[i
]);
4920 display debug level on a node
4922 static int control_getdebug(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4927 ret
= ctdb_ctrl_get_debuglevel(ctdb
, options
.pnn
, &level
);
4929 DEBUG(DEBUG_ERR
, ("Unable to get debuglevel response from node %u\n", options
.pnn
));
4932 if (options
.machinereadable
){
4933 printf(":Name:Level:\n");
4934 printf(":%s:%d:\n",get_debug_by_level(level
),level
);
4936 printf("Node %u is at debug level %s (%d)\n", options
.pnn
, get_debug_by_level(level
), level
);
4943 display reclock file of a node
4945 static int control_getreclock(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4948 const char *reclock
;
4950 ret
= ctdb_ctrl_getreclock(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &reclock
);
4952 DEBUG(DEBUG_ERR
, ("Unable to get reclock file from node %u\n", options
.pnn
));
4955 if (options
.machinereadable
){
4956 if (reclock
!= NULL
) {
4957 printf("%s", reclock
);
4960 if (reclock
== NULL
) {
4961 printf("No reclock file used.\n");
4963 printf("Reclock file:%s\n", reclock
);
4971 set the reclock file of a node
4973 static int control_setreclock(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4976 const char *reclock
;
4980 } else if (argc
== 1) {
4986 ret
= ctdb_ctrl_setreclock(ctdb
, TIMELIMIT(), options
.pnn
, reclock
);
4988 DEBUG(DEBUG_ERR
, ("Unable to get reclock file from node %u\n", options
.pnn
));
4995 set the natgw state on/off
4997 static int control_setnatgwstate(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5000 uint32_t natgwstate
;
5006 if (!strcmp(argv
[0], "on")) {
5008 } else if (!strcmp(argv
[0], "off")) {
5014 ret
= ctdb_ctrl_setnatgwstate(ctdb
, TIMELIMIT(), options
.pnn
, natgwstate
);
5016 DEBUG(DEBUG_ERR
, ("Unable to set the natgw state for node %u\n", options
.pnn
));
5024 set the lmaster role on/off
5026 static int control_setlmasterrole(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5029 uint32_t lmasterrole
;
5035 if (!strcmp(argv
[0], "on")) {
5037 } else if (!strcmp(argv
[0], "off")) {
5043 ret
= ctdb_ctrl_setlmasterrole(ctdb
, TIMELIMIT(), options
.pnn
, lmasterrole
);
5045 DEBUG(DEBUG_ERR
, ("Unable to set the lmaster role for node %u\n", options
.pnn
));
5053 set the recmaster role on/off
5055 static int control_setrecmasterrole(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5058 uint32_t recmasterrole
;
5064 if (!strcmp(argv
[0], "on")) {
5066 } else if (!strcmp(argv
[0], "off")) {
5072 ret
= ctdb_ctrl_setrecmasterrole(ctdb
, TIMELIMIT(), options
.pnn
, recmasterrole
);
5074 DEBUG(DEBUG_ERR
, ("Unable to set the recmaster role for node %u\n", options
.pnn
));
5082 set debug level on a node or all nodes
5084 static int control_setdebug(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5090 printf("You must specify the debug level. Valid levels are:\n");
5091 for (i
=0; debug_levels
[i
].description
!= NULL
; i
++) {
5092 printf("%s (%d)\n", debug_levels
[i
].description
, debug_levels
[i
].level
);
5098 if (isalpha(argv
[0][0]) || argv
[0][0] == '-') {
5099 level
= get_debug_by_desc(argv
[0]);
5101 level
= strtol(argv
[0], NULL
, 0);
5104 for (i
=0; debug_levels
[i
].description
!= NULL
; i
++) {
5105 if (level
== debug_levels
[i
].level
) {
5109 if (debug_levels
[i
].description
== NULL
) {
5110 printf("Invalid debug level, must be one of\n");
5111 for (i
=0; debug_levels
[i
].description
!= NULL
; i
++) {
5112 printf("%s (%d)\n", debug_levels
[i
].description
, debug_levels
[i
].level
);
5117 ret
= ctdb_ctrl_set_debuglevel(ctdb
, options
.pnn
, level
);
5119 DEBUG(DEBUG_ERR
, ("Unable to set debug level on node %u\n", options
.pnn
));
5128 static int control_thaw(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5134 priority
= strtol(argv
[0], NULL
, 0);
5138 DEBUG(DEBUG_ERR
,("Thaw by priority %u\n", priority
));
5140 ret
= ctdb_ctrl_thaw_priority(ctdb
, TIMELIMIT(), options
.pnn
, priority
);
5142 DEBUG(DEBUG_ERR
, ("Unable to thaw node %u\n", options
.pnn
));
5149 attach to a database
5151 static int control_attach(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5153 const char *db_name
;
5154 struct ctdb_db_context
*ctdb_db
;
5155 bool persistent
= false;
5165 if (strcmp(argv
[1], "persistent") != 0) {
5171 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, persistent
, 0);
5172 if (ctdb_db
== NULL
) {
5173 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
5183 static int control_setdbprio(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5185 struct ctdb_db_priority db_prio
;
5192 db_prio
.db_id
= strtoul(argv
[0], NULL
, 0);
5193 db_prio
.priority
= strtoul(argv
[1], NULL
, 0);
5195 ret
= ctdb_ctrl_set_db_priority(ctdb
, TIMELIMIT(), options
.pnn
, &db_prio
);
5197 DEBUG(DEBUG_ERR
,("Unable to set db prio\n"));
5207 static int control_getdbprio(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5209 uint32_t db_id
, priority
;
5216 if (!db_exists(ctdb
, argv
[0], &db_id
, NULL
, NULL
)) {
5220 ret
= ctdb_ctrl_get_db_priority(ctdb
, TIMELIMIT(), options
.pnn
, db_id
, &priority
);
5222 DEBUG(DEBUG_ERR
,("Unable to get db prio\n"));
5226 DEBUG(DEBUG_ERR
,("Priority:%u\n", priority
));
5232 set the sticky records capability for a database
5234 static int control_setdbsticky(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5236 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5244 if (!db_exists(ctdb
, argv
[0], &db_id
, NULL
, NULL
)) {
5248 ret
= ctdb_ctrl_set_db_sticky(ctdb
, options
.pnn
, db_id
);
5250 DEBUG(DEBUG_ERR
,("Unable to set db to support sticky records\n"));
5251 talloc_free(tmp_ctx
);
5255 talloc_free(tmp_ctx
);
5260 set the readonly capability for a database
5262 static int control_setdbreadonly(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5264 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5272 if (!db_exists(ctdb
, argv
[0], &db_id
, NULL
, NULL
)) {
5276 ret
= ctdb_ctrl_set_db_readonly(ctdb
, options
.pnn
, db_id
);
5278 DEBUG(DEBUG_ERR
,("Unable to set db to support readonly\n"));
5279 talloc_free(tmp_ctx
);
5283 talloc_free(tmp_ctx
);
5290 static int control_getdbseqnum(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5300 if (!db_exists(ctdb
, argv
[0], &db_id
, NULL
, NULL
)) {
5304 ret
= ctdb_ctrl_getdbseqnum(ctdb
, TIMELIMIT(), options
.pnn
, db_id
, &seqnum
);
5306 DEBUG(DEBUG_ERR
, ("Unable to get seqnum from node."));
5310 printf("Sequence number:%lld\n", (long long)seqnum
);
5316 run an eventscript on a node
5318 static int control_eventscript(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5324 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5327 DEBUG(DEBUG_ERR
,("Invalid arguments\n"));
5331 data
.dptr
= (unsigned char *)discard_const(argv
[0]);
5332 data
.dsize
= strlen((char *)data
.dptr
) + 1;
5334 DEBUG(DEBUG_ERR
, ("Running eventscripts with arguments \"%s\" on node %u\n", data
.dptr
, options
.pnn
));
5336 ret
= ctdb_control(ctdb
, options
.pnn
, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS
,
5337 0, data
, tmp_ctx
, NULL
, &res
, NULL
, &errmsg
);
5338 if (ret
!= 0 || res
!= 0) {
5339 DEBUG(DEBUG_ERR
,("Failed to run eventscripts - %s\n", errmsg
));
5340 talloc_free(tmp_ctx
);
5343 talloc_free(tmp_ctx
);
5347 #define DB_VERSION 1
5348 #define MAX_DB_NAME 64
5349 struct db_file_header
{
5350 unsigned long version
;
5352 unsigned long persistent
;
5354 const char name
[MAX_DB_NAME
];
5357 struct backup_data
{
5358 struct ctdb_marshall_buffer
*records
;
5361 bool traverse_error
;
5364 static int backup_traverse(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA data
, void *private)
5366 struct backup_data
*bd
= talloc_get_type(private, struct backup_data
);
5367 struct ctdb_rec_data
*rec
;
5369 /* add the record */
5370 rec
= ctdb_marshall_record(bd
->records
, 0, key
, NULL
, data
);
5372 bd
->traverse_error
= true;
5373 DEBUG(DEBUG_ERR
,("Failed to marshall record\n"));
5376 bd
->records
= talloc_realloc_size(NULL
, bd
->records
, rec
->length
+ bd
->len
);
5377 if (bd
->records
== NULL
) {
5378 DEBUG(DEBUG_ERR
,("Failed to expand marshalling buffer\n"));
5379 bd
->traverse_error
= true;
5382 bd
->records
->count
++;
5383 memcpy(bd
->len
+(uint8_t *)bd
->records
, rec
, rec
->length
);
5384 bd
->len
+= rec
->length
;
5392 * backup a database to a file
5394 static int control_backupdb(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5396 const char *db_name
;
5398 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5399 struct db_file_header dbhdr
;
5400 struct ctdb_db_context
*ctdb_db
;
5401 struct backup_data
*bd
;
5404 const char *reason
= NULL
;
5408 assert_single_node_only();
5411 DEBUG(DEBUG_ERR
,("Invalid arguments\n"));
5415 if (!db_exists(ctdb
, argv
[0], &db_id
, &db_name
, &flags
)) {
5419 ret
= ctdb_ctrl_getdbhealth(ctdb
, TIMELIMIT(), options
.pnn
,
5420 db_id
, tmp_ctx
, &reason
);
5422 DEBUG(DEBUG_ERR
,("Unable to get dbhealth for database '%s'\n",
5424 talloc_free(tmp_ctx
);
5428 uint32_t allow_unhealthy
= 0;
5430 ctdb_ctrl_get_tunable(ctdb
, TIMELIMIT(), options
.pnn
,
5431 "AllowUnhealthyDBRead",
5434 if (allow_unhealthy
!= 1) {
5435 DEBUG(DEBUG_ERR
,("database '%s' is unhealthy: %s\n",
5438 DEBUG(DEBUG_ERR
,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
5440 talloc_free(tmp_ctx
);
5444 DEBUG(DEBUG_WARNING
,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
5446 DEBUG(DEBUG_WARNING
,("WARNING! allow backup of unhealthy database: "
5447 "tunnable AllowUnhealthyDBRead = %u\n",
5451 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, flags
& CTDB_DB_FLAGS_PERSISTENT
, 0);
5452 if (ctdb_db
== NULL
) {
5453 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", argv
[0]));
5454 talloc_free(tmp_ctx
);
5459 ret
= tdb_transaction_start(ctdb_db
->ltdb
->tdb
);
5461 DEBUG(DEBUG_ERR
,("Failed to start transaction\n"));
5462 talloc_free(tmp_ctx
);
5467 bd
= talloc_zero(tmp_ctx
, struct backup_data
);
5469 DEBUG(DEBUG_ERR
,("Failed to allocate backup_data\n"));
5470 talloc_free(tmp_ctx
);
5474 bd
->records
= talloc_zero(bd
, struct ctdb_marshall_buffer
);
5475 if (bd
->records
== NULL
) {
5476 DEBUG(DEBUG_ERR
,("Failed to allocate ctdb_marshall_buffer\n"));
5477 talloc_free(tmp_ctx
);
5481 bd
->len
= offsetof(struct ctdb_marshall_buffer
, data
);
5482 bd
->records
->db_id
= ctdb_db
->db_id
;
5483 /* traverse the database collecting all records */
5484 if (tdb_traverse_read(ctdb_db
->ltdb
->tdb
, backup_traverse
, bd
) == -1 ||
5485 bd
->traverse_error
) {
5486 DEBUG(DEBUG_ERR
,("Traverse error\n"));
5487 talloc_free(tmp_ctx
);
5491 tdb_transaction_cancel(ctdb_db
->ltdb
->tdb
);
5494 fh
= open(argv
[1], O_RDWR
|O_CREAT
, 0600);
5496 DEBUG(DEBUG_ERR
,("Failed to open file '%s'\n", argv
[1]));
5497 talloc_free(tmp_ctx
);
5502 dbhdr
.version
= DB_VERSION
;
5503 dbhdr
.timestamp
= time(NULL
);
5504 dbhdr
.persistent
= flags
& CTDB_DB_FLAGS_PERSISTENT
;
5505 dbhdr
.size
= bd
->len
;
5506 if (strlen(argv
[0]) >= MAX_DB_NAME
) {
5507 DEBUG(DEBUG_ERR
,("Too long dbname\n"));
5510 strncpy(discard_const(dbhdr
.name
), argv
[0], MAX_DB_NAME
-1);
5511 ret
= write(fh
, &dbhdr
, sizeof(dbhdr
));
5513 DEBUG(DEBUG_ERR
,("write failed: %s\n", strerror(errno
)));
5516 ret
= write(fh
, bd
->records
, bd
->len
);
5518 DEBUG(DEBUG_ERR
,("write failed: %s\n", strerror(errno
)));
5527 DEBUG(DEBUG_ERR
,("close failed: %s\n", strerror(errno
)));
5531 DEBUG(DEBUG_ERR
,("Database backed up to %s\n", argv
[1]));
5533 talloc_free(tmp_ctx
);
5538 * restore a database from a file
5540 static int control_restoredb(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5543 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5546 struct db_file_header dbhdr
;
5547 struct ctdb_db_context
*ctdb_db
;
5548 struct ctdb_node_map
*nodemap
=NULL
;
5549 struct ctdb_vnn_map
*vnnmap
=NULL
;
5551 struct ctdb_control_wipe_database w
;
5553 uint32_t generation
;
5558 assert_single_node_only();
5560 if (argc
< 1 || argc
> 2) {
5561 DEBUG(DEBUG_ERR
,("Invalid arguments\n"));
5565 fh
= open(argv
[0], O_RDONLY
);
5567 DEBUG(DEBUG_ERR
,("Failed to open file '%s'\n", argv
[0]));
5568 talloc_free(tmp_ctx
);
5572 read(fh
, &dbhdr
, sizeof(dbhdr
));
5573 if (dbhdr
.version
!= DB_VERSION
) {
5574 DEBUG(DEBUG_ERR
,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr
.version
, DB_VERSION
));
5576 talloc_free(tmp_ctx
);
5580 dbname
= discard_const(dbhdr
.name
);
5582 dbname
= discard_const(argv
[1]);
5585 outdata
.dsize
= dbhdr
.size
;
5586 outdata
.dptr
= talloc_size(tmp_ctx
, outdata
.dsize
);
5587 if (outdata
.dptr
== NULL
) {
5588 DEBUG(DEBUG_ERR
,("Failed to allocate data of size '%lu'\n", dbhdr
.size
));
5590 talloc_free(tmp_ctx
);
5593 read(fh
, outdata
.dptr
, outdata
.dsize
);
5596 tm
= localtime(&dbhdr
.timestamp
);
5597 strftime(tbuf
,sizeof(tbuf
)-1,"%Y/%m/%d %H:%M:%S", tm
);
5598 printf("Restoring database '%s' from backup @ %s\n",
5602 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), dbname
, dbhdr
.persistent
, 0);
5603 if (ctdb_db
== NULL
) {
5604 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", dbname
));
5605 talloc_free(tmp_ctx
);
5609 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &nodemap
);
5611 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
5612 talloc_free(tmp_ctx
);
5617 ret
= ctdb_ctrl_getvnnmap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &vnnmap
);
5619 DEBUG(DEBUG_ERR
, ("Unable to get vnnmap from node %u\n", options
.pnn
));
5620 talloc_free(tmp_ctx
);
5624 /* freeze all nodes */
5625 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5626 for (i
=1; i
<=NUM_DB_PRIORITIES
; i
++) {
5627 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_FREEZE
,
5633 DEBUG(DEBUG_ERR
, ("Unable to freeze nodes.\n"));
5634 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5635 talloc_free(tmp_ctx
);
5640 generation
= vnnmap
->generation
;
5641 data
.dptr
= (void *)&generation
;
5642 data
.dsize
= sizeof(generation
);
5644 /* start a cluster wide transaction */
5645 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5646 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_TRANSACTION_START
,
5648 TIMELIMIT(), false, data
,
5651 DEBUG(DEBUG_ERR
, ("Unable to start cluster wide transactions.\n"));
5656 w
.db_id
= ctdb_db
->db_id
;
5657 w
.transaction_id
= generation
;
5659 data
.dptr
= (void *)&w
;
5660 data
.dsize
= sizeof(w
);
5662 /* wipe all the remote databases. */
5663 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5664 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_WIPE_DATABASE
,
5666 TIMELIMIT(), false, data
,
5669 DEBUG(DEBUG_ERR
, ("Unable to wipe database.\n"));
5670 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5671 talloc_free(tmp_ctx
);
5675 /* push the database */
5676 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5677 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_PUSH_DB
,
5679 TIMELIMIT(), false, outdata
,
5682 DEBUG(DEBUG_ERR
, ("Failed to push database.\n"));
5683 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5684 talloc_free(tmp_ctx
);
5688 data
.dptr
= (void *)&ctdb_db
->db_id
;
5689 data
.dsize
= sizeof(ctdb_db
->db_id
);
5691 /* mark the database as healthy */
5692 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5693 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_DB_SET_HEALTHY
,
5695 TIMELIMIT(), false, data
,
5698 DEBUG(DEBUG_ERR
, ("Failed to mark database as healthy.\n"));
5699 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5700 talloc_free(tmp_ctx
);
5704 data
.dptr
= (void *)&generation
;
5705 data
.dsize
= sizeof(generation
);
5707 /* commit all the changes */
5708 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_TRANSACTION_COMMIT
,
5710 TIMELIMIT(), false, data
,
5713 DEBUG(DEBUG_ERR
, ("Unable to commit databases.\n"));
5714 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5715 talloc_free(tmp_ctx
);
5720 /* thaw all nodes */
5721 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5722 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_THAW
,
5728 DEBUG(DEBUG_ERR
, ("Unable to thaw nodes.\n"));
5729 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5730 talloc_free(tmp_ctx
);
5735 talloc_free(tmp_ctx
);
5740 * dump a database backup from a file
5742 static int control_dumpdbbackup(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5744 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5746 struct db_file_header dbhdr
;
5750 struct ctdb_rec_data
*rec
= NULL
;
5751 struct ctdb_marshall_buffer
*m
;
5752 struct ctdb_dump_db_context c
;
5754 assert_single_node_only();
5757 DEBUG(DEBUG_ERR
,("Invalid arguments\n"));
5761 fh
= open(argv
[0], O_RDONLY
);
5763 DEBUG(DEBUG_ERR
,("Failed to open file '%s'\n", argv
[0]));
5764 talloc_free(tmp_ctx
);
5768 read(fh
, &dbhdr
, sizeof(dbhdr
));
5769 if (dbhdr
.version
!= DB_VERSION
) {
5770 DEBUG(DEBUG_ERR
,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr
.version
, DB_VERSION
));
5772 talloc_free(tmp_ctx
);
5776 outdata
.dsize
= dbhdr
.size
;
5777 outdata
.dptr
= talloc_size(tmp_ctx
, outdata
.dsize
);
5778 if (outdata
.dptr
== NULL
) {
5779 DEBUG(DEBUG_ERR
,("Failed to allocate data of size '%lu'\n", dbhdr
.size
));
5781 talloc_free(tmp_ctx
);
5784 read(fh
, outdata
.dptr
, outdata
.dsize
);
5786 m
= (struct ctdb_marshall_buffer
*)outdata
.dptr
;
5788 tm
= localtime(&dbhdr
.timestamp
);
5789 strftime(tbuf
,sizeof(tbuf
)-1,"%Y/%m/%d %H:%M:%S", tm
);
5790 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
5791 dbhdr
.name
, m
->db_id
, tbuf
);
5795 c
.printemptyrecords
= (bool)options
.printemptyrecords
;
5796 c
.printdatasize
= (bool)options
.printdatasize
;
5797 c
.printlmaster
= false;
5798 c
.printhash
= (bool)options
.printhash
;
5799 c
.printrecordflags
= (bool)options
.printrecordflags
;
5801 for (i
=0; i
< m
->count
; i
++) {
5805 /* we do not want the header splitted, so we pass NULL*/
5806 rec
= ctdb_marshall_loop_next(m
, rec
, &reqid
,
5809 ctdb_dumpdb_record(ctdb
, key
, data
, &c
);
5812 printf("Dumped %d records\n", i
);
5813 talloc_free(tmp_ctx
);
5818 * wipe a database from a file
5820 static int control_wipedb(struct ctdb_context
*ctdb
, int argc
,
5823 const char *db_name
;
5825 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5827 struct ctdb_db_context
*ctdb_db
;
5828 struct ctdb_node_map
*nodemap
= NULL
;
5829 struct ctdb_vnn_map
*vnnmap
= NULL
;
5831 struct ctdb_control_wipe_database w
;
5833 uint32_t generation
;
5836 assert_single_node_only();
5839 DEBUG(DEBUG_ERR
,("Invalid arguments\n"));
5843 if (!db_exists(ctdb
, argv
[0], NULL
, &db_name
, &flags
)) {
5847 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, flags
& CTDB_DB_FLAGS_PERSISTENT
, 0);
5848 if (ctdb_db
== NULL
) {
5849 DEBUG(DEBUG_ERR
, ("Unable to attach to database '%s'\n",
5851 talloc_free(tmp_ctx
);
5855 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
,
5858 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n",
5860 talloc_free(tmp_ctx
);
5864 ret
= ctdb_ctrl_getvnnmap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
,
5867 DEBUG(DEBUG_ERR
, ("Unable to get vnnmap from node %u\n",
5869 talloc_free(tmp_ctx
);
5873 /* freeze all nodes */
5874 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5875 for (i
=1; i
<=NUM_DB_PRIORITIES
; i
++) {
5876 ret
= ctdb_client_async_control(ctdb
, CTDB_CONTROL_FREEZE
,
5883 DEBUG(DEBUG_ERR
, ("Unable to freeze nodes.\n"));
5884 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
,
5885 CTDB_RECOVERY_ACTIVE
);
5886 talloc_free(tmp_ctx
);
5891 generation
= vnnmap
->generation
;
5892 data
.dptr
= (void *)&generation
;
5893 data
.dsize
= sizeof(generation
);
5895 /* start a cluster wide transaction */
5896 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5897 ret
= ctdb_client_async_control(ctdb
, CTDB_CONTROL_TRANSACTION_START
,
5899 TIMELIMIT(), false, data
,
5903 DEBUG(DEBUG_ERR
, ("Unable to start cluster wide "
5904 "transactions.\n"));
5908 w
.db_id
= ctdb_db
->db_id
;
5909 w
.transaction_id
= generation
;
5911 data
.dptr
= (void *)&w
;
5912 data
.dsize
= sizeof(w
);
5914 /* wipe all the remote databases. */
5915 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5916 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_WIPE_DATABASE
,
5918 TIMELIMIT(), false, data
,
5921 DEBUG(DEBUG_ERR
, ("Unable to wipe database.\n"));
5922 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5923 talloc_free(tmp_ctx
);
5927 data
.dptr
= (void *)&ctdb_db
->db_id
;
5928 data
.dsize
= sizeof(ctdb_db
->db_id
);
5930 /* mark the database as healthy */
5931 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5932 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_DB_SET_HEALTHY
,
5934 TIMELIMIT(), false, data
,
5937 DEBUG(DEBUG_ERR
, ("Failed to mark database as healthy.\n"));
5938 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5939 talloc_free(tmp_ctx
);
5943 data
.dptr
= (void *)&generation
;
5944 data
.dsize
= sizeof(generation
);
5946 /* commit all the changes */
5947 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_TRANSACTION_COMMIT
,
5949 TIMELIMIT(), false, data
,
5952 DEBUG(DEBUG_ERR
, ("Unable to commit databases.\n"));
5953 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5954 talloc_free(tmp_ctx
);
5958 /* thaw all nodes */
5959 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5960 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_THAW
,
5966 DEBUG(DEBUG_ERR
, ("Unable to thaw nodes.\n"));
5967 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5968 talloc_free(tmp_ctx
);
5972 DEBUG(DEBUG_ERR
, ("Database wiped.\n"));
5974 talloc_free(tmp_ctx
);
5981 static int control_dumpmemory(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5987 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5988 ret
= ctdb_control(ctdb
, options
.pnn
, 0, CTDB_CONTROL_DUMP_MEMORY
,
5989 0, tdb_null
, tmp_ctx
, &data
, &res
, NULL
, &errmsg
);
5990 if (ret
!= 0 || res
!= 0) {
5991 DEBUG(DEBUG_ERR
,("Failed to dump memory - %s\n", errmsg
));
5992 talloc_free(tmp_ctx
);
5995 write(1, data
.dptr
, data
.dsize
);
5996 talloc_free(tmp_ctx
);
6001 handler for memory dumps
6003 static void mem_dump_handler(struct ctdb_context
*ctdb
, uint64_t srvid
,
6004 TDB_DATA data
, void *private_data
)
6006 write(1, data
.dptr
, data
.dsize
);
6011 dump memory usage on the recovery daemon
6013 static int control_rddumpmemory(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
6017 struct srvid_request rd
;
6019 rd
.pnn
= ctdb_get_pnn(ctdb
);
6020 rd
.srvid
= getpid();
6022 /* register a message port for receiveing the reply so that we
6023 can receive the reply
6025 ctdb_client_set_message_handler(ctdb
, rd
.srvid
, mem_dump_handler
, NULL
);
6028 data
.dptr
= (uint8_t *)&rd
;
6029 data
.dsize
= sizeof(rd
);
6031 ret
= ctdb_client_send_message(ctdb
, options
.pnn
, CTDB_SRVID_MEM_DUMP
, data
);
6033 DEBUG(DEBUG_ERR
,("Failed to send memdump request message to %u\n", options
.pnn
));
6037 /* this loop will terminate when we have received the reply */
6039 event_loop_once(ctdb
->ev
);
6046 send a message to a srvid
6048 static int control_msgsend(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
6050 unsigned long srvid
;
6058 srvid
= strtoul(argv
[0], NULL
, 0);
6060 data
.dptr
= (uint8_t *)discard_const(argv
[1]);
6061 data
.dsize
= strlen(argv
[1]);
6063 ret
= ctdb_client_send_message(ctdb
, CTDB_BROADCAST_CONNECTED
, srvid
, data
);
6065 DEBUG(DEBUG_ERR
,("Failed to send memdump request message to %u\n", options
.pnn
));
6073 handler for msglisten
6075 static void msglisten_handler(struct ctdb_context
*ctdb
, uint64_t srvid
,
6076 TDB_DATA data
, void *private_data
)
6080 printf("Message received: ");
6081 for (i
=0;i
<data
.dsize
;i
++) {
6082 printf("%c", data
.dptr
[i
]);
6088 listen for messages on a messageport
6090 static int control_msglisten(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
6096 /* register a message port and listen for messages
6098 ctdb_client_set_message_handler(ctdb
, srvid
, msglisten_handler
, NULL
);
6099 printf("Listening for messages on srvid:%d\n", (int)srvid
);
6102 event_loop_once(ctdb
->ev
);
6109 list all nodes in the cluster
6110 we parse the nodes file directly
6112 static int control_listnodes(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
6114 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
6115 struct pnn_node
*pnn_nodes
;
6116 struct pnn_node
*pnn_node
;
6118 assert_single_node_only();
6120 pnn_nodes
= read_nodes_file(mem_ctx
);
6121 if (pnn_nodes
== NULL
) {
6122 DEBUG(DEBUG_ERR
,("Failed to read nodes file\n"));
6123 talloc_free(mem_ctx
);
6127 for(pnn_node
=pnn_nodes
;pnn_node
;pnn_node
=pnn_node
->next
) {
6128 ctdb_sock_addr addr
;
6129 if (parse_ip(pnn_node
->addr
, NULL
, 63999, &addr
) == 0) {
6130 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s' in nodes file\n", pnn_node
->addr
));
6131 talloc_free(mem_ctx
);
6134 if (options
.machinereadable
){
6135 printf(":%d:%s:\n", pnn_node
->pnn
, pnn_node
->addr
);
6137 printf("%s\n", pnn_node
->addr
);
6140 talloc_free(mem_ctx
);
6146 reload the nodes file on the local node
6148 static int control_reload_nodes_file(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
6152 struct ctdb_node_map
*nodemap
=NULL
;
6154 assert_single_node_only();
6156 mypnn
= ctdb_get_pnn(ctdb
);
6158 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, ctdb
, &nodemap
);
6160 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from local node\n"));
6164 /* reload the nodes file on all remote nodes */
6165 for (i
=0;i
<nodemap
->num
;i
++) {
6166 if (nodemap
->nodes
[i
].pnn
== mypnn
) {
6169 DEBUG(DEBUG_NOTICE
, ("Reloading nodes file on node %u\n", nodemap
->nodes
[i
].pnn
));
6170 ret
= ctdb_ctrl_reload_nodes_file(ctdb
, TIMELIMIT(),
6171 nodemap
->nodes
[i
].pnn
);
6173 DEBUG(DEBUG_ERR
, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap
->nodes
[i
].pnn
));
6177 /* reload the nodes file on the local node */
6178 DEBUG(DEBUG_NOTICE
, ("Reloading nodes file on node %u\n", mypnn
));
6179 ret
= ctdb_ctrl_reload_nodes_file(ctdb
, TIMELIMIT(), mypnn
);
6181 DEBUG(DEBUG_ERR
, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn
));
6184 /* initiate a recovery */
6185 control_recover(ctdb
, argc
, argv
);
6191 static const struct {
6193 int (*fn
)(struct ctdb_context
*, int, const char **);
6195 bool without_daemon
; /* can be run without daemon running ? */
6198 } ctdb_commands
[] = {
6199 { "version", control_version
, true, true, "show version of ctdb" },
6200 { "status", control_status
, true, false, "show node status" },
6201 { "uptime", control_uptime
, true, false, "show node uptime" },
6202 { "ping", control_ping
, true, false, "ping all nodes" },
6203 { "runstate", control_runstate
, true, false, "get/check runstate of a node", "[setup|first_recovery|startup|running]" },
6204 { "getvar", control_getvar
, true, false, "get a tunable variable", "<name>"},
6205 { "setvar", control_setvar
, true, false, "set a tunable variable", "<name> <value>"},
6206 { "listvars", control_listvars
, true, false, "list tunable variables"},
6207 { "statistics", control_statistics
, false, false, "show statistics" },
6208 { "statisticsreset", control_statistics_reset
, true, false, "reset statistics"},
6209 { "stats", control_stats
, false, false, "show rolling statistics", "[number of history records]" },
6210 { "ip", control_ip
, false, false, "show which public ip's that ctdb manages" },
6211 { "ipinfo", control_ipinfo
, true, false, "show details about a public ip that ctdb manages", "<ip>" },
6212 { "ifaces", control_ifaces
, true, false, "show which interfaces that ctdb manages" },
6213 { "setifacelink", control_setifacelink
, true, false, "set interface link status", "<iface> <status>" },
6214 { "process-exists", control_process_exists
, true, false, "check if a process exists on a node", "<pid>"},
6215 { "getdbmap", control_getdbmap
, true, false, "show the database map" },
6216 { "getdbstatus", control_getdbstatus
, true, false, "show the status of a database", "<dbname|dbid>" },
6217 { "catdb", control_catdb
, true, false, "dump a ctdb database" , "<dbname|dbid>"},
6218 { "cattdb", control_cattdb
, true, false, "dump a local tdb database" , "<dbname|dbid>"},
6219 { "getmonmode", control_getmonmode
, true, false, "show monitoring mode" },
6220 { "getcapabilities", control_getcapabilities
, true, false, "show node capabilities" },
6221 { "pnn", control_pnn
, true, false, "show the pnn of the currnet node" },
6222 { "lvs", control_lvs
, true, false, "show lvs configuration" },
6223 { "lvsmaster", control_lvsmaster
, true, false, "show which node is the lvs master" },
6224 { "disablemonitor", control_disable_monmode
,true, false, "set monitoring mode to DISABLE" },
6225 { "enablemonitor", control_enable_monmode
, true, false, "set monitoring mode to ACTIVE" },
6226 { "setdebug", control_setdebug
, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
6227 { "getdebug", control_getdebug
, true, false, "get debug level" },
6228 { "getlog", control_getlog
, true, false, "get the log data from the in memory ringbuffer", "[<level>] [recoverd]" },
6229 { "clearlog", control_clearlog
, true, false, "clear the log data from the in memory ringbuffer", "[recoverd]" },
6230 { "attach", control_attach
, true, false, "attach to a database", "<dbname> [persistent]" },
6231 { "dumpmemory", control_dumpmemory
, true, false, "dump memory map to stdout" },
6232 { "rddumpmemory", control_rddumpmemory
, true, false, "dump memory map from the recovery daemon to stdout" },
6233 { "getpid", control_getpid
, true, false, "get ctdbd process ID" },
6234 { "disable", control_disable
, true, false, "disable a nodes public IP" },
6235 { "enable", control_enable
, true, false, "enable a nodes public IP" },
6236 { "stop", control_stop
, true, false, "stop a node" },
6237 { "continue", control_continue
, true, false, "re-start a stopped node" },
6238 { "ban", control_ban
, true, false, "ban a node from the cluster", "<bantime>"},
6239 { "unban", control_unban
, true, false, "unban a node" },
6240 { "showban", control_showban
, true, false, "show ban information"},
6241 { "shutdown", control_shutdown
, true, false, "shutdown ctdbd" },
6242 { "recover", control_recover
, true, false, "force recovery" },
6243 { "sync", control_ipreallocate
, false, false, "wait until ctdbd has synced all state changes" },
6244 { "ipreallocate", control_ipreallocate
, false, false, "force the recovery daemon to perform a ip reallocation procedure" },
6245 { "thaw", control_thaw
, true, false, "thaw databases", "[priority:1-3]" },
6246 { "isnotrecmaster", control_isnotrecmaster
, false, false, "check if the local node is recmaster or not" },
6247 { "killtcp", kill_tcp
, false, false, "kill a tcp connection.", "[<srcip:port> <dstip:port>]" },
6248 { "gratiousarp", control_gratious_arp
, false, false, "send a gratious arp", "<ip> <interface>" },
6249 { "tickle", tickle_tcp
, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
6250 { "gettickles", control_get_tickles
, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
6251 { "addtickle", control_add_tickle
, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
6253 { "deltickle", control_del_tickle
, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
6255 { "regsrvid", regsrvid
, false, false, "register a server id", "<pnn> <type> <id>" },
6256 { "unregsrvid", unregsrvid
, false, false, "unregister a server id", "<pnn> <type> <id>" },
6257 { "chksrvid", chksrvid
, false, false, "check if a server id exists", "<pnn> <type> <id>" },
6258 { "getsrvids", getsrvids
, false, false, "get a list of all server ids"},
6259 { "check_srvids", check_srvids
, false, false, "check if a srvid exists", "<id>+" },
6260 { "repack", ctdb_repack
, false, false, "repack all databases", "[max_freelist]"},
6261 { "listnodes", control_listnodes
, false, true, "list all nodes in the cluster"},
6262 { "reloadnodes", control_reload_nodes_file
, false, false, "reload the nodes file and restart the transport on all nodes"},
6263 { "moveip", control_moveip
, false, false, "move/failover an ip address to another node", "<ip> <node>"},
6264 { "rebalanceip", control_rebalanceip
, false, false, "release an ip from the node and let recd rebalance it", "<ip>"},
6265 { "addip", control_addip
, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
6266 { "delip", control_delip
, false, false, "delete an ip address from a node", "<ip>"},
6267 { "eventscript", control_eventscript
, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
6268 { "backupdb", control_backupdb
, false, false, "backup the database into a file.", "<dbname|dbid> <file>"},
6269 { "restoredb", control_restoredb
, false, false, "restore the database from a file.", "<file> [dbname]"},
6270 { "dumpdbbackup", control_dumpdbbackup
, false, true, "dump database backup from a file.", "<file>"},
6271 { "wipedb", control_wipedb
, false, false, "wipe the contents of a database.", "<dbname|dbid>"},
6272 { "recmaster", control_recmaster
, true, false, "show the pnn for the recovery master."},
6273 { "scriptstatus", control_scriptstatus
, true, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
6274 { "enablescript", control_enablescript
, true, false, "enable an eventscript", "<script>"},
6275 { "disablescript", control_disablescript
, true, false, "disable an eventscript", "<script>"},
6276 { "natgwlist", control_natgwlist
, true, false, "show the nodes belonging to this natgw configuration"},
6277 { "xpnn", control_xpnn
, false, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
6278 { "getreclock", control_getreclock
, true, false, "Show the reclock file of a node"},
6279 { "setreclock", control_setreclock
, true, false, "Set/clear the reclock file of a node", "[filename]"},
6280 { "setnatgwstate", control_setnatgwstate
, false, false, "Set NATGW state to on/off", "{on|off}"},
6281 { "setlmasterrole", control_setlmasterrole
, false, false, "Set LMASTER role to on/off", "{on|off}"},
6282 { "setrecmasterrole", control_setrecmasterrole
, false, false, "Set RECMASTER role to on/off", "{on|off}"},
6283 { "setdbprio", control_setdbprio
, false, false, "Set DB priority", "<dbname|dbid> <prio:1-3>"},
6284 { "getdbprio", control_getdbprio
, false, false, "Get DB priority", "<dbname|dbid>"},
6285 { "setdbreadonly", control_setdbreadonly
, false, false, "Set DB readonly capable", "<dbname|dbid>"},
6286 { "setdbsticky", control_setdbsticky
, false, false, "Set DB sticky-records capable", "<dbname|dbid>"},
6287 { "msglisten", control_msglisten
, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
6288 { "msgsend", control_msgsend
, false, false, "Send a message to srvid", "<srvid> <message>"},
6289 { "pfetch", control_pfetch
, false, false, "fetch a record from a persistent database", "<dbname|dbid> <key> [<file>]" },
6290 { "pstore", control_pstore
, false, false, "write a record to a persistent database", "<dbname|dbid> <key> <file containing record>" },
6291 { "pdelete", control_pdelete
, false, false, "delete a record from a persistent database", "<dbname|dbid> <key>" },
6292 { "ptrans", control_ptrans
, false, false, "update a persistent database (from stdin)", "<dbname|dbid>" },
6293 { "tfetch", control_tfetch
, false, true, "fetch a record from a [c]tdb-file [-v]", "<tdb-file> <key> [<file>]" },
6294 { "tstore", control_tstore
, false, true, "store a record (including ltdb header)", "<tdb-file> <key> <data> [<rsn> <dmaster> <flags>]" },
6295 { "readkey", control_readkey
, true, false, "read the content off a database key", "<dbname|dbid> <key>" },
6296 { "writekey", control_writekey
, true, false, "write to a database key", "<dbname|dbid> <key> <value>" },
6297 { "checktcpport", control_chktcpport
, false, true, "check if a service is bound to a specific tcp port or not", "<port>" },
6298 { "rebalancenode", control_rebalancenode
, false, false, "mark nodes as forced IP rebalancing targets", "[<pnn-list>]"},
6299 { "getdbseqnum", control_getdbseqnum
, false, false, "get the sequence number off a database", "<dbname|dbid>" },
6300 { "nodestatus", control_nodestatus
, true, false, "show and return node status", "[<pnn-list>]" },
6301 { "dbstatistics", control_dbstatistics
, false, false, "show db statistics", "<dbname|dbid>" },
6302 { "reloadips", control_reloadips
, false, false, "reload the public addresses file on specified nodes" , "[<pnn-list>]" },
6303 { "ipiface", control_ipiface
, false, true, "Find which interface an ip address is hosted on", "<ip>" },
6309 static void usage(void)
6313 "Usage: ctdb [options] <control>\n" \
6315 " -n <node> choose node number, or 'all' (defaults to local node)\n"
6316 " -Y generate machinereadable output\n"
6317 " -v generate verbose output\n"
6318 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options
.timelimit
);
6319 printf("Controls:\n");
6320 for (i
=0;i
<ARRAY_SIZE(ctdb_commands
);i
++) {
6321 printf(" %-15s %-27s %s\n",
6322 ctdb_commands
[i
].name
,
6323 ctdb_commands
[i
].args
?ctdb_commands
[i
].args
:"",
6324 ctdb_commands
[i
].msg
);
6330 static void ctdb_alarm(int sig
)
6332 printf("Maximum runtime exceeded - exiting\n");
6339 int main(int argc
, const char *argv
[])
6341 struct ctdb_context
*ctdb
;
6342 char *nodestring
= NULL
;
6343 struct poptOption popt_options
[] = {
6346 { "timelimit", 't', POPT_ARG_INT
, &options
.timelimit
, 0, "timelimit", "integer" },
6347 { "node", 'n', POPT_ARG_STRING
, &nodestring
, 0, "node", "integer|all" },
6348 { "machinereadable", 'Y', POPT_ARG_NONE
, &options
.machinereadable
, 0, "enable machinereadable output", NULL
},
6349 { "verbose", 'v', POPT_ARG_NONE
, &options
.verbose
, 0, "enable verbose output", NULL
},
6350 { "maxruntime", 'T', POPT_ARG_INT
, &options
.maxruntime
, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
6351 { "print-emptyrecords", 0, POPT_ARG_NONE
, &options
.printemptyrecords
, 0, "print the empty records when dumping databases (catdb, cattdb, dumpdbbackup)", NULL
},
6352 { "print-datasize", 0, POPT_ARG_NONE
, &options
.printdatasize
, 0, "do not print record data when dumping databases, only the data size", NULL
},
6353 { "print-lmaster", 0, POPT_ARG_NONE
, &options
.printlmaster
, 0, "print the record's lmaster in catdb", NULL
},
6354 { "print-hash", 0, POPT_ARG_NONE
, &options
.printhash
, 0, "print the record's hash when dumping databases", NULL
},
6355 { "print-recordflags", 0, POPT_ARG_NONE
, &options
.printrecordflags
, 0, "print the record flags in catdb and dumpdbbackup", NULL
},
6359 const char **extra_argv
;
6363 struct event_context
*ev
;
6364 const char *control
;
6368 /* set some defaults */
6369 options
.maxruntime
= 0;
6370 options
.timelimit
= 10;
6371 options
.pnn
= CTDB_CURRENT_NODE
;
6373 pc
= poptGetContext(argv
[0], argc
, argv
, popt_options
, POPT_CONTEXT_KEEP_FIRST
);
6375 while ((opt
= poptGetNextOpt(pc
)) != -1) {
6378 DEBUG(DEBUG_ERR
, ("Invalid option %s: %s\n",
6379 poptBadOption(pc
, 0), poptStrerror(opt
)));
6384 /* setup the remaining options for the main program to use */
6385 extra_argv
= poptGetArgs(pc
);
6388 while (extra_argv
[extra_argc
]) extra_argc
++;
6391 if (extra_argc
< 1) {
6395 if (options
.maxruntime
== 0) {
6396 const char *ctdb_timeout
;
6397 ctdb_timeout
= getenv("CTDB_TIMEOUT");
6398 if (ctdb_timeout
!= NULL
) {
6399 options
.maxruntime
= strtoul(ctdb_timeout
, NULL
, 0);
6401 /* default timeout is 120 seconds */
6402 options
.maxruntime
= 120;
6406 signal(SIGALRM
, ctdb_alarm
);
6407 alarm(options
.maxruntime
);
6409 control
= extra_argv
[0];
6411 /* Default value for CTDB_BASE - don't override */
6412 setenv("CTDB_BASE", ETCDIR
"/ctdb", 0);
6414 ev
= event_context_init(NULL
);
6416 DEBUG(DEBUG_ERR
, ("Failed to initialize event system\n"));
6420 for (i
=0;i
<ARRAY_SIZE(ctdb_commands
);i
++) {
6421 if (strcmp(control
, ctdb_commands
[i
].name
) == 0) {
6426 if (i
== ARRAY_SIZE(ctdb_commands
)) {
6427 DEBUG(DEBUG_ERR
, ("Unknown control '%s'\n", control
));
6431 if (ctdb_commands
[i
].without_daemon
== true) {
6432 if (nodestring
!= NULL
) {
6433 DEBUG(DEBUG_ERR
, ("Can't specify node(s) with \"ctdb %s\"\n", control
));
6437 return ctdb_commands
[i
].fn(NULL
, extra_argc
-1, extra_argv
+1);
6440 /* initialise ctdb */
6441 ctdb
= ctdb_cmdline_client(ev
, TIMELIMIT());
6444 DEBUG(DEBUG_ERR
, ("Failed to init ctdb\n"));
6448 /* setup the node number(s) to contact */
6449 if (!parse_nodestring(ctdb
, ctdb
, nodestring
, CTDB_CURRENT_NODE
, false,
6450 &options
.nodes
, &options
.pnn
)) {
6454 if (options
.pnn
== CTDB_CURRENT_NODE
) {
6455 options
.pnn
= options
.nodes
[0];
6458 if (ctdb_commands
[i
].auto_all
&&
6459 ((options
.pnn
== CTDB_BROADCAST_ALL
) ||
6460 (options
.pnn
== CTDB_MULTICAST
))) {
6464 for (j
= 0; j
< talloc_array_length(options
.nodes
); j
++) {
6465 options
.pnn
= options
.nodes
[j
];
6466 ret
|= ctdb_commands
[i
].fn(ctdb
, extra_argc
-1, extra_argv
+1);
6469 ret
= ctdb_commands
[i
].fn(ctdb
, extra_argc
-1, extra_argv
+1);
6474 (void)poptFreeContext(pc
);