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) strcat(flags_str
, "|");
124 (void) strcat(flags_str
, flag_names
[j
].name
);
128 if (flags_str
[0] == '\0') {
129 (void) strcpy(flags_str
, "OK");
135 static int h2i(char h
)
137 if (h
>= 'a' && h
<= 'f') return h
- 'a' + 10;
138 if (h
>= 'A' && h
<= 'F') return h
- 'f' + 10;
142 static TDB_DATA
hextodata(TALLOC_CTX
*mem_ctx
, const char *str
)
145 TDB_DATA key
= {NULL
, 0};
149 DEBUG(DEBUG_ERR
,("Key specified with odd number of hexadecimal digits\n"));
154 key
.dptr
= talloc_size(mem_ctx
, key
.dsize
);
156 for (i
=0; i
< len
/2; i
++) {
157 key
.dptr
[i
] = h2i(str
[i
*2]) << 4 | h2i(str
[i
*2+1]);
162 /* Parse a nodestring. Parameter dd_ok controls what happens to nodes
163 * that are disconnected or deleted. If dd_ok is true those nodes are
164 * included in the output list of nodes. If dd_ok is false, those
165 * nodes are filtered from the "all" case and cause an error if
166 * explicitly specified.
168 static bool parse_nodestring(struct ctdb_context
*ctdb
,
170 const char * nodestring
,
171 uint32_t current_pnn
,
176 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
179 struct ctdb_node_map
*nodemap
;
184 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, tmp_ctx
, &nodemap
);
186 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from local node\n"));
187 talloc_free(tmp_ctx
);
191 if (nodestring
!= NULL
) {
192 *nodes
= talloc_array(mem_ctx
, uint32_t, 0);
193 if (*nodes
== NULL
) {
199 if (strcmp(nodestring
, "all") == 0) {
200 *pnn_mode
= CTDB_BROADCAST_ALL
;
203 for (i
= 0; i
< nodemap
->num
; i
++) {
204 if ((nodemap
->nodes
[i
].flags
&
205 (NODE_FLAGS_DISCONNECTED
|
206 NODE_FLAGS_DELETED
)) && !dd_ok
) {
209 *nodes
= talloc_realloc(mem_ctx
, *nodes
,
211 if (*nodes
== NULL
) {
221 ns
= talloc_strdup(tmp_ctx
, nodestring
);
222 tok
= strtok(ns
, ",");
223 while (tok
!= NULL
) {
225 i
= (uint32_t)strtoul(tok
, NULL
, 0);
226 if (i
>= nodemap
->num
) {
227 DEBUG(DEBUG_ERR
, ("Node %u does not exist\n", i
));
228 talloc_free(tmp_ctx
);
231 if ((nodemap
->nodes
[i
].flags
&
232 (NODE_FLAGS_DISCONNECTED
|
233 NODE_FLAGS_DELETED
)) && !dd_ok
) {
234 DEBUG(DEBUG_ERR
, ("Node %u has status %s\n", i
, pretty_print_flags(nodemap
->nodes
[i
].flags
)));
235 talloc_free(tmp_ctx
);
238 if ((pnn
= ctdb_ctrl_getpnn(ctdb
, TIMELIMIT(), i
)) < 0) {
239 DEBUG(DEBUG_ERR
, ("Can not access node %u. Node is not operational.\n", i
));
240 talloc_free(tmp_ctx
);
244 *nodes
= talloc_realloc(mem_ctx
, *nodes
,
246 if (*nodes
== NULL
) {
253 tok
= strtok(NULL
, ",");
258 *pnn_mode
= (*nodes
)[0];
260 *pnn_mode
= CTDB_MULTICAST
;
264 /* default - no nodes specified */
265 *nodes
= talloc_array(mem_ctx
, uint32_t, 1);
266 if (*nodes
== NULL
) {
269 *pnn_mode
= CTDB_CURRENT_NODE
;
271 if (((*nodes
)[0] = ctdb_ctrl_getpnn(ctdb
, TIMELIMIT(), current_pnn
)) < 0) {
276 talloc_free(tmp_ctx
);
280 talloc_free(tmp_ctx
);
285 check if a database exists
287 static bool db_exists(struct ctdb_context
*ctdb
, const char *dbarg
, uint32_t *dbid
, uint8_t *flags
)
290 struct ctdb_dbid_map
*dbmap
=NULL
;
291 bool dbid_given
= false, found
= false;
293 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
295 ret
= ctdb_ctrl_getdbmap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &dbmap
);
297 DEBUG(DEBUG_ERR
, ("Unable to get dbids from node %u\n", options
.pnn
));
301 if (strncmp(dbarg
, "0x", 2) == 0) {
302 id
= strtoul(dbarg
, NULL
, 0);
306 for(i
=0; i
<dbmap
->num
; i
++) {
308 if (id
== dbmap
->dbs
[i
].dbid
) {
314 ret
= ctdb_ctrl_getdbname(ctdb
, TIMELIMIT(), options
.pnn
, dbmap
->dbs
[i
].dbid
, tmp_ctx
, &name
);
316 DEBUG(DEBUG_ERR
, ("Unable to get dbname from dbid %u\n", dbmap
->dbs
[i
].dbid
));
320 if (strcmp(name
, dbarg
) == 0) {
321 id
= dbmap
->dbs
[i
].dbid
;
329 if (dbid
) *dbid
= id
;
330 if (flags
) *flags
= dbmap
->dbs
[i
].flags
;
332 DEBUG(DEBUG_ERR
,("No database matching '%s' found\n", dbarg
));
336 talloc_free(tmp_ctx
);
341 see if a process exists
343 static int control_process_exists(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
351 if (sscanf(argv
[0], "%u:%u", &pnn
, &pid
) != 2) {
352 DEBUG(DEBUG_ERR
, ("Badly formed pnn:pid\n"));
356 ret
= ctdb_ctrl_process_exists(ctdb
, pnn
, pid
);
358 printf("%u:%u exists\n", pnn
, pid
);
360 printf("%u:%u does not exist\n", pnn
, pid
);
366 display statistics structure
368 static void show_statistics(struct ctdb_statistics
*s
, int show_header
)
370 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
372 const char *prefix
=NULL
;
374 int tmp
, days
, hours
, minutes
, seconds
;
379 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
380 STATISTICS_FIELD(num_clients
),
381 STATISTICS_FIELD(frozen
),
382 STATISTICS_FIELD(recovering
),
383 STATISTICS_FIELD(num_recoveries
),
384 STATISTICS_FIELD(client_packets_sent
),
385 STATISTICS_FIELD(client_packets_recv
),
386 STATISTICS_FIELD(node_packets_sent
),
387 STATISTICS_FIELD(node_packets_recv
),
388 STATISTICS_FIELD(keepalive_packets_sent
),
389 STATISTICS_FIELD(keepalive_packets_recv
),
390 STATISTICS_FIELD(node
.req_call
),
391 STATISTICS_FIELD(node
.reply_call
),
392 STATISTICS_FIELD(node
.req_dmaster
),
393 STATISTICS_FIELD(node
.reply_dmaster
),
394 STATISTICS_FIELD(node
.reply_error
),
395 STATISTICS_FIELD(node
.req_message
),
396 STATISTICS_FIELD(node
.req_control
),
397 STATISTICS_FIELD(node
.reply_control
),
398 STATISTICS_FIELD(client
.req_call
),
399 STATISTICS_FIELD(client
.req_message
),
400 STATISTICS_FIELD(client
.req_control
),
401 STATISTICS_FIELD(timeouts
.call
),
402 STATISTICS_FIELD(timeouts
.control
),
403 STATISTICS_FIELD(timeouts
.traverse
),
404 STATISTICS_FIELD(locks
.num_calls
),
405 STATISTICS_FIELD(locks
.num_current
),
406 STATISTICS_FIELD(locks
.num_pending
),
407 STATISTICS_FIELD(locks
.num_failed
),
408 STATISTICS_FIELD(total_calls
),
409 STATISTICS_FIELD(pending_calls
),
410 STATISTICS_FIELD(childwrite_calls
),
411 STATISTICS_FIELD(pending_childwrite_calls
),
412 STATISTICS_FIELD(memory_used
),
413 STATISTICS_FIELD(max_hop_count
),
414 STATISTICS_FIELD(total_ro_delegations
),
415 STATISTICS_FIELD(total_ro_revokes
),
418 tmp
= s
->statistics_current_time
.tv_sec
- s
->statistics_start_time
.tv_sec
;
427 if (options
.machinereadable
){
429 printf("CTDB version:");
430 printf("Current time of statistics:");
431 printf("Statistics collected since:");
432 for (i
=0;i
<ARRAY_SIZE(fields
);i
++) {
433 printf("%s:", fields
[i
].name
);
435 printf("num_reclock_ctdbd_latency:");
436 printf("min_reclock_ctdbd_latency:");
437 printf("avg_reclock_ctdbd_latency:");
438 printf("max_reclock_ctdbd_latency:");
440 printf("num_reclock_recd_latency:");
441 printf("min_reclock_recd_latency:");
442 printf("avg_reclock_recd_latency:");
443 printf("max_reclock_recd_latency:");
445 printf("num_call_latency:");
446 printf("min_call_latency:");
447 printf("avg_call_latency:");
448 printf("max_call_latency:");
450 printf("num_lockwait_latency:");
451 printf("min_lockwait_latency:");
452 printf("avg_lockwait_latency:");
453 printf("max_lockwait_latency:");
455 printf("num_childwrite_latency:");
456 printf("min_childwrite_latency:");
457 printf("avg_childwrite_latency:");
458 printf("max_childwrite_latency:");
461 printf("%d:", CTDB_VERSION
);
462 printf("%d:", (int)s
->statistics_current_time
.tv_sec
);
463 printf("%d:", (int)s
->statistics_start_time
.tv_sec
);
464 for (i
=0;i
<ARRAY_SIZE(fields
);i
++) {
465 printf("%d:", *(uint32_t *)(fields
[i
].offset
+(uint8_t *)s
));
467 printf("%d:", s
->reclock
.ctdbd
.num
);
468 printf("%.6f:", s
->reclock
.ctdbd
.min
);
469 printf("%.6f:", s
->reclock
.ctdbd
.num
?s
->reclock
.ctdbd
.total
/s
->reclock
.ctdbd
.num
:0.0);
470 printf("%.6f:", s
->reclock
.ctdbd
.max
);
472 printf("%d:", s
->reclock
.recd
.num
);
473 printf("%.6f:", s
->reclock
.recd
.min
);
474 printf("%.6f:", s
->reclock
.recd
.num
?s
->reclock
.recd
.total
/s
->reclock
.recd
.num
:0.0);
475 printf("%.6f:", s
->reclock
.recd
.max
);
477 printf("%d:", s
->call_latency
.num
);
478 printf("%.6f:", s
->call_latency
.min
);
479 printf("%.6f:", s
->call_latency
.num
?s
->call_latency
.total
/s
->call_latency
.num
:0.0);
480 printf("%.6f:", s
->call_latency
.max
);
482 printf("%d:", s
->childwrite_latency
.num
);
483 printf("%.6f:", s
->childwrite_latency
.min
);
484 printf("%.6f:", s
->childwrite_latency
.num
?s
->childwrite_latency
.total
/s
->childwrite_latency
.num
:0.0);
485 printf("%.6f:", s
->childwrite_latency
.max
);
488 printf("CTDB version %u\n", CTDB_VERSION
);
489 printf("Current time of statistics : %s", ctime(&s
->statistics_current_time
.tv_sec
));
490 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s", days
, hours
, minutes
, seconds
, ctime(&s
->statistics_start_time
.tv_sec
));
492 for (i
=0;i
<ARRAY_SIZE(fields
);i
++) {
493 if (strchr(fields
[i
].name
, '.')) {
494 preflen
= strcspn(fields
[i
].name
, ".")+1;
495 if (!prefix
|| strncmp(prefix
, fields
[i
].name
, preflen
) != 0) {
496 prefix
= fields
[i
].name
;
497 printf(" %*.*s\n", preflen
-1, preflen
-1, fields
[i
].name
);
502 printf(" %*s%-22s%*s%10u\n",
504 fields
[i
].name
+preflen
,
506 *(uint32_t *)(fields
[i
].offset
+(uint8_t *)s
));
508 printf(" hop_count_buckets:");
509 for (i
=0;i
<MAX_COUNT_BUCKETS
;i
++) {
510 printf(" %d", s
->hop_count_bucket
[i
]);
513 printf(" lock_buckets:");
514 for (i
=0; i
<MAX_COUNT_BUCKETS
; i
++) {
515 printf(" %d", s
->locks
.buckets
[i
]);
518 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
);
520 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
);
522 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
);
524 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
);
525 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
);
528 talloc_free(tmp_ctx
);
532 display remote ctdb statistics combined from all nodes
534 static int control_statistics_all(struct ctdb_context
*ctdb
)
537 struct ctdb_statistics statistics
;
541 nodes
= ctdb_get_connected_nodes(ctdb
, TIMELIMIT(), ctdb
, &num_nodes
);
542 CTDB_NO_MEMORY(ctdb
, nodes
);
544 ZERO_STRUCT(statistics
);
546 for (i
=0;i
<num_nodes
;i
++) {
547 struct ctdb_statistics s1
;
549 uint32_t *v1
= (uint32_t *)&s1
;
550 uint32_t *v2
= (uint32_t *)&statistics
;
552 offsetof(struct ctdb_statistics
, __last_counter
) / sizeof(uint32_t);
553 ret
= ctdb_ctrl_statistics(ctdb
, nodes
[i
], &s1
);
555 DEBUG(DEBUG_ERR
, ("Unable to get statistics from node %u\n", nodes
[i
]));
558 for (j
=0;j
<num_ints
;j
++) {
561 statistics
.max_hop_count
=
562 MAX(statistics
.max_hop_count
, s1
.max_hop_count
);
563 statistics
.call_latency
.max
=
564 MAX(statistics
.call_latency
.max
, s1
.call_latency
.max
);
567 printf("Gathered statistics for %u nodes\n", num_nodes
);
568 show_statistics(&statistics
, 1);
573 display remote ctdb statistics
575 static int control_statistics(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
578 struct ctdb_statistics statistics
;
580 if (options
.pnn
== CTDB_BROADCAST_ALL
) {
581 return control_statistics_all(ctdb
);
584 ret
= ctdb_ctrl_statistics(ctdb
, options
.pnn
, &statistics
);
586 DEBUG(DEBUG_ERR
, ("Unable to get statistics from node %u\n", options
.pnn
));
589 show_statistics(&statistics
, 1);
595 reset remote ctdb statistics
597 static int control_statistics_reset(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
601 ret
= ctdb_statistics_reset(ctdb
, options
.pnn
);
603 DEBUG(DEBUG_ERR
, ("Unable to reset statistics on node %u\n", options
.pnn
));
611 display remote ctdb rolling statistics
613 static int control_stats(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
616 struct ctdb_statistics_wire
*stats
;
617 int i
, num_records
= -1;
619 assert_single_node_only();
622 num_records
= atoi(argv
[0]) - 1;
625 ret
= ctdb_ctrl_getstathistory(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &stats
);
627 DEBUG(DEBUG_ERR
, ("Unable to get rolling statistics from node %u\n", options
.pnn
));
630 for (i
=0;i
<stats
->num
;i
++) {
631 if (stats
->stats
[i
].statistics_start_time
.tv_sec
== 0) {
634 show_statistics(&stats
->stats
[i
], i
==0);
635 if (i
== num_records
) {
644 display remote ctdb db statistics
646 static int control_dbstatistics(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
648 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
649 struct ctdb_db_statistics
*dbstat
;
659 if (!db_exists(ctdb
, argv
[0], &db_id
, NULL
)) {
663 ret
= ctdb_ctrl_dbstatistics(ctdb
, options
.pnn
, db_id
, tmp_ctx
, &dbstat
);
665 DEBUG(DEBUG_ERR
,("Failed to read db statistics from node\n"));
666 talloc_free(tmp_ctx
);
670 printf("DB Statistics: %s\n", argv
[0]);
671 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_delegations", 4, "",
672 dbstat
->db_ro_delegations
);
673 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_revokes", 4, "",
674 dbstat
->db_ro_delegations
);
675 printf(" %s\n", "locks");
676 printf(" %*s%-22s%*s%10u\n", 4, "", "total", 0, "",
677 dbstat
->locks
.num_calls
);
678 printf(" %*s%-22s%*s%10u\n", 4, "", "failed", 0, "",
679 dbstat
->locks
.num_failed
);
680 printf(" %*s%-22s%*s%10u\n", 4, "", "current", 0, "",
681 dbstat
->locks
.num_current
);
682 printf(" %*s%-22s%*s%10u\n", 4, "", "pending", 0, "",
683 dbstat
->locks
.num_pending
);
684 printf(" %s", "hop_count_buckets:");
685 for (i
=0; i
<MAX_COUNT_BUCKETS
; i
++) {
686 printf(" %d", dbstat
->hop_count_bucket
[i
]);
689 printf(" %s", "lock_buckets:");
690 for (i
=0; i
<MAX_COUNT_BUCKETS
; i
++) {
691 printf(" %d", dbstat
->locks
.buckets
[i
]);
694 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
695 "locks_latency MIN/AVG/MAX",
696 dbstat
->locks
.latency
.min
,
697 (dbstat
->locks
.latency
.num
?
698 dbstat
->locks
.latency
.total
/dbstat
->locks
.latency
.num
:
700 dbstat
->locks
.latency
.max
,
701 dbstat
->locks
.latency
.num
);
703 for (i
=0; i
<dbstat
->num_hot_keys
; i
++) {
704 if (dbstat
->hot_keys
[i
].count
> 0) {
708 dbstat
->num_hot_keys
= num_hot_keys
;
710 printf(" Num Hot Keys: %d\n", dbstat
->num_hot_keys
);
711 for (i
= 0; i
< dbstat
->num_hot_keys
; i
++) {
713 printf(" Count:%d Key:", dbstat
->hot_keys
[i
].count
);
714 for (j
= 0; j
< dbstat
->hot_keys
[i
].key
.dsize
; j
++) {
715 printf("%02x", dbstat
->hot_keys
[i
].key
.dptr
[j
]&0xff);
720 talloc_free(tmp_ctx
);
725 display uptime of remote node
727 static int control_uptime(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
730 struct ctdb_uptime
*uptime
= NULL
;
731 int tmp
, days
, hours
, minutes
, seconds
;
733 ret
= ctdb_ctrl_uptime(ctdb
, ctdb
, TIMELIMIT(), options
.pnn
, &uptime
);
735 DEBUG(DEBUG_ERR
, ("Unable to get uptime from node %u\n", options
.pnn
));
739 if (options
.machinereadable
){
740 printf(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
741 printf(":%u:%u:%u:%lf\n",
742 (unsigned int)uptime
->current_time
.tv_sec
,
743 (unsigned int)uptime
->ctdbd_start_time
.tv_sec
,
744 (unsigned int)uptime
->last_recovery_finished
.tv_sec
,
745 timeval_delta(&uptime
->last_recovery_finished
,
746 &uptime
->last_recovery_started
)
751 printf("Current time of node : %s", ctime(&uptime
->current_time
.tv_sec
));
753 tmp
= uptime
->current_time
.tv_sec
- uptime
->ctdbd_start_time
.tv_sec
;
761 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days
, hours
, minutes
, seconds
, ctime(&uptime
->ctdbd_start_time
.tv_sec
));
763 tmp
= uptime
->current_time
.tv_sec
- uptime
->last_recovery_finished
.tv_sec
;
771 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days
, hours
, minutes
, seconds
, ctime(&uptime
->last_recovery_finished
.tv_sec
));
773 printf("Duration of last recovery/failover: %lf seconds\n",
774 timeval_delta(&uptime
->last_recovery_finished
,
775 &uptime
->last_recovery_started
));
781 show the PNN of the current node
783 static int control_pnn(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
787 mypnn
= getpnn(ctdb
);
789 printf("PNN:%d\n", mypnn
);
795 struct pnn_node
*next
;
800 static struct pnn_node
*read_nodes_file(TALLOC_CTX
*mem_ctx
)
802 const char *nodes_list
;
806 struct pnn_node
*pnn_nodes
= NULL
;
807 struct pnn_node
*pnn_node
;
808 struct pnn_node
*tmp_node
;
810 /* read the nodes file */
811 nodes_list
= getenv("CTDB_NODES");
812 if (nodes_list
== NULL
) {
813 nodes_list
= talloc_asprintf(mem_ctx
, "%s/nodes",
814 getenv("CTDB_BASE"));
815 if (nodes_list
== NULL
) {
816 DEBUG(DEBUG_ALERT
,(__location__
" Out of memory\n"));
820 lines
= file_lines_load(nodes_list
, &nlines
, mem_ctx
);
824 while (nlines
> 0 && strcmp(lines
[nlines
-1], "") == 0) {
827 for (i
=0, pnn
=0; i
<nlines
; i
++) {
831 /* strip leading spaces */
832 while((*node
== ' ') || (*node
== '\t')) {
839 if (strcmp(node
, "") == 0) {
842 pnn_node
= talloc(mem_ctx
, struct pnn_node
);
843 pnn_node
->pnn
= pnn
++;
844 pnn_node
->addr
= talloc_strdup(pnn_node
, node
);
845 pnn_node
->next
= pnn_nodes
;
846 pnn_nodes
= pnn_node
;
849 /* swap them around so we return them in incrementing order */
850 pnn_node
= pnn_nodes
;
854 pnn_node
= pnn_node
->next
;
856 tmp_node
->next
= pnn_nodes
;
857 pnn_nodes
= tmp_node
;
864 show the PNN of the current node
865 discover the pnn by loading the nodes file and try to bind to all
866 addresses one at a time until the ip address is found.
868 static int control_xpnn(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
870 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
871 struct pnn_node
*pnn_nodes
;
872 struct pnn_node
*pnn_node
;
874 assert_single_node_only();
876 pnn_nodes
= read_nodes_file(mem_ctx
);
877 if (pnn_nodes
== NULL
) {
878 DEBUG(DEBUG_ERR
,("Failed to read nodes file\n"));
879 talloc_free(mem_ctx
);
883 for(pnn_node
=pnn_nodes
;pnn_node
;pnn_node
=pnn_node
->next
) {
886 if (parse_ip(pnn_node
->addr
, NULL
, 63999, &addr
) == 0) {
887 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s' in nodes file\n", pnn_node
->addr
));
888 talloc_free(mem_ctx
);
892 if (ctdb_sys_have_ip(&addr
)) {
893 printf("PNN:%d\n", pnn_node
->pnn
);
894 talloc_free(mem_ctx
);
899 printf("Failed to detect which PNN this node is\n");
900 talloc_free(mem_ctx
);
904 /* Helpers for ctdb status
906 static bool is_partially_online(struct ctdb_context
*ctdb
, struct ctdb_node_and_flags
*node
)
908 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
912 if (node
->flags
== 0) {
913 struct ctdb_control_get_ifaces
*ifaces
;
915 if (ctdb_ctrl_get_ifaces(ctdb
, TIMELIMIT(), node
->pnn
,
916 tmp_ctx
, &ifaces
) == 0) {
917 for (j
=0; j
< ifaces
->num
; j
++) {
918 if (ifaces
->ifaces
[j
].link_state
!= 0) {
926 talloc_free(tmp_ctx
);
931 static void control_status_header_machine(void)
933 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
934 ":Inactive:PartiallyOnline:ThisNode:\n");
937 static int control_status_1_machine(struct ctdb_context
*ctdb
, int mypnn
,
938 struct ctdb_node_and_flags
*node
)
940 printf(":%d:%s:%d:%d:%d:%d:%d:%d:%d:%c:\n", node
->pnn
,
941 ctdb_addr_to_str(&node
->addr
),
942 !!(node
->flags
&NODE_FLAGS_DISCONNECTED
),
943 !!(node
->flags
&NODE_FLAGS_BANNED
),
944 !!(node
->flags
&NODE_FLAGS_PERMANENTLY_DISABLED
),
945 !!(node
->flags
&NODE_FLAGS_UNHEALTHY
),
946 !!(node
->flags
&NODE_FLAGS_STOPPED
),
947 !!(node
->flags
&NODE_FLAGS_INACTIVE
),
948 is_partially_online(ctdb
, node
) ? 1 : 0,
949 (node
->pnn
== mypnn
)?'Y':'N');
954 static int control_status_1_human(struct ctdb_context
*ctdb
, int mypnn
,
955 struct ctdb_node_and_flags
*node
)
957 printf("pnn:%d %-16s %s%s\n", node
->pnn
,
958 ctdb_addr_to_str(&node
->addr
),
959 is_partially_online(ctdb
, node
) ? "PARTIALLYONLINE" : pretty_print_flags(node
->flags
),
960 node
->pnn
== mypnn
?" (THIS NODE)":"");
966 display remote ctdb status
968 static int control_status(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
970 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
972 struct ctdb_vnn_map
*vnnmap
=NULL
;
973 struct ctdb_node_map
*nodemap
=NULL
;
974 uint32_t recmode
, recmaster
, mypnn
;
975 int num_deleted_nodes
= 0;
978 mypnn
= getpnn(ctdb
);
980 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &nodemap
);
982 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
983 talloc_free(tmp_ctx
);
987 if (options
.machinereadable
) {
988 control_status_header_machine();
989 for (i
=0;i
<nodemap
->num
;i
++) {
990 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DELETED
) {
993 (void) control_status_1_machine(ctdb
, mypnn
,
996 talloc_free(tmp_ctx
);
1000 for (i
=0; i
<nodemap
->num
; i
++) {
1001 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DELETED
) {
1002 num_deleted_nodes
++;
1005 if (num_deleted_nodes
== 0) {
1006 printf("Number of nodes:%d\n", nodemap
->num
);
1008 printf("Number of nodes:%d (including %d deleted nodes)\n",
1009 nodemap
->num
, num_deleted_nodes
);
1011 for(i
=0;i
<nodemap
->num
;i
++){
1012 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DELETED
) {
1015 (void) control_status_1_human(ctdb
, mypnn
, &nodemap
->nodes
[i
]);
1018 ret
= ctdb_ctrl_getvnnmap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &vnnmap
);
1020 DEBUG(DEBUG_ERR
, ("Unable to get vnnmap from node %u\n", options
.pnn
));
1021 talloc_free(tmp_ctx
);
1024 if (vnnmap
->generation
== INVALID_GENERATION
) {
1025 printf("Generation:INVALID\n");
1027 printf("Generation:%d\n",vnnmap
->generation
);
1029 printf("Size:%d\n",vnnmap
->size
);
1030 for(i
=0;i
<vnnmap
->size
;i
++){
1031 printf("hash:%d lmaster:%d\n", i
, vnnmap
->map
[i
]);
1034 ret
= ctdb_ctrl_getrecmode(ctdb
, tmp_ctx
, TIMELIMIT(), options
.pnn
, &recmode
);
1036 DEBUG(DEBUG_ERR
, ("Unable to get recmode from node %u\n", options
.pnn
));
1037 talloc_free(tmp_ctx
);
1040 printf("Recovery mode:%s (%d)\n",recmode
==CTDB_RECOVERY_NORMAL
?"NORMAL":"RECOVERY",recmode
);
1042 ret
= ctdb_ctrl_getrecmaster(ctdb
, tmp_ctx
, TIMELIMIT(), options
.pnn
, &recmaster
);
1044 DEBUG(DEBUG_ERR
, ("Unable to get recmaster from node %u\n", options
.pnn
));
1045 talloc_free(tmp_ctx
);
1048 printf("Recovery master:%d\n",recmaster
);
1050 talloc_free(tmp_ctx
);
1054 static int control_nodestatus(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1056 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1058 struct ctdb_node_map
*nodemap
=NULL
;
1060 uint32_t pnn_mode
, mypnn
;
1066 if (!parse_nodestring(ctdb
, tmp_ctx
, argc
== 1 ? argv
[0] : NULL
,
1067 options
.pnn
, true, &nodes
, &pnn_mode
)) {
1071 if (options
.machinereadable
) {
1072 control_status_header_machine();
1073 } else if (pnn_mode
== CTDB_BROADCAST_ALL
) {
1074 printf("Number of nodes:%d\n", (int) talloc_array_length(nodes
));
1077 mypnn
= getpnn(ctdb
);
1079 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &nodemap
);
1081 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
1082 talloc_free(tmp_ctx
);
1088 for (i
= 0; i
< talloc_array_length(nodes
); i
++) {
1089 if (options
.machinereadable
) {
1090 ret
|= control_status_1_machine(ctdb
, mypnn
,
1091 &nodemap
->nodes
[nodes
[i
]]);
1093 ret
|= control_status_1_human(ctdb
, mypnn
,
1094 &nodemap
->nodes
[nodes
[i
]]);
1098 talloc_free(tmp_ctx
);
1103 struct natgw_node
*next
;
1107 static int find_natgw(struct ctdb_context
*ctdb
,
1108 struct ctdb_node_map
*nodemap
, uint32_t flags
,
1109 uint32_t *pnn
, const char **ip
)
1112 uint32_t capabilities
;
1115 for (i
=0;i
<nodemap
->num
;i
++) {
1116 if (!(nodemap
->nodes
[i
].flags
& flags
)) {
1117 ret
= ctdb_ctrl_getcapabilities(ctdb
, TIMELIMIT(),
1118 nodemap
->nodes
[i
].pnn
,
1121 DEBUG(DEBUG_ERR
, ("Unable to get capabilities from node %u\n",
1122 nodemap
->nodes
[i
].pnn
));
1125 if (!(capabilities
&CTDB_CAP_NATGW
)) {
1128 *pnn
= nodemap
->nodes
[i
].pnn
;
1129 *ip
= ctdb_addr_to_str(&nodemap
->nodes
[i
].addr
);
1134 return 2; /* matches ENOENT */
1138 display the list of nodes belonging to this natgw configuration
1140 static int control_natgwlist(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1142 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1144 const char *natgw_list
;
1147 struct natgw_node
*natgw_nodes
= NULL
;
1148 struct natgw_node
*natgw_node
;
1149 struct ctdb_node_map
*nodemap
=NULL
;
1150 uint32_t mypnn
, pnn
;
1153 /* When we have some nodes that could be the NATGW, make a
1154 * series of attempts to find the first node that doesn't have
1155 * certain status flags set.
1157 uint32_t exclude_flags
[] = {
1158 /* Look for a nice healthy node */
1159 NODE_FLAGS_DISCONNECTED
|NODE_FLAGS_STOPPED
|NODE_FLAGS_DELETED
|NODE_FLAGS_BANNED
|NODE_FLAGS_UNHEALTHY
,
1160 /* If not found, an UNHEALTHY/BANNED node will do */
1161 NODE_FLAGS_DISCONNECTED
|NODE_FLAGS_STOPPED
|NODE_FLAGS_DELETED
,
1162 /* If not found, a STOPPED node will do */
1163 NODE_FLAGS_DISCONNECTED
|NODE_FLAGS_DELETED
,
1167 /* read the natgw nodes file into a linked list */
1168 natgw_list
= getenv("CTDB_NATGW_NODES");
1169 if (natgw_list
== NULL
) {
1170 natgw_list
= talloc_asprintf(tmp_ctx
, "%s/natgw_nodes",
1171 getenv("CTDB_BASE"));
1172 if (natgw_list
== NULL
) {
1173 DEBUG(DEBUG_ALERT
,(__location__
" Out of memory\n"));
1177 lines
= file_lines_load(natgw_list
, &nlines
, ctdb
);
1178 if (lines
== NULL
) {
1179 ctdb_set_error(ctdb
, "Failed to load natgw node list '%s'\n", natgw_list
);
1180 talloc_free(tmp_ctx
);
1183 for (i
=0;i
<nlines
;i
++) {
1187 /* strip leading spaces */
1188 while((*node
== ' ') || (*node
== '\t')) {
1194 if (strcmp(node
, "") == 0) {
1197 natgw_node
= talloc(ctdb
, struct natgw_node
);
1198 natgw_node
->addr
= talloc_strdup(natgw_node
, node
);
1199 CTDB_NO_MEMORY(ctdb
, natgw_node
->addr
);
1200 natgw_node
->next
= natgw_nodes
;
1201 natgw_nodes
= natgw_node
;
1204 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, tmp_ctx
, &nodemap
);
1206 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from local node.\n"));
1207 talloc_free(tmp_ctx
);
1211 /* Trim the nodemap so it only includes connected nodes in the
1212 * current natgw group.
1215 while(i
<nodemap
->num
) {
1216 for(natgw_node
=natgw_nodes
;natgw_node
;natgw_node
=natgw_node
->next
) {
1217 if (!strcmp(natgw_node
->addr
, ctdb_addr_to_str(&nodemap
->nodes
[i
].addr
))) {
1222 /* this node was not in the natgw so we just remove it from
1225 if ((natgw_node
== NULL
)
1226 || (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DISCONNECTED
) ) {
1229 for (j
=i
+1; j
<nodemap
->num
; j
++) {
1230 nodemap
->nodes
[j
-1] = nodemap
->nodes
[j
];
1239 ret
= 2; /* matches ENOENT */
1242 for (i
= 0; exclude_flags
[i
] != 0; i
++) {
1243 ret
= find_natgw(ctdb
, nodemap
,
1254 if (options
.machinereadable
) {
1255 printf(":Node:IP:\n");
1256 printf(":%d:%s:\n", pnn
, ip
);
1258 printf("%d %s\n", pnn
, ip
);
1261 /* print the pruned list of nodes belonging to this natgw list */
1262 mypnn
= getpnn(ctdb
);
1263 if (options
.machinereadable
) {
1264 control_status_header_machine();
1266 printf("Number of nodes:%d\n", nodemap
->num
);
1268 for(i
=0;i
<nodemap
->num
;i
++){
1269 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DELETED
) {
1272 if (options
.machinereadable
) {
1273 control_status_1_machine(ctdb
, mypnn
, &(nodemap
->nodes
[i
]));
1275 control_status_1_human(ctdb
, mypnn
, &(nodemap
->nodes
[i
]));
1280 talloc_free(tmp_ctx
);
1285 display the status of the scripts for monitoring (or other events)
1287 static int control_one_scriptstatus(struct ctdb_context
*ctdb
,
1288 enum ctdb_eventscript_call type
)
1290 struct ctdb_scripts_wire
*script_status
;
1293 ret
= ctdb_ctrl_getscriptstatus(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, type
, &script_status
);
1295 DEBUG(DEBUG_ERR
, ("Unable to get script status from node %u\n", options
.pnn
));
1299 if (script_status
== NULL
) {
1300 if (!options
.machinereadable
) {
1301 printf("%s cycle never run\n",
1302 ctdb_eventscript_call_names
[type
]);
1307 if (!options
.machinereadable
) {
1308 printf("%d scripts were executed last %s cycle\n",
1309 script_status
->num_scripts
,
1310 ctdb_eventscript_call_names
[type
]);
1312 for (i
=0; i
<script_status
->num_scripts
; i
++) {
1313 const char *status
= NULL
;
1315 switch (script_status
->scripts
[i
].status
) {
1317 status
= "TIMEDOUT";
1320 status
= "DISABLED";
1326 if (script_status
->scripts
[i
].status
> 0)
1330 if (options
.machinereadable
) {
1331 printf(":%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
1332 ctdb_eventscript_call_names
[type
],
1333 script_status
->scripts
[i
].name
,
1334 script_status
->scripts
[i
].status
,
1336 (long)script_status
->scripts
[i
].start
.tv_sec
,
1337 (long)script_status
->scripts
[i
].start
.tv_usec
,
1338 (long)script_status
->scripts
[i
].finished
.tv_sec
,
1339 (long)script_status
->scripts
[i
].finished
.tv_usec
,
1340 script_status
->scripts
[i
].output
);
1344 printf("%-20s Status:%s ",
1345 script_status
->scripts
[i
].name
, status
);
1347 /* Some other error, eg from stat. */
1348 printf("%-20s Status:CANNOT RUN (%s)",
1349 script_status
->scripts
[i
].name
,
1350 strerror(-script_status
->scripts
[i
].status
));
1352 if (script_status
->scripts
[i
].status
>= 0) {
1353 printf("Duration:%.3lf ",
1354 timeval_delta(&script_status
->scripts
[i
].finished
,
1355 &script_status
->scripts
[i
].start
));
1357 if (script_status
->scripts
[i
].status
!= -ENOEXEC
) {
1359 ctime(&script_status
->scripts
[i
].start
.tv_sec
));
1360 if (script_status
->scripts
[i
].status
!= 0) {
1361 printf(" OUTPUT:%s\n",
1362 script_status
->scripts
[i
].output
);
1372 static int control_scriptstatus(struct ctdb_context
*ctdb
,
1373 int argc
, const char **argv
)
1376 enum ctdb_eventscript_call type
, min
, max
;
1380 DEBUG(DEBUG_ERR
, ("Unknown arguments to scriptstatus\n"));
1385 arg
= ctdb_eventscript_call_names
[CTDB_EVENT_MONITOR
];
1389 for (type
= 0; type
< CTDB_EVENT_MAX
; type
++) {
1390 if (strcmp(arg
, ctdb_eventscript_call_names
[type
]) == 0) {
1396 if (type
== CTDB_EVENT_MAX
) {
1397 if (strcmp(arg
, "all") == 0) {
1399 max
= CTDB_EVENT_MAX
;
1401 DEBUG(DEBUG_ERR
, ("Unknown event type %s\n", argv
[0]));
1406 if (options
.machinereadable
) {
1407 printf(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1410 for (type
= min
; type
< max
; type
++) {
1411 ret
= control_one_scriptstatus(ctdb
, type
);
1421 enable an eventscript
1423 static int control_enablescript(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1431 ret
= ctdb_ctrl_enablescript(ctdb
, TIMELIMIT(), options
.pnn
, argv
[0]);
1433 DEBUG(DEBUG_ERR
, ("Unable to enable script %s on node %u\n", argv
[0], options
.pnn
));
1441 disable an eventscript
1443 static int control_disablescript(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1451 ret
= ctdb_ctrl_disablescript(ctdb
, TIMELIMIT(), options
.pnn
, argv
[0]);
1453 DEBUG(DEBUG_ERR
, ("Unable to disable script %s on node %u\n", argv
[0], options
.pnn
));
1461 display the pnn of the recovery master
1463 static int control_recmaster(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1468 ret
= ctdb_ctrl_getrecmaster(ctdb
, ctdb
, TIMELIMIT(), options
.pnn
, &recmaster
);
1470 DEBUG(DEBUG_ERR
, ("Unable to get recmaster from node %u\n", options
.pnn
));
1473 printf("%d\n",recmaster
);
1479 add a tickle to a public address
1481 static int control_add_tickle(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1483 struct ctdb_tcp_connection t
;
1487 assert_single_node_only();
1493 if (parse_ip_port(argv
[0], &t
.src_addr
) == 0) {
1494 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
1497 if (parse_ip_port(argv
[1], &t
.dst_addr
) == 0) {
1498 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[1]));
1502 data
.dptr
= (uint8_t *)&t
;
1503 data
.dsize
= sizeof(t
);
1505 /* tell all nodes about this tcp connection */
1506 ret
= ctdb_control(ctdb
, options
.pnn
, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE
,
1507 0, data
, ctdb
, NULL
, NULL
, NULL
, NULL
);
1509 DEBUG(DEBUG_ERR
,("Failed to add tickle\n"));
1518 delete a tickle from a node
1520 static int control_del_tickle(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1522 struct ctdb_tcp_connection t
;
1526 assert_single_node_only();
1532 if (parse_ip_port(argv
[0], &t
.src_addr
) == 0) {
1533 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
1536 if (parse_ip_port(argv
[1], &t
.dst_addr
) == 0) {
1537 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[1]));
1541 data
.dptr
= (uint8_t *)&t
;
1542 data
.dsize
= sizeof(t
);
1544 /* tell all nodes about this tcp connection */
1545 ret
= ctdb_control(ctdb
, options
.pnn
, 0, CTDB_CONTROL_TCP_REMOVE
,
1546 0, data
, ctdb
, NULL
, NULL
, NULL
, NULL
);
1548 DEBUG(DEBUG_ERR
,("Failed to remove tickle\n"));
1557 get a list of all tickles for this pnn
1559 static int control_get_tickles(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1561 struct ctdb_control_tcp_tickle_list
*list
;
1562 ctdb_sock_addr addr
;
1566 assert_single_node_only();
1573 port
= atoi(argv
[1]);
1576 if (parse_ip(argv
[0], NULL
, 0, &addr
) == 0) {
1577 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
1581 ret
= ctdb_ctrl_get_tcp_tickles(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &addr
, &list
);
1583 DEBUG(DEBUG_ERR
, ("Unable to list tickles\n"));
1587 if (options
.machinereadable
){
1588 printf(":source ip:port:destination ip:port:\n");
1589 for (i
=0;i
<list
->tickles
.num
;i
++) {
1590 if (port
&& port
!= ntohs(list
->tickles
.connections
[i
].dst_addr
.ip
.sin_port
)) {
1593 printf(":%s:%u", ctdb_addr_to_str(&list
->tickles
.connections
[i
].src_addr
), ntohs(list
->tickles
.connections
[i
].src_addr
.ip
.sin_port
));
1594 printf(":%s:%u:\n", ctdb_addr_to_str(&list
->tickles
.connections
[i
].dst_addr
), ntohs(list
->tickles
.connections
[i
].dst_addr
.ip
.sin_port
));
1597 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list
->addr
));
1598 printf("Num tickles:%u\n", list
->tickles
.num
);
1599 for (i
=0;i
<list
->tickles
.num
;i
++) {
1600 if (port
&& port
!= ntohs(list
->tickles
.connections
[i
].dst_addr
.ip
.sin_port
)) {
1603 printf("SRC: %s:%u ", ctdb_addr_to_str(&list
->tickles
.connections
[i
].src_addr
), ntohs(list
->tickles
.connections
[i
].src_addr
.ip
.sin_port
));
1604 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
));
1614 static int move_ip(struct ctdb_context
*ctdb
, ctdb_sock_addr
*addr
, uint32_t pnn
)
1616 struct ctdb_all_public_ips
*ips
;
1617 struct ctdb_public_ip ip
;
1620 uint32_t disable_time
;
1622 struct ctdb_node_map
*nodemap
=NULL
;
1623 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1626 data
.dptr
= (uint8_t*)&disable_time
;
1627 data
.dsize
= sizeof(disable_time
);
1628 ret
= ctdb_client_send_message(ctdb
, CTDB_BROADCAST_CONNECTED
, CTDB_SRVID_DISABLE_IP_CHECK
, data
);
1630 DEBUG(DEBUG_ERR
,("Failed to send message to disable ipcheck\n"));
1636 /* read the public ip list from the node */
1637 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), pnn
, ctdb
, &ips
);
1639 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from node %u\n", pnn
));
1640 talloc_free(tmp_ctx
);
1644 for (i
=0;i
<ips
->num
;i
++) {
1645 if (ctdb_same_ip(addr
, &ips
->ips
[i
].addr
)) {
1650 DEBUG(DEBUG_ERR
, ("Node %u can not host ip address '%s'\n",
1651 pnn
, ctdb_addr_to_str(addr
)));
1652 talloc_free(tmp_ctx
);
1659 data
.dptr
= (uint8_t *)&ip
;
1660 data
.dsize
= sizeof(ip
);
1662 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &nodemap
);
1664 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
1665 talloc_free(tmp_ctx
);
1669 nodes
= list_of_nodes(ctdb
, nodemap
, tmp_ctx
, NODE_FLAGS_INACTIVE
, pnn
);
1670 ret
= ctdb_client_async_control(ctdb
, CTDB_CONTROL_RELEASE_IP
,
1677 DEBUG(DEBUG_ERR
,("Failed to release IP on nodes\n"));
1678 talloc_free(tmp_ctx
);
1682 ret
= ctdb_ctrl_takeover_ip(ctdb
, LONGTIMELIMIT(), pnn
, &ip
);
1684 DEBUG(DEBUG_ERR
,("Failed to take over IP on node %d\n", pnn
));
1685 talloc_free(tmp_ctx
);
1689 /* update the recovery daemon so it now knows to expect the new
1690 node assignment for this ip.
1692 ret
= ctdb_client_send_message(ctdb
, CTDB_BROADCAST_CONNECTED
, CTDB_SRVID_RECD_UPDATE_IP
, data
);
1694 DEBUG(DEBUG_ERR
,("Failed to send message to update the ip on the recovery master.\n"));
1698 talloc_free(tmp_ctx
);
1704 * scans all other nodes and returns a pnn for another node that can host this
1708 find_other_host_for_public_ip(struct ctdb_context
*ctdb
, ctdb_sock_addr
*addr
)
1710 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1711 struct ctdb_all_public_ips
*ips
;
1712 struct ctdb_node_map
*nodemap
=NULL
;
1715 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, tmp_ctx
, &nodemap
);
1717 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
1718 talloc_free(tmp_ctx
);
1722 for(i
=0;i
<nodemap
->num
;i
++){
1723 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_INACTIVE
) {
1726 if (nodemap
->nodes
[i
].pnn
== options
.pnn
) {
1730 /* read the public ip list from this node */
1731 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), nodemap
->nodes
[i
].pnn
, tmp_ctx
, &ips
);
1733 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from node %u\n", nodemap
->nodes
[i
].pnn
));
1737 for (j
=0;j
<ips
->num
;j
++) {
1738 if (ctdb_same_ip(addr
, &ips
->ips
[j
].addr
)) {
1739 talloc_free(tmp_ctx
);
1740 return nodemap
->nodes
[i
].pnn
;
1746 talloc_free(tmp_ctx
);
1750 /* If pnn is -1 then try to find a node to move IP to... */
1751 static bool try_moveip(struct ctdb_context
*ctdb
, ctdb_sock_addr
*addr
, uint32_t pnn
)
1753 bool pnn_specified
= (pnn
== -1 ? false : true);
1756 while (retries
< 5) {
1757 if (!pnn_specified
) {
1758 pnn
= find_other_host_for_public_ip(ctdb
, addr
);
1763 ("Trying to move public IP to node %u\n", pnn
));
1766 if (move_ip(ctdb
, addr
, pnn
) == 0) {
1779 move/failover an ip address to a specific node
1781 static int control_moveip(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1784 ctdb_sock_addr addr
;
1786 assert_single_node_only();
1793 if (parse_ip(argv
[0], NULL
, 0, &addr
) == 0) {
1794 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
1799 if (sscanf(argv
[1], "%u", &pnn
) != 1) {
1800 DEBUG(DEBUG_ERR
, ("Badly formed pnn\n"));
1804 if (!try_moveip(ctdb
, &addr
, pnn
)) {
1805 DEBUG(DEBUG_ERR
,("Failed to move IP to node %d.\n", pnn
));
1812 static int rebalance_node(struct ctdb_context
*ctdb
, uint32_t pnn
)
1816 data
.dptr
= (uint8_t *)&pnn
;
1817 data
.dsize
= sizeof(uint32_t);
1818 if (ctdb_client_send_message(ctdb
, CTDB_BROADCAST_CONNECTED
, CTDB_SRVID_REBALANCE_NODE
, data
) != 0) {
1820 ("Failed to send message to force node %u to be a rebalancing target\n",
1830 rebalance a node by setting it to allow failback and triggering a
1833 static int control_rebalancenode(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1835 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1840 assert_single_node_only();
1846 /* Determine the nodes where IPs need to be reloaded */
1847 if (!parse_nodestring(ctdb
, tmp_ctx
, argc
== 1 ? argv
[0] : NULL
,
1848 options
.pnn
, true, &nodes
, &pnn_mode
)) {
1853 for (i
= 0; i
< talloc_array_length(nodes
); i
++) {
1854 if (!rebalance_node(ctdb
, nodes
[i
])) {
1860 talloc_free(tmp_ctx
);
1864 static int rebalance_ip(struct ctdb_context
*ctdb
, ctdb_sock_addr
*addr
)
1866 struct ctdb_public_ip ip
;
1869 uint32_t disable_time
;
1871 struct ctdb_node_map
*nodemap
=NULL
;
1872 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1875 data
.dptr
= (uint8_t*)&disable_time
;
1876 data
.dsize
= sizeof(disable_time
);
1877 ret
= ctdb_client_send_message(ctdb
, CTDB_BROADCAST_CONNECTED
, CTDB_SRVID_DISABLE_IP_CHECK
, data
);
1879 DEBUG(DEBUG_ERR
,("Failed to send message to disable ipcheck\n"));
1886 data
.dptr
= (uint8_t *)&ip
;
1887 data
.dsize
= sizeof(ip
);
1889 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &nodemap
);
1891 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
1892 talloc_free(tmp_ctx
);
1896 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
1897 ret
= ctdb_client_async_control(ctdb
, CTDB_CONTROL_RELEASE_IP
,
1904 DEBUG(DEBUG_ERR
,("Failed to release IP on nodes\n"));
1905 talloc_free(tmp_ctx
);
1909 talloc_free(tmp_ctx
);
1914 release an ip form all nodes and have it re-assigned by recd
1916 static int control_rebalanceip(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1918 ctdb_sock_addr addr
;
1920 assert_single_node_only();
1927 if (parse_ip(argv
[0], NULL
, 0, &addr
) == 0) {
1928 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
1932 if (rebalance_ip(ctdb
, &addr
) != 0) {
1933 DEBUG(DEBUG_ERR
,("Error when trying to reassign ip\n"));
1940 static int getips_store_callback(void *param
, void *data
)
1942 struct ctdb_public_ip
*node_ip
= (struct ctdb_public_ip
*)data
;
1943 struct ctdb_all_public_ips
*ips
= param
;
1947 ips
->ips
[i
].pnn
= node_ip
->pnn
;
1948 ips
->ips
[i
].addr
= node_ip
->addr
;
1952 static int getips_count_callback(void *param
, void *data
)
1954 uint32_t *count
= param
;
1961 static uint32_t *ip_key(ctdb_sock_addr
*ip
)
1963 static uint32_t key
[IP_KEYLEN
];
1965 bzero(key
, sizeof(key
));
1967 switch (ip
->sa
.sa_family
) {
1969 key
[0] = ip
->ip
.sin_addr
.s_addr
;
1972 uint32_t *s6_a32
= (uint32_t *)&(ip
->ip6
.sin6_addr
.s6_addr
);
1980 DEBUG(DEBUG_ERR
, (__location__
" ERROR, unknown family passed :%u\n", ip
->sa
.sa_family
));
1987 static void *add_ip_callback(void *parm
, void *data
)
1993 control_get_all_public_ips(struct ctdb_context
*ctdb
, TALLOC_CTX
*tmp_ctx
, struct ctdb_all_public_ips
**ips
)
1995 struct ctdb_all_public_ips
*tmp_ips
;
1996 struct ctdb_node_map
*nodemap
=NULL
;
1997 trbt_tree_t
*ip_tree
;
2001 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, tmp_ctx
, &nodemap
);
2003 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
2007 ip_tree
= trbt_create(tmp_ctx
, 0);
2009 for(i
=0;i
<nodemap
->num
;i
++){
2010 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DELETED
) {
2013 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DISCONNECTED
) {
2017 /* read the public ip list from this node */
2018 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), nodemap
->nodes
[i
].pnn
, tmp_ctx
, &tmp_ips
);
2020 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from node %u\n", nodemap
->nodes
[i
].pnn
));
2024 for (j
=0; j
<tmp_ips
->num
;j
++) {
2025 struct ctdb_public_ip
*node_ip
;
2027 node_ip
= talloc(tmp_ctx
, struct ctdb_public_ip
);
2028 node_ip
->pnn
= tmp_ips
->ips
[j
].pnn
;
2029 node_ip
->addr
= tmp_ips
->ips
[j
].addr
;
2031 trbt_insertarray32_callback(ip_tree
,
2032 IP_KEYLEN
, ip_key(&tmp_ips
->ips
[j
].addr
),
2036 talloc_free(tmp_ips
);
2041 trbt_traversearray32(ip_tree
, IP_KEYLEN
, getips_count_callback
, &count
);
2043 len
= offsetof(struct ctdb_all_public_ips
, ips
) +
2044 count
*sizeof(struct ctdb_public_ip
);
2045 tmp_ips
= talloc_zero_size(tmp_ctx
, len
);
2046 trbt_traversearray32(ip_tree
, IP_KEYLEN
, getips_store_callback
, tmp_ips
);
2054 static void ctdb_every_second(struct event_context
*ev
, struct timed_event
*te
, struct timeval t
, void *p
)
2056 struct ctdb_context
*ctdb
= talloc_get_type(p
, struct ctdb_context
);
2058 event_add_timed(ctdb
->ev
, ctdb
,
2059 timeval_current_ofs(1, 0),
2060 ctdb_every_second
, ctdb
);
2063 struct srvid_reply_handler_data
{
2067 const char *srvid_str
;
2070 static void srvid_broadcast_reply_handler(struct ctdb_context
*ctdb
,
2075 struct srvid_reply_handler_data
*d
=
2076 (struct srvid_reply_handler_data
*)private_data
;
2080 if (data
.dsize
!= sizeof(ret
)) {
2081 DEBUG(DEBUG_ERR
, (__location__
" Wrong reply size\n"));
2085 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
2086 ret
= *(int32_t *)data
.dptr
;
2089 ("%s failed with result %d\n", d
->srvid_str
, ret
));
2093 if (!d
->wait_for_all
) {
2098 /* Wait for all replies */
2100 for (i
= 0; i
< talloc_array_length(d
->nodes
); i
++) {
2101 if (d
->nodes
[i
] == ret
) {
2103 ("%s reply received from node %u\n",
2104 d
->srvid_str
, ret
));
2107 if (d
->nodes
[i
] != -1) {
2108 /* Found a node that hasn't yet replied */
2114 /* Broadcast the given SRVID to all connected nodes. Wait for 1 reply
2115 * or replies from all connected nodes. arg is the data argument to
2116 * pass in the srvid_request structure - pass 0 if this isn't needed.
2118 static int srvid_broadcast(struct ctdb_context
*ctdb
,
2119 uint64_t srvid
, uint32_t arg
,
2120 const char *srvid_str
, bool wait_for_all
)
2124 struct srvid_request request
;
2125 struct srvid_reply_handler_data reply_data
;
2128 ZERO_STRUCT(request
);
2130 /* Time ticks to enable timeouts to be processed */
2131 event_add_timed(ctdb
->ev
, ctdb
,
2132 timeval_current_ofs(1, 0),
2133 ctdb_every_second
, ctdb
);
2135 request
.pnn
= ctdb_get_pnn(ctdb
);
2136 request
.srvid
= getpid();
2139 /* Register message port for reply from recovery master */
2140 ctdb_client_set_message_handler(ctdb
, request
.srvid
,
2141 srvid_broadcast_reply_handler
,
2144 data
.dptr
= (uint8_t *)&request
;
2145 data
.dsize
= sizeof(request
);
2147 reply_data
.wait_for_all
= wait_for_all
;
2148 reply_data
.nodes
= NULL
;
2149 reply_data
.srvid_str
= srvid_str
;
2152 reply_data
.done
= false;
2155 struct ctdb_node_map
*nodemap
;
2157 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(),
2158 CTDB_CURRENT_NODE
, ctdb
, &nodemap
);
2161 ("Unable to get nodemap from current node, try again\n"));
2166 if (reply_data
.nodes
!= NULL
) {
2167 talloc_free(reply_data
.nodes
);
2169 reply_data
.nodes
= list_of_connected_nodes(ctdb
, nodemap
,
2172 talloc_free(nodemap
);
2175 /* Send to all connected nodes. Only recmaster replies */
2176 ret
= ctdb_client_send_message(ctdb
, CTDB_BROADCAST_CONNECTED
,
2179 /* This can only happen if the socket is closed and
2180 * there's no way to recover from that, so don't try
2184 ("Failed to send %s request to connected nodes\n",
2189 tv
= timeval_current();
2190 /* This loop terminates the reply is received */
2191 while (timeval_elapsed(&tv
) < 5.0 && !reply_data
.done
) {
2192 event_loop_once(ctdb
->ev
);
2195 if (!reply_data
.done
) {
2197 ("Still waiting for confirmation of %s\n", srvid_str
));
2202 ctdb_client_remove_message_handler(ctdb
, request
.srvid
, &reply_data
);
2204 talloc_free(reply_data
.nodes
);
2209 static int ipreallocate(struct ctdb_context
*ctdb
)
2211 return srvid_broadcast(ctdb
, CTDB_SRVID_TAKEOVER_RUN
, 0,
2212 "IP reallocation", false);
2216 static int control_ipreallocate(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2218 return ipreallocate(ctdb
);
2222 add a public ip address to a node
2224 static int control_addip(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2227 int len
, retries
= 0;
2229 ctdb_sock_addr addr
;
2230 struct ctdb_control_ip_iface
*pub
;
2231 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
2232 struct ctdb_all_public_ips
*ips
;
2236 talloc_free(tmp_ctx
);
2240 if (!parse_ip_mask(argv
[0], argv
[1], &addr
, &mask
)) {
2241 DEBUG(DEBUG_ERR
, ("Badly formed ip/mask : %s\n", argv
[0]));
2242 talloc_free(tmp_ctx
);
2246 /* read the public ip list from the node */
2247 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &ips
);
2249 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from node %u\n", options
.pnn
));
2250 talloc_free(tmp_ctx
);
2253 for (i
=0;i
<ips
->num
;i
++) {
2254 if (ctdb_same_ip(&addr
, &ips
->ips
[i
].addr
)) {
2255 DEBUG(DEBUG_ERR
,("Can not add ip to node. Node already hosts this ip\n"));
2262 /* Dont timeout. This command waits for an ip reallocation
2263 which sometimes can take wuite a while if there has
2264 been a recent recovery
2268 len
= offsetof(struct ctdb_control_ip_iface
, iface
) + strlen(argv
[1]) + 1;
2269 pub
= talloc_size(tmp_ctx
, len
);
2270 CTDB_NO_MEMORY(ctdb
, pub
);
2274 pub
->len
= strlen(argv
[1])+1;
2275 memcpy(&pub
->iface
[0], argv
[1], strlen(argv
[1])+1);
2278 ret
= ctdb_ctrl_add_public_ip(ctdb
, TIMELIMIT(), options
.pnn
, pub
);
2280 DEBUG(DEBUG_ERR
, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options
.pnn
));
2284 } while (retries
< 5 && ret
!= 0);
2286 DEBUG(DEBUG_ERR
, ("Unable to add public ip to node %u. Giving up.\n", options
.pnn
));
2287 talloc_free(tmp_ctx
);
2291 if (rebalance_node(ctdb
, options
.pnn
) != 0) {
2292 DEBUG(DEBUG_ERR
,("Error when trying to rebalance node\n"));
2296 talloc_free(tmp_ctx
);
2301 add a public ip address to a node
2303 static int control_ipiface(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2305 ctdb_sock_addr addr
;
2311 if (!parse_ip(argv
[0], NULL
, 0, &addr
)) {
2312 printf("Badly formed ip : %s\n", argv
[0]);
2316 printf("IP on interface %s\n", ctdb_sys_find_ifname(&addr
));
2321 static int control_delip(struct ctdb_context
*ctdb
, int argc
, const char **argv
);
2323 static int control_delip_all(struct ctdb_context
*ctdb
, int argc
, const char **argv
, ctdb_sock_addr
*addr
)
2325 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
2326 struct ctdb_node_map
*nodemap
=NULL
;
2327 struct ctdb_all_public_ips
*ips
;
2330 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, tmp_ctx
, &nodemap
);
2332 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from current node\n"));
2336 /* remove it from the nodes that are not hosting the ip currently */
2337 for(i
=0;i
<nodemap
->num
;i
++){
2338 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_INACTIVE
) {
2341 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), nodemap
->nodes
[i
].pnn
, tmp_ctx
, &ips
);
2343 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from node %d\n", nodemap
->nodes
[i
].pnn
));
2347 for (j
=0;j
<ips
->num
;j
++) {
2348 if (ctdb_same_ip(addr
, &ips
->ips
[j
].addr
)) {
2356 if (ips
->ips
[j
].pnn
== nodemap
->nodes
[i
].pnn
) {
2360 options
.pnn
= nodemap
->nodes
[i
].pnn
;
2361 control_delip(ctdb
, argc
, argv
);
2365 /* remove it from every node (also the one hosting it) */
2366 for(i
=0;i
<nodemap
->num
;i
++){
2367 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_INACTIVE
) {
2370 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), nodemap
->nodes
[i
].pnn
, tmp_ctx
, &ips
);
2372 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from node %d\n", nodemap
->nodes
[i
].pnn
));
2376 for (j
=0;j
<ips
->num
;j
++) {
2377 if (ctdb_same_ip(addr
, &ips
->ips
[j
].addr
)) {
2385 options
.pnn
= nodemap
->nodes
[i
].pnn
;
2386 control_delip(ctdb
, argc
, argv
);
2389 talloc_free(tmp_ctx
);
2394 delete a public ip address from a node
2396 static int control_delip(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2399 ctdb_sock_addr addr
;
2400 struct ctdb_control_ip_iface pub
;
2401 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
2402 struct ctdb_all_public_ips
*ips
;
2405 talloc_free(tmp_ctx
);
2409 if (parse_ip(argv
[0], NULL
, 0, &addr
) == 0) {
2410 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
2414 if (options
.pnn
== CTDB_BROADCAST_ALL
) {
2415 return control_delip_all(ctdb
, argc
, argv
, &addr
);
2422 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &ips
);
2424 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from cluster\n"));
2425 talloc_free(tmp_ctx
);
2429 for (i
=0;i
<ips
->num
;i
++) {
2430 if (ctdb_same_ip(&addr
, &ips
->ips
[i
].addr
)) {
2436 DEBUG(DEBUG_ERR
, ("This node does not support this public address '%s'\n",
2437 ctdb_addr_to_str(&addr
)));
2438 talloc_free(tmp_ctx
);
2442 /* This is an optimisation. If this node is hosting the IP
2443 * then try to move it somewhere else without invoking a full
2444 * takeover run. We don't care if this doesn't work!
2446 if (ips
->ips
[i
].pnn
== options
.pnn
) {
2447 (void) try_moveip(ctdb
, &addr
, -1);
2450 ret
= ctdb_ctrl_del_public_ip(ctdb
, TIMELIMIT(), options
.pnn
, &pub
);
2452 DEBUG(DEBUG_ERR
, ("Unable to del public ip from node %u\n", options
.pnn
));
2453 talloc_free(tmp_ctx
);
2457 talloc_free(tmp_ctx
);
2461 static int kill_tcp_from_file(struct ctdb_context
*ctdb
,
2462 int argc
, const char **argv
)
2464 struct ctdb_control_killtcp
*killtcp
;
2465 int max_entries
, current
, i
;
2466 struct timeval timeout
;
2467 char line
[128], src
[128], dst
[128];
2470 struct client_async_data
*async_data
;
2471 struct ctdb_client_control_state
*state
;
2481 while (!feof(stdin
)) {
2482 if (fgets(line
, sizeof(line
), stdin
) == NULL
) {
2486 /* Silently skip empty lines */
2487 if (line
[0] == '\n') {
2491 if (sscanf(line
, "%s %s\n", src
, dst
) != 2) {
2492 DEBUG(DEBUG_ERR
, ("Bad line [%d]: '%s'\n",
2494 talloc_free(killtcp
);
2498 if (current
>= max_entries
) {
2499 max_entries
+= 1024;
2500 killtcp
= talloc_realloc(ctdb
, killtcp
,
2501 struct ctdb_control_killtcp
,
2503 CTDB_NO_MEMORY(ctdb
, killtcp
);
2506 if (!parse_ip_port(src
, &killtcp
[current
].src_addr
)) {
2507 DEBUG(DEBUG_ERR
, ("Bad IP:port on line [%d]: '%s'\n",
2509 talloc_free(killtcp
);
2513 if (!parse_ip_port(dst
, &killtcp
[current
].dst_addr
)) {
2514 DEBUG(DEBUG_ERR
, ("Bad IP:port on line [%d]: '%s'\n",
2516 talloc_free(killtcp
);
2523 async_data
= talloc_zero(ctdb
, struct client_async_data
);
2524 if (async_data
== NULL
) {
2525 talloc_free(killtcp
);
2529 for (i
= 0; i
< current
; i
++) {
2531 data
.dsize
= sizeof(struct ctdb_control_killtcp
);
2532 data
.dptr
= (unsigned char *)&killtcp
[i
];
2534 timeout
= TIMELIMIT();
2535 state
= ctdb_control_send(ctdb
, options
.pnn
, 0,
2536 CTDB_CONTROL_KILL_TCP
, 0, data
,
2537 async_data
, &timeout
, NULL
);
2539 if (state
== NULL
) {
2541 ("Failed to call async killtcp control to node %u\n",
2543 talloc_free(killtcp
);
2547 ctdb_client_async_add(async_data
, state
);
2550 if (ctdb_client_async_wait(ctdb
, async_data
) != 0) {
2551 DEBUG(DEBUG_ERR
,("killtcp failed\n"));
2552 talloc_free(killtcp
);
2556 talloc_free(killtcp
);
2562 kill a tcp connection
2564 static int kill_tcp(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2567 struct ctdb_control_killtcp killtcp
;
2569 assert_single_node_only();
2572 return kill_tcp_from_file(ctdb
, argc
, argv
);
2579 if (!parse_ip_port(argv
[0], &killtcp
.src_addr
)) {
2580 DEBUG(DEBUG_ERR
, ("Bad IP:port '%s'\n", argv
[0]));
2584 if (!parse_ip_port(argv
[1], &killtcp
.dst_addr
)) {
2585 DEBUG(DEBUG_ERR
, ("Bad IP:port '%s'\n", argv
[1]));
2589 ret
= ctdb_ctrl_killtcp(ctdb
, TIMELIMIT(), options
.pnn
, &killtcp
);
2591 DEBUG(DEBUG_ERR
, ("Unable to killtcp from node %u\n", options
.pnn
));
2602 static int control_gratious_arp(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2605 ctdb_sock_addr addr
;
2607 assert_single_node_only();
2613 if (!parse_ip(argv
[0], NULL
, 0, &addr
)) {
2614 DEBUG(DEBUG_ERR
, ("Bad IP '%s'\n", argv
[0]));
2618 ret
= ctdb_ctrl_gratious_arp(ctdb
, TIMELIMIT(), options
.pnn
, &addr
, argv
[1]);
2620 DEBUG(DEBUG_ERR
, ("Unable to send gratious_arp from node %u\n", options
.pnn
));
2628 register a server id
2630 static int regsrvid(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2633 struct ctdb_server_id server_id
;
2639 server_id
.pnn
= strtoul(argv
[0], NULL
, 0);
2640 server_id
.type
= strtoul(argv
[1], NULL
, 0);
2641 server_id
.server_id
= strtoul(argv
[2], NULL
, 0);
2643 ret
= ctdb_ctrl_register_server_id(ctdb
, TIMELIMIT(), &server_id
);
2645 DEBUG(DEBUG_ERR
, ("Unable to register server_id from node %u\n", options
.pnn
));
2648 DEBUG(DEBUG_ERR
,("Srvid registered. Sleeping for 999 seconds\n"));
2654 unregister a server id
2656 static int unregsrvid(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2659 struct ctdb_server_id server_id
;
2665 server_id
.pnn
= strtoul(argv
[0], NULL
, 0);
2666 server_id
.type
= strtoul(argv
[1], NULL
, 0);
2667 server_id
.server_id
= strtoul(argv
[2], NULL
, 0);
2669 ret
= ctdb_ctrl_unregister_server_id(ctdb
, TIMELIMIT(), &server_id
);
2671 DEBUG(DEBUG_ERR
, ("Unable to unregister server_id from node %u\n", options
.pnn
));
2678 check if a server id exists
2680 static int chksrvid(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2684 struct ctdb_server_id server_id
;
2690 server_id
.pnn
= strtoul(argv
[0], NULL
, 0);
2691 server_id
.type
= strtoul(argv
[1], NULL
, 0);
2692 server_id
.server_id
= strtoul(argv
[2], NULL
, 0);
2694 ret
= ctdb_ctrl_check_server_id(ctdb
, TIMELIMIT(), options
.pnn
, &server_id
, &status
);
2696 DEBUG(DEBUG_ERR
, ("Unable to check server_id from node %u\n", options
.pnn
));
2701 printf("Server id %d:%d:%d EXISTS\n", server_id
.pnn
, server_id
.type
, server_id
.server_id
);
2703 printf("Server id %d:%d:%d does NOT exist\n", server_id
.pnn
, server_id
.type
, server_id
.server_id
);
2709 get a list of all server ids that are registered on a node
2711 static int getsrvids(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2714 struct ctdb_server_id_list
*server_ids
;
2716 ret
= ctdb_ctrl_get_server_id_list(ctdb
, ctdb
, TIMELIMIT(), options
.pnn
, &server_ids
);
2718 DEBUG(DEBUG_ERR
, ("Unable to get server_id list from node %u\n", options
.pnn
));
2722 for (i
=0; i
<server_ids
->num
; i
++) {
2723 printf("Server id %d:%d:%d\n",
2724 server_ids
->server_ids
[i
].pnn
,
2725 server_ids
->server_ids
[i
].type
,
2726 server_ids
->server_ids
[i
].server_id
);
2733 check if a server id exists
2735 static int check_srvids(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2737 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
2743 talloc_free(tmp_ctx
);
2747 ids
= talloc_array(tmp_ctx
, uint64_t, argc
);
2748 result
= talloc_array(tmp_ctx
, uint8_t, argc
);
2750 for (i
= 0; i
< argc
; i
++) {
2751 ids
[i
] = strtoull(argv
[i
], NULL
, 0);
2754 if (!ctdb_client_check_message_handlers(ctdb
, ids
, argc
, result
)) {
2755 DEBUG(DEBUG_ERR
, ("Unable to check server_id from node %u\n",
2757 talloc_free(tmp_ctx
);
2761 for (i
=0; i
< argc
; i
++) {
2762 printf("Server id %d:%llu %s\n", options
.pnn
, (long long)ids
[i
],
2763 result
[i
] ? "exists" : "does not exist");
2766 talloc_free(tmp_ctx
);
2771 send a tcp tickle ack
2773 static int tickle_tcp(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2776 ctdb_sock_addr src
, dst
;
2782 if (!parse_ip_port(argv
[0], &src
)) {
2783 DEBUG(DEBUG_ERR
, ("Bad IP:port '%s'\n", argv
[0]));
2787 if (!parse_ip_port(argv
[1], &dst
)) {
2788 DEBUG(DEBUG_ERR
, ("Bad IP:port '%s'\n", argv
[1]));
2792 ret
= ctdb_sys_send_tcp(&src
, &dst
, 0, 0, 0);
2796 DEBUG(DEBUG_ERR
, ("Error while sending tickle ack\n"));
2803 display public ip status
2805 static int control_ip(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2808 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
2809 struct ctdb_all_public_ips
*ips
;
2811 if (options
.pnn
== CTDB_BROADCAST_ALL
) {
2812 /* read the list of public ips from all nodes */
2813 ret
= control_get_all_public_ips(ctdb
, tmp_ctx
, &ips
);
2815 /* read the public ip list from this node */
2816 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &ips
);
2819 DEBUG(DEBUG_ERR
, ("Unable to get public ips from node %u\n", options
.pnn
));
2820 talloc_free(tmp_ctx
);
2824 if (options
.machinereadable
){
2825 printf(":Public IP:Node:");
2826 if (options
.verbose
){
2827 printf("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2831 if (options
.pnn
== CTDB_BROADCAST_ALL
) {
2832 printf("Public IPs on ALL nodes\n");
2834 printf("Public IPs on node %u\n", options
.pnn
);
2838 for (i
=1;i
<=ips
->num
;i
++) {
2839 struct ctdb_control_public_ip_info
*info
= NULL
;
2841 char *aciface
= NULL
;
2842 char *avifaces
= NULL
;
2843 char *cifaces
= NULL
;
2845 if (options
.pnn
== CTDB_BROADCAST_ALL
) {
2846 pnn
= ips
->ips
[ips
->num
-i
].pnn
;
2852 ret
= ctdb_ctrl_get_public_ip_info(ctdb
, TIMELIMIT(), pnn
, ctdb
,
2853 &ips
->ips
[ips
->num
-i
].addr
, &info
);
2860 for (j
=0; j
< info
->num
; j
++) {
2861 if (cifaces
== NULL
) {
2862 cifaces
= talloc_strdup(info
,
2863 info
->ifaces
[j
].name
);
2865 cifaces
= talloc_asprintf_append(cifaces
,
2867 info
->ifaces
[j
].name
);
2870 if (info
->active_idx
== j
) {
2871 aciface
= info
->ifaces
[j
].name
;
2874 if (info
->ifaces
[j
].link_state
== 0) {
2878 if (avifaces
== NULL
) {
2879 avifaces
= talloc_strdup(info
, info
->ifaces
[j
].name
);
2881 avifaces
= talloc_asprintf_append(avifaces
,
2883 info
->ifaces
[j
].name
);
2888 if (options
.machinereadable
){
2890 ctdb_addr_to_str(&ips
->ips
[ips
->num
-i
].addr
),
2891 ips
->ips
[ips
->num
-i
].pnn
);
2892 if (options
.verbose
){
2895 avifaces
?avifaces
:"",
2896 cifaces
?cifaces
:"");
2900 if (options
.verbose
) {
2901 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
2902 ctdb_addr_to_str(&ips
->ips
[ips
->num
-i
].addr
),
2903 ips
->ips
[ips
->num
-i
].pnn
,
2905 avifaces
?avifaces
:"",
2906 cifaces
?cifaces
:"");
2909 ctdb_addr_to_str(&ips
->ips
[ips
->num
-i
].addr
),
2910 ips
->ips
[ips
->num
-i
].pnn
);
2916 talloc_free(tmp_ctx
);
2923 static int control_ipinfo(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2926 ctdb_sock_addr addr
;
2927 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
2928 struct ctdb_control_public_ip_info
*info
;
2931 talloc_free(tmp_ctx
);
2935 if (parse_ip(argv
[0], NULL
, 0, &addr
) == 0) {
2936 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
2940 /* read the public ip info from this node */
2941 ret
= ctdb_ctrl_get_public_ip_info(ctdb
, TIMELIMIT(), options
.pnn
,
2942 tmp_ctx
, &addr
, &info
);
2944 DEBUG(DEBUG_ERR
, ("Unable to get public ip[%s]info from node %u\n",
2945 argv
[0], options
.pnn
));
2946 talloc_free(tmp_ctx
);
2950 printf("Public IP[%s] info on node %u\n",
2951 ctdb_addr_to_str(&info
->ip
.addr
),
2954 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
2955 ctdb_addr_to_str(&info
->ip
.addr
),
2956 info
->ip
.pnn
, info
->num
);
2958 for (i
=0; i
<info
->num
; i
++) {
2959 info
->ifaces
[i
].name
[CTDB_IFACE_SIZE
] = '\0';
2961 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
2962 i
+1, info
->ifaces
[i
].name
,
2963 info
->ifaces
[i
].link_state
?"up":"down",
2964 (unsigned int)info
->ifaces
[i
].references
,
2965 (i
==info
->active_idx
)?" (active)":"");
2968 talloc_free(tmp_ctx
);
2973 display interfaces status
2975 static int control_ifaces(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2977 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
2979 struct ctdb_control_get_ifaces
*ifaces
;
2982 /* read the public ip list from this node */
2983 ret
= ctdb_ctrl_get_ifaces(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &ifaces
);
2985 DEBUG(DEBUG_ERR
, ("Unable to get interfaces from node %u\n",
2987 talloc_free(tmp_ctx
);
2991 if (options
.machinereadable
){
2992 printf(":Name:LinkStatus:References:\n");
2994 printf("Interfaces on node %u\n", options
.pnn
);
2997 for (i
=0; i
<ifaces
->num
; i
++) {
2998 if (options
.machinereadable
){
2999 printf(":%s:%s:%u\n",
3000 ifaces
->ifaces
[i
].name
,
3001 ifaces
->ifaces
[i
].link_state
?"1":"0",
3002 (unsigned int)ifaces
->ifaces
[i
].references
);
3004 printf("name:%s link:%s references:%u\n",
3005 ifaces
->ifaces
[i
].name
,
3006 ifaces
->ifaces
[i
].link_state
?"up":"down",
3007 (unsigned int)ifaces
->ifaces
[i
].references
);
3011 talloc_free(tmp_ctx
);
3017 set link status of an interface
3019 static int control_setifacelink(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3022 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3023 struct ctdb_control_iface_info info
;
3031 if (strlen(argv
[0]) > CTDB_IFACE_SIZE
) {
3032 DEBUG(DEBUG_ERR
, ("interfaces name '%s' too long\n",
3034 talloc_free(tmp_ctx
);
3037 strcpy(info
.name
, argv
[0]);
3039 if (strcmp(argv
[1], "up") == 0) {
3040 info
.link_state
= 1;
3041 } else if (strcmp(argv
[1], "down") == 0) {
3042 info
.link_state
= 0;
3044 DEBUG(DEBUG_ERR
, ("link state invalid '%s' should be 'up' or 'down'\n",
3046 talloc_free(tmp_ctx
);
3050 /* read the public ip list from this node */
3051 ret
= ctdb_ctrl_set_iface_link(ctdb
, TIMELIMIT(), options
.pnn
,
3054 DEBUG(DEBUG_ERR
, ("Unable to set link state for interfaces %s node %u\n",
3055 argv
[0], options
.pnn
));
3056 talloc_free(tmp_ctx
);
3060 talloc_free(tmp_ctx
);
3065 display pid of a ctdb daemon
3067 static int control_getpid(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3072 ret
= ctdb_ctrl_getpid(ctdb
, TIMELIMIT(), options
.pnn
, &pid
);
3074 DEBUG(DEBUG_ERR
, ("Unable to get daemon pid from node %u\n", options
.pnn
));
3077 printf("Pid:%d\n", pid
);
3082 typedef bool update_flags_handler_t(struct ctdb_context
*ctdb
, void *data
);
3084 static int update_flags_and_ipreallocate(struct ctdb_context
*ctdb
,
3086 update_flags_handler_t handler
,
3091 struct ctdb_node_map
*nodemap
= NULL
;
3095 /* Check if the node is already in the desired state */
3096 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, ctdb
, &nodemap
);
3098 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from local node\n"));
3101 flag_is_set
= nodemap
->nodes
[options
.pnn
].flags
& flag
;
3102 if (set_flag
== flag_is_set
) {
3103 DEBUG(DEBUG_NOTICE
, ("Node %d is %s %s\n", options
.pnn
,
3104 (set_flag
? "already" : "not"), desc
));
3109 if (!handler(ctdb
, data
)) {
3110 DEBUG(DEBUG_WARNING
,
3111 ("Failed to send control to set state %s on node %u, try again\n",
3112 desc
, options
.pnn
));
3117 /* Read the nodemap and verify the change took effect.
3118 * Even if the above control/hanlder timed out then it
3119 * could still have worked!
3121 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
,
3124 DEBUG(DEBUG_WARNING
,
3125 ("Unable to get nodemap from local node, try again\n"));
3127 flag_is_set
= nodemap
->nodes
[options
.pnn
].flags
& flag
;
3128 } while (nodemap
== NULL
|| (set_flag
!= flag_is_set
));
3130 return ipreallocate(ctdb
);
3133 /* Administratively disable a node */
3134 static bool update_flags_disabled(struct ctdb_context
*ctdb
, void *data
)
3138 ret
= ctdb_ctrl_modflags(ctdb
, TIMELIMIT(), options
.pnn
,
3139 NODE_FLAGS_PERMANENTLY_DISABLED
, 0);
3143 static int control_disable(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3145 return update_flags_and_ipreallocate(ctdb
, NULL
,
3146 update_flags_disabled
,
3147 NODE_FLAGS_PERMANENTLY_DISABLED
,
3149 true /* set_flag*/);
3152 /* Administratively re-enable a node */
3153 static bool update_flags_not_disabled(struct ctdb_context
*ctdb
, void *data
)
3157 ret
= ctdb_ctrl_modflags(ctdb
, TIMELIMIT(), options
.pnn
,
3158 0, NODE_FLAGS_PERMANENTLY_DISABLED
);
3162 static int control_enable(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3164 return update_flags_and_ipreallocate(ctdb
, NULL
,
3165 update_flags_not_disabled
,
3166 NODE_FLAGS_PERMANENTLY_DISABLED
,
3168 false /* set_flag*/);
3172 static bool update_flags_stopped(struct ctdb_context
*ctdb
, void *data
)
3176 ret
= ctdb_ctrl_stop_node(ctdb
, TIMELIMIT(), options
.pnn
);
3181 static int control_stop(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3183 return update_flags_and_ipreallocate(ctdb
, NULL
,
3184 update_flags_stopped
,
3187 true /* set_flag*/);
3190 /* Continue a stopped node */
3191 static bool update_flags_not_stopped(struct ctdb_context
*ctdb
, void *data
)
3195 ret
= ctdb_ctrl_continue_node(ctdb
, TIMELIMIT(), options
.pnn
);
3200 static int control_continue(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3202 return update_flags_and_ipreallocate(ctdb
, NULL
,
3203 update_flags_not_stopped
,
3206 false /* set_flag */);
3209 static uint32_t get_generation(struct ctdb_context
*ctdb
)
3211 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3212 struct ctdb_vnn_map
*vnnmap
=NULL
;
3214 uint32_t generation
;
3216 /* wait until the recmaster is not in recovery mode */
3218 uint32_t recmode
, recmaster
;
3220 if (vnnmap
!= NULL
) {
3221 talloc_free(vnnmap
);
3225 /* get the recmaster */
3226 ret
= ctdb_ctrl_getrecmaster(ctdb
, tmp_ctx
, TIMELIMIT(), CTDB_CURRENT_NODE
, &recmaster
);
3228 DEBUG(DEBUG_ERR
, ("Unable to get recmaster from node %u\n", options
.pnn
));
3229 talloc_free(tmp_ctx
);
3233 /* get recovery mode */
3234 ret
= ctdb_ctrl_getrecmode(ctdb
, tmp_ctx
, TIMELIMIT(), recmaster
, &recmode
);
3236 DEBUG(DEBUG_ERR
, ("Unable to get recmode from node %u\n", options
.pnn
));
3237 talloc_free(tmp_ctx
);
3241 /* get the current generation number */
3242 ret
= ctdb_ctrl_getvnnmap(ctdb
, TIMELIMIT(), recmaster
, tmp_ctx
, &vnnmap
);
3244 DEBUG(DEBUG_ERR
, ("Unable to get vnnmap from recmaster (%u)\n", recmaster
));
3245 talloc_free(tmp_ctx
);
3249 if ((recmode
== CTDB_RECOVERY_NORMAL
) && (vnnmap
->generation
!= 1)) {
3250 generation
= vnnmap
->generation
;
3251 talloc_free(tmp_ctx
);
3259 static bool update_state_banned(struct ctdb_context
*ctdb
, void *data
)
3261 struct ctdb_ban_time
*bantime
= (struct ctdb_ban_time
*)data
;
3264 ret
= ctdb_ctrl_set_ban(ctdb
, TIMELIMIT(), options
.pnn
, bantime
);
3269 static int control_ban(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3271 struct ctdb_ban_time bantime
;
3277 bantime
.pnn
= options
.pnn
;
3278 bantime
.time
= strtoul(argv
[0], NULL
, 0);
3280 if (bantime
.time
== 0) {
3281 DEBUG(DEBUG_ERR
, ("Invalid ban time specified - must be >0\n"));
3285 return update_flags_and_ipreallocate(ctdb
, &bantime
,
3286 update_state_banned
,
3289 true /* set_flag*/);
3294 static int control_unban(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3296 struct ctdb_ban_time bantime
;
3298 bantime
.pnn
= options
.pnn
;
3301 return update_flags_and_ipreallocate(ctdb
, &bantime
,
3302 update_state_banned
,
3305 false /* set_flag*/);
3309 show ban information for a node
3311 static int control_showban(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3314 struct ctdb_node_map
*nodemap
=NULL
;
3315 struct ctdb_ban_time
*bantime
;
3317 /* verify the node exists */
3318 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, ctdb
, &nodemap
);
3320 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from local node\n"));
3324 ret
= ctdb_ctrl_get_ban(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &bantime
);
3326 DEBUG(DEBUG_ERR
,("Showing ban info for node %d failed.\n", options
.pnn
));
3330 if (bantime
->time
== 0) {
3331 printf("Node %u is not banned\n", bantime
->pnn
);
3333 printf("Node %u is banned, %d seconds remaining\n",
3334 bantime
->pnn
, bantime
->time
);
3343 static int control_shutdown(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3347 ret
= ctdb_ctrl_shutdown(ctdb
, TIMELIMIT(), options
.pnn
);
3349 DEBUG(DEBUG_ERR
, ("Unable to shutdown node %u\n", options
.pnn
));
3359 static int control_recover(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3362 uint32_t generation
, next_generation
;
3365 /* "force" option ignores freeze failure and forces recovery */
3366 force
= (argc
== 1) && (strcasecmp(argv
[0], "force") == 0);
3368 /* record the current generation number */
3369 generation
= get_generation(ctdb
);
3371 ret
= ctdb_ctrl_freeze_priority(ctdb
, TIMELIMIT(), options
.pnn
, 1);
3374 DEBUG(DEBUG_ERR
, ("Unable to freeze node\n"));
3377 DEBUG(DEBUG_WARNING
, ("Unable to freeze node but proceeding because \"force\" option given\n"));
3380 ret
= ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
3382 DEBUG(DEBUG_ERR
, ("Unable to set recovery mode\n"));
3386 /* wait until we are in a new generation */
3388 next_generation
= get_generation(ctdb
);
3389 if (next_generation
!= generation
) {
3400 display monitoring mode of a remote node
3402 static int control_getmonmode(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3407 ret
= ctdb_ctrl_getmonmode(ctdb
, TIMELIMIT(), options
.pnn
, &monmode
);
3409 DEBUG(DEBUG_ERR
, ("Unable to get monmode from node %u\n", options
.pnn
));
3412 if (!options
.machinereadable
){
3413 printf("Monitoring mode:%s (%d)\n",monmode
==CTDB_MONITORING_ACTIVE
?"ACTIVE":"DISABLED",monmode
);
3416 printf(":%d:\n",monmode
);
3423 display capabilities of a remote node
3425 static int control_getcapabilities(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3427 uint32_t capabilities
;
3430 ret
= ctdb_ctrl_getcapabilities(ctdb
, TIMELIMIT(), options
.pnn
, &capabilities
);
3432 DEBUG(DEBUG_ERR
, ("Unable to get capabilities from node %u\n", options
.pnn
));
3436 if (!options
.machinereadable
){
3437 printf("RECMASTER: %s\n", (capabilities
&CTDB_CAP_RECMASTER
)?"YES":"NO");
3438 printf("LMASTER: %s\n", (capabilities
&CTDB_CAP_LMASTER
)?"YES":"NO");
3439 printf("LVS: %s\n", (capabilities
&CTDB_CAP_LVS
)?"YES":"NO");
3440 printf("NATGW: %s\n", (capabilities
&CTDB_CAP_NATGW
)?"YES":"NO");
3442 printf(":RECMASTER:LMASTER:LVS:NATGW:\n");
3443 printf(":%d:%d:%d:%d:\n",
3444 !!(capabilities
&CTDB_CAP_RECMASTER
),
3445 !!(capabilities
&CTDB_CAP_LMASTER
),
3446 !!(capabilities
&CTDB_CAP_LVS
),
3447 !!(capabilities
&CTDB_CAP_NATGW
));
3453 display lvs configuration
3455 static int control_lvs(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3457 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3458 uint32_t *capabilities
;
3459 struct ctdb_node_map
*nodemap
=NULL
;
3461 int healthy_count
= 0;
3463 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &nodemap
);
3465 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
3466 talloc_free(tmp_ctx
);
3470 capabilities
= talloc_array(ctdb
, uint32_t, nodemap
->num
);
3471 CTDB_NO_MEMORY(ctdb
, capabilities
);
3475 /* collect capabilities for all connected nodes */
3476 for (i
=0; i
<nodemap
->num
; i
++) {
3477 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_INACTIVE
) {
3480 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_PERMANENTLY_DISABLED
) {
3484 ret
= ctdb_ctrl_getcapabilities(ctdb
, TIMELIMIT(), i
, &capabilities
[i
]);
3486 DEBUG(DEBUG_ERR
, ("Unable to get capabilities from node %u\n", i
));
3491 if (!(capabilities
[i
] & CTDB_CAP_LVS
)) {
3495 if (!(nodemap
->nodes
[i
].flags
& NODE_FLAGS_UNHEALTHY
)) {
3500 /* Print all LVS nodes */
3501 for (i
=0; i
<nodemap
->num
; i
++) {
3502 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_INACTIVE
) {
3505 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_PERMANENTLY_DISABLED
) {
3508 if (!(capabilities
[i
] & CTDB_CAP_LVS
)) {
3512 if (healthy_count
!= 0) {
3513 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_UNHEALTHY
) {
3518 printf("%d:%s\n", i
,
3519 ctdb_addr_to_str(&nodemap
->nodes
[i
].addr
));
3523 talloc_free(tmp_ctx
);
3528 display who is the lvs master
3530 static int control_lvsmaster(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3532 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3533 uint32_t *capabilities
;
3534 struct ctdb_node_map
*nodemap
=NULL
;
3536 int healthy_count
= 0;
3538 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &nodemap
);
3540 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
3541 talloc_free(tmp_ctx
);
3545 capabilities
= talloc_array(tmp_ctx
, uint32_t, nodemap
->num
);
3546 if (capabilities
== NULL
) {
3547 talloc_free(tmp_ctx
);
3548 CTDB_NO_MEMORY(ctdb
, capabilities
);
3551 /* collect capabilities for all connected nodes */
3552 for (i
=0; i
<nodemap
->num
; i
++) {
3553 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_INACTIVE
) {
3556 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_PERMANENTLY_DISABLED
) {
3560 ret
= ctdb_ctrl_getcapabilities(ctdb
, TIMELIMIT(), i
, &capabilities
[i
]);
3562 DEBUG(DEBUG_ERR
, ("Unable to get capabilities from node %u\n", i
));
3567 if (!(capabilities
[i
] & CTDB_CAP_LVS
)) {
3571 if (!(nodemap
->nodes
[i
].flags
& NODE_FLAGS_UNHEALTHY
)) {
3578 /* find and show the lvsmaster */
3579 for (i
=0; i
<nodemap
->num
; i
++) {
3580 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_INACTIVE
) {
3583 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_PERMANENTLY_DISABLED
) {
3586 if (!(capabilities
[i
] & CTDB_CAP_LVS
)) {
3590 if (healthy_count
!= 0) {
3591 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_UNHEALTHY
) {
3596 if (options
.machinereadable
){
3599 printf("Node %d is LVS master\n", i
);
3605 printf("There is no LVS master\n");
3607 talloc_free(tmp_ctx
);
3612 disable monitoring on a node
3614 static int control_disable_monmode(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3619 ret
= ctdb_ctrl_disable_monmode(ctdb
, TIMELIMIT(), options
.pnn
);
3621 DEBUG(DEBUG_ERR
, ("Unable to disable monmode on node %u\n", options
.pnn
));
3624 printf("Monitoring mode:%s\n","DISABLED");
3630 enable monitoring on a node
3632 static int control_enable_monmode(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3637 ret
= ctdb_ctrl_enable_monmode(ctdb
, TIMELIMIT(), options
.pnn
);
3639 DEBUG(DEBUG_ERR
, ("Unable to enable monmode on node %u\n", options
.pnn
));
3642 printf("Monitoring mode:%s\n","ACTIVE");
3648 display remote list of keys/data for a db
3650 static int control_catdb(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3652 const char *db_name
;
3653 struct ctdb_db_context
*ctdb_db
;
3655 struct ctdb_dump_db_context c
;
3664 if (!db_exists(ctdb
, db_name
, NULL
, &flags
)) {
3668 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, flags
& CTDB_DB_FLAGS_PERSISTENT
, 0);
3669 if (ctdb_db
== NULL
) {
3670 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
3674 if (options
.printlmaster
) {
3675 ret
= ctdb_ctrl_getvnnmap(ctdb
, TIMELIMIT(), options
.pnn
,
3676 ctdb
, &ctdb
->vnn_map
);
3678 DEBUG(DEBUG_ERR
, ("Unable to get vnnmap from node %u\n",
3686 c
.printemptyrecords
= (bool)options
.printemptyrecords
;
3687 c
.printdatasize
= (bool)options
.printdatasize
;
3688 c
.printlmaster
= (bool)options
.printlmaster
;
3689 c
.printhash
= (bool)options
.printhash
;
3690 c
.printrecordflags
= (bool)options
.printrecordflags
;
3692 /* traverse and dump the cluster tdb */
3693 ret
= ctdb_dump_db(ctdb_db
, &c
);
3695 DEBUG(DEBUG_ERR
, ("Unable to dump database\n"));
3696 DEBUG(DEBUG_ERR
, ("Maybe try 'ctdb getdbstatus %s'"
3697 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3701 talloc_free(ctdb_db
);
3703 printf("Dumped %d records\n", ret
);
3707 struct cattdb_data
{
3708 struct ctdb_context
*ctdb
;
3712 static int cattdb_traverse(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA data
, void *private_data
)
3714 struct cattdb_data
*d
= private_data
;
3715 struct ctdb_dump_db_context c
;
3721 c
.printemptyrecords
= (bool)options
.printemptyrecords
;
3722 c
.printdatasize
= (bool)options
.printdatasize
;
3723 c
.printlmaster
= false;
3724 c
.printhash
= (bool)options
.printhash
;
3725 c
.printrecordflags
= true;
3727 return ctdb_dumpdb_record(d
->ctdb
, key
, data
, &c
);
3731 cat the local tdb database using same format as catdb
3733 static int control_cattdb(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3735 const char *db_name
;
3736 struct ctdb_db_context
*ctdb_db
;
3737 struct cattdb_data d
;
3746 if (!db_exists(ctdb
, db_name
, NULL
, &flags
)) {
3750 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, flags
& CTDB_DB_FLAGS_PERSISTENT
, 0);
3751 if (ctdb_db
== NULL
) {
3752 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
3756 /* traverse the local tdb */
3759 if (tdb_traverse_read(ctdb_db
->ltdb
->tdb
, cattdb_traverse
, &d
) == -1) {
3760 printf("Failed to cattdb data\n");
3763 talloc_free(ctdb_db
);
3765 printf("Dumped %d records\n", d
.count
);
3770 display the content of a database key
3772 static int control_readkey(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3774 const char *db_name
;
3775 struct ctdb_db_context
*ctdb_db
;
3776 struct ctdb_record_handle
*h
;
3777 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3787 if (!db_exists(ctdb
, db_name
, NULL
, &flags
)) {
3791 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, flags
& CTDB_DB_FLAGS_PERSISTENT
, 0);
3792 if (ctdb_db
== NULL
) {
3793 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
3797 key
.dptr
= discard_const(argv
[1]);
3798 key
.dsize
= strlen((char *)key
.dptr
);
3800 h
= ctdb_fetch_lock(ctdb_db
, tmp_ctx
, key
, &data
);
3802 printf("Failed to fetch record '%s' on node %d\n",
3803 (const char *)key
.dptr
, ctdb_get_pnn(ctdb
));
3804 talloc_free(tmp_ctx
);
3808 printf("Data: size:%d ptr:[%.*s]\n", (int)data
.dsize
, (int)data
.dsize
, data
.dptr
);
3810 talloc_free(ctdb_db
);
3811 talloc_free(tmp_ctx
);
3816 display the content of a database key
3818 static int control_writekey(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3820 const char *db_name
;
3821 struct ctdb_db_context
*ctdb_db
;
3822 struct ctdb_record_handle
*h
;
3823 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3833 if (!db_exists(ctdb
, db_name
, NULL
, &flags
)) {
3837 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, flags
& CTDB_DB_FLAGS_PERSISTENT
, 0);
3838 if (ctdb_db
== NULL
) {
3839 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
3843 key
.dptr
= discard_const(argv
[1]);
3844 key
.dsize
= strlen((char *)key
.dptr
);
3846 h
= ctdb_fetch_lock(ctdb_db
, tmp_ctx
, key
, &data
);
3848 printf("Failed to fetch record '%s' on node %d\n",
3849 (const char *)key
.dptr
, ctdb_get_pnn(ctdb
));
3850 talloc_free(tmp_ctx
);
3854 data
.dptr
= discard_const(argv
[2]);
3855 data
.dsize
= strlen((char *)data
.dptr
);
3857 if (ctdb_record_store(h
, data
) != 0) {
3858 printf("Failed to store record\n");
3862 talloc_free(ctdb_db
);
3863 talloc_free(tmp_ctx
);
3868 fetch a record from a persistent database
3870 static int control_pfetch(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3872 const char *db_name
;
3873 struct ctdb_db_context
*ctdb_db
;
3874 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3875 struct ctdb_transaction_handle
*h
;
3882 talloc_free(tmp_ctx
);
3888 if (!db_exists(ctdb
, db_name
, NULL
, &flags
)) {
3889 talloc_free(tmp_ctx
);
3893 persistent
= flags
& CTDB_DB_FLAGS_PERSISTENT
;
3895 DEBUG(DEBUG_ERR
,("Database '%s' is not persistent\n", db_name
));
3896 talloc_free(tmp_ctx
);
3900 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, persistent
, 0);
3901 if (ctdb_db
== NULL
) {
3902 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
3903 talloc_free(tmp_ctx
);
3907 h
= ctdb_transaction_start(ctdb_db
, tmp_ctx
);
3909 DEBUG(DEBUG_ERR
,("Failed to start transaction on database %s\n", db_name
));
3910 talloc_free(tmp_ctx
);
3914 key
.dptr
= discard_const(argv
[1]);
3915 key
.dsize
= strlen(argv
[1]);
3916 ret
= ctdb_transaction_fetch(h
, tmp_ctx
, key
, &data
);
3918 DEBUG(DEBUG_ERR
,("Failed to fetch record\n"));
3919 talloc_free(tmp_ctx
);
3923 if (data
.dsize
== 0 || data
.dptr
== NULL
) {
3924 DEBUG(DEBUG_ERR
,("Record is empty\n"));
3925 talloc_free(tmp_ctx
);
3930 fd
= open(argv
[2], O_WRONLY
|O_CREAT
|O_TRUNC
, 0600);
3932 DEBUG(DEBUG_ERR
,("Failed to open output file %s\n", argv
[2]));
3933 talloc_free(tmp_ctx
);
3936 write(fd
, data
.dptr
, data
.dsize
);
3939 write(1, data
.dptr
, data
.dsize
);
3942 /* abort the transaction */
3946 talloc_free(tmp_ctx
);
3951 fetch a record from a tdb-file
3953 static int control_tfetch(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3955 const char *tdb_file
;
3958 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
3967 tdb
= tdb_open(tdb_file
, 0, 0, O_RDONLY
, 0);
3969 printf("Failed to open TDB file %s\n", tdb_file
);
3973 if (!strncmp(argv
[1], "0x", 2)) {
3974 key
= hextodata(tmp_ctx
, argv
[1] + 2);
3975 if (key
.dsize
== 0) {
3976 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv
[1]);
3980 key
.dptr
= discard_const(argv
[1]);
3981 key
.dsize
= strlen(argv
[1]);
3984 data
= tdb_fetch(tdb
, key
);
3985 if (data
.dptr
== NULL
|| data
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
3986 printf("Failed to read record %s from tdb %s\n", argv
[1], tdb_file
);
3994 fd
= open(argv
[2], O_WRONLY
|O_CREAT
|O_TRUNC
, 0600);
3996 printf("Failed to open output file %s\n", argv
[2]);
3999 if (options
.verbose
){
4000 write(fd
, data
.dptr
, data
.dsize
);
4002 write(fd
, data
.dptr
+sizeof(struct ctdb_ltdb_header
), data
.dsize
-sizeof(struct ctdb_ltdb_header
));
4006 if (options
.verbose
){
4007 write(1, data
.dptr
, data
.dsize
);
4009 write(1, data
.dptr
+sizeof(struct ctdb_ltdb_header
), data
.dsize
-sizeof(struct ctdb_ltdb_header
));
4013 talloc_free(tmp_ctx
);
4018 store a record and header to a tdb-file
4020 static int control_tstore(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4022 const char *tdb_file
;
4025 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
4033 tdb
= tdb_open(tdb_file
, 0, 0, O_RDWR
, 0);
4035 printf("Failed to open TDB file %s\n", tdb_file
);
4039 if (!strncmp(argv
[1], "0x", 2)) {
4040 key
= hextodata(tmp_ctx
, argv
[1] + 2);
4041 if (key
.dsize
== 0) {
4042 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv
[1]);
4046 key
.dptr
= discard_const(argv
[1]);
4047 key
.dsize
= strlen(argv
[1]);
4050 if (!strncmp(argv
[2], "0x", 2)) {
4051 data
= hextodata(tmp_ctx
, argv
[2] + 2);
4052 if (data
.dsize
== 0) {
4053 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv
[2]);
4057 data
.dptr
= discard_const(argv
[2]);
4058 data
.dsize
= strlen(argv
[2]);
4061 if (data
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
4062 printf("Not enough data. You must specify the full ctdb_ltdb_header too when storing\n");
4065 if (tdb_store(tdb
, key
, data
, TDB_REPLACE
) != 0) {
4066 printf("Failed to write record %s to tdb %s\n", argv
[1], tdb_file
);
4073 talloc_free(tmp_ctx
);
4078 write a record to a persistent database
4080 static int control_pstore(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4082 const char *db_name
;
4083 struct ctdb_db_context
*ctdb_db
;
4084 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
4085 struct ctdb_transaction_handle
*h
;
4091 talloc_free(tmp_ctx
);
4095 fd
= open(argv
[2], O_RDONLY
);
4097 DEBUG(DEBUG_ERR
,("Failed to open file containing record data : %s %s\n", argv
[2], strerror(errno
)));
4098 talloc_free(tmp_ctx
);
4102 ret
= fstat(fd
, &st
);
4104 DEBUG(DEBUG_ERR
,("fstat of file %s failed: %s\n", argv
[2], strerror(errno
)));
4106 talloc_free(tmp_ctx
);
4110 if (!S_ISREG(st
.st_mode
)) {
4111 DEBUG(DEBUG_ERR
,("Not a regular file %s\n", argv
[2]));
4113 talloc_free(tmp_ctx
);
4117 data
.dsize
= st
.st_size
;
4118 if (data
.dsize
== 0) {
4121 data
.dptr
= talloc_size(tmp_ctx
, data
.dsize
);
4122 if (data
.dptr
== NULL
) {
4123 DEBUG(DEBUG_ERR
,("Failed to talloc %d of memory to store record data\n", (int)data
.dsize
));
4125 talloc_free(tmp_ctx
);
4128 ret
= read(fd
, data
.dptr
, data
.dsize
);
4129 if (ret
!= data
.dsize
) {
4130 DEBUG(DEBUG_ERR
,("Failed to read %d bytes of record data\n", (int)data
.dsize
));
4132 talloc_free(tmp_ctx
);
4141 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, true, 0);
4142 if (ctdb_db
== NULL
) {
4143 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
4144 talloc_free(tmp_ctx
);
4148 h
= ctdb_transaction_start(ctdb_db
, tmp_ctx
);
4150 DEBUG(DEBUG_ERR
,("Failed to start transaction on database %s\n", db_name
));
4151 talloc_free(tmp_ctx
);
4155 key
.dptr
= discard_const(argv
[1]);
4156 key
.dsize
= strlen(argv
[1]);
4157 ret
= ctdb_transaction_store(h
, key
, data
);
4159 DEBUG(DEBUG_ERR
,("Failed to store record\n"));
4160 talloc_free(tmp_ctx
);
4164 ret
= ctdb_transaction_commit(h
);
4166 DEBUG(DEBUG_ERR
,("Failed to commit transaction\n"));
4167 talloc_free(tmp_ctx
);
4172 talloc_free(tmp_ctx
);
4177 * delete a record from a persistent database
4179 static int control_pdelete(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4181 const char *db_name
;
4182 struct ctdb_db_context
*ctdb_db
;
4183 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
4184 struct ctdb_transaction_handle
*h
;
4191 talloc_free(tmp_ctx
);
4197 if (!db_exists(ctdb
, db_name
, NULL
, &flags
)) {
4198 talloc_free(tmp_ctx
);
4202 persistent
= flags
& CTDB_DB_FLAGS_PERSISTENT
;
4204 DEBUG(DEBUG_ERR
, ("Database '%s' is not persistent\n", db_name
));
4205 talloc_free(tmp_ctx
);
4209 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, persistent
, 0);
4210 if (ctdb_db
== NULL
) {
4211 DEBUG(DEBUG_ERR
, ("Unable to attach to database '%s'\n", db_name
));
4212 talloc_free(tmp_ctx
);
4216 h
= ctdb_transaction_start(ctdb_db
, tmp_ctx
);
4218 DEBUG(DEBUG_ERR
, ("Failed to start transaction on database %s\n", db_name
));
4219 talloc_free(tmp_ctx
);
4223 key
.dptr
= discard_const(argv
[1]);
4224 key
.dsize
= strlen(argv
[1]);
4225 ret
= ctdb_transaction_store(h
, key
, tdb_null
);
4227 DEBUG(DEBUG_ERR
, ("Failed to delete record\n"));
4228 talloc_free(tmp_ctx
);
4232 ret
= ctdb_transaction_commit(h
);
4234 DEBUG(DEBUG_ERR
, ("Failed to commit transaction\n"));
4235 talloc_free(tmp_ctx
);
4239 talloc_free(tmp_ctx
);
4244 check if a service is bound to a port or not
4246 static int control_chktcpport(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4251 struct sockaddr_in sin
;
4254 printf("Use: ctdb chktcport <port>\n");
4258 port
= atoi(argv
[0]);
4260 s
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
4262 printf("Failed to open local socket\n");
4266 v
= fcntl(s
, F_GETFL
, 0);
4267 fcntl(s
, F_SETFL
, v
| O_NONBLOCK
);
4269 bzero(&sin
, sizeof(sin
));
4270 sin
.sin_family
= PF_INET
;
4271 sin
.sin_port
= htons(port
);
4272 ret
= bind(s
, (struct sockaddr
*)&sin
, sizeof(sin
));
4275 printf("Failed to bind to local socket: %d %s\n", errno
, strerror(errno
));
4284 static void log_handler(struct ctdb_context
*ctdb
, uint64_t srvid
,
4285 TDB_DATA data
, void *private_data
)
4287 DEBUG(DEBUG_ERR
,("Log data received\n"));
4288 if (data
.dsize
> 0) {
4289 printf("%s", data
.dptr
);
4296 display a list of log messages from the in memory ringbuffer
4298 static int control_getlog(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4302 struct ctdb_get_log_addr log_addr
;
4306 /* Process options */
4308 log_addr
.pnn
= ctdb_get_pnn(ctdb
);
4309 log_addr
.level
= DEBUG_NOTICE
;
4310 for (i
= 0; i
< argc
; i
++) {
4311 if (strcmp(argv
[i
], "recoverd") == 0) {
4312 main_daemon
= false;
4314 if (isalpha(argv
[i
][0]) || argv
[i
][0] == '-') {
4315 log_addr
.level
= get_debug_by_desc(argv
[i
]);
4317 log_addr
.level
= strtol(argv
[i
], NULL
, 0);
4322 /* Our message port is our PID */
4323 log_addr
.srvid
= getpid();
4325 data
.dptr
= (unsigned char *)&log_addr
;
4326 data
.dsize
= sizeof(log_addr
);
4328 DEBUG(DEBUG_ERR
, ("Pulling logs from node %u\n", options
.pnn
));
4330 ctdb_client_set_message_handler(ctdb
, log_addr
.srvid
, log_handler
, NULL
);
4333 DEBUG(DEBUG_ERR
,("Listen for response on %d\n", (int)log_addr
.srvid
));
4338 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
4340 ret
= ctdb_control(ctdb
, options
.pnn
, 0, CTDB_CONTROL_GET_LOG
,
4341 0, data
, tmp_ctx
, NULL
, &res
, NULL
, &errmsg
);
4342 if (ret
!= 0 || res
!= 0) {
4343 DEBUG(DEBUG_ERR
,("Failed to get logs - %s\n", errmsg
));
4344 talloc_free(tmp_ctx
);
4347 talloc_free(tmp_ctx
);
4349 ret
= ctdb_client_send_message(ctdb
, options
.pnn
,
4350 CTDB_SRVID_GETLOG
, data
);
4352 DEBUG(DEBUG_ERR
,("Failed to send getlog request message to %u\n", options
.pnn
));
4357 tv
= timeval_current();
4358 /* this loop will terminate when we have received the reply */
4359 while (timeval_elapsed(&tv
) < (double)options
.timelimit
) {
4360 event_loop_once(ctdb
->ev
);
4363 DEBUG(DEBUG_INFO
,("Timed out waiting for log data.\n"));
4369 clear the in memory log area
4371 static int control_clearlog(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4375 if (argc
== 0 || (argc
>= 1 && strcmp(argv
[0], "recoverd") != 0)) {
4376 /* "recoverd" not given - get logs from main daemon */
4379 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
4381 ret
= ctdb_control(ctdb
, options
.pnn
, 0, CTDB_CONTROL_CLEAR_LOG
,
4382 0, tdb_null
, tmp_ctx
, NULL
, &res
, NULL
, &errmsg
);
4383 if (ret
!= 0 || res
!= 0) {
4384 DEBUG(DEBUG_ERR
,("Failed to clear logs\n"));
4385 talloc_free(tmp_ctx
);
4389 talloc_free(tmp_ctx
);
4391 TDB_DATA data
; /* unused in recoverd... */
4394 ret
= ctdb_client_send_message(ctdb
, options
.pnn
, CTDB_SRVID_CLEARLOG
, data
);
4396 DEBUG(DEBUG_ERR
,("Failed to send clearlog request message to %u\n", options
.pnn
));
4404 /* Reload public IPs on a specified nodes */
4405 static int control_reloadips(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4407 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
4412 assert_single_node_only();
4418 /* Determine the nodes where IPs need to be reloaded */
4419 if (!parse_nodestring(ctdb
, tmp_ctx
, argc
== 1 ? argv
[0] : NULL
,
4420 options
.pnn
, true, &nodes
, &pnn_mode
)) {
4426 /* Disable takeover runs on all connected nodes. A reply
4427 * indicating success is needed from each node so all nodes
4428 * will need to be active. This will retry until maxruntime
4429 * is exceeded, hence no error handling.
4431 * A check could be added to not allow reloading of IPs when
4432 * there are disconnected nodes. However, this should
4433 * probably be left up to the administrator.
4435 srvid_broadcast(ctdb
, CTDB_SRVID_DISABLE_TAKEOVER_RUNS
, LONGTIMEOUT
,
4436 "Disable takeover runs", true);
4438 /* Now tell all the desired nodes to reload their public IPs.
4439 * Keep trying this until it succeeds. This assumes all
4440 * failures are transient, which might not be true...
4442 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_RELOAD_PUBLIC_IPS
,
4443 nodes
, 0, LONGTIMELIMIT(),
4445 NULL
, NULL
, NULL
) != 0) {
4447 ("Unable to reload IPs on some nodes, try again.\n"));
4451 /* It isn't strictly necessary to wait until takeover runs are
4452 * re-enabled but doing so can't hurt.
4454 srvid_broadcast(ctdb
, CTDB_SRVID_DISABLE_TAKEOVER_RUNS
, 0,
4455 "Enable takeover runs", true);
4461 talloc_free(tmp_ctx
);
4466 display a list of the databases on a remote ctdb
4468 static int control_getdbmap(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4471 struct ctdb_dbid_map
*dbmap
=NULL
;
4473 ret
= ctdb_ctrl_getdbmap(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &dbmap
);
4475 DEBUG(DEBUG_ERR
, ("Unable to get dbids from node %u\n", options
.pnn
));
4479 if(options
.machinereadable
){
4480 printf(":ID:Name:Path:Persistent:Sticky:Unhealthy:ReadOnly:\n");
4481 for(i
=0;i
<dbmap
->num
;i
++){
4489 ctdb_ctrl_getdbpath(ctdb
, TIMELIMIT(), options
.pnn
,
4490 dbmap
->dbs
[i
].dbid
, ctdb
, &path
);
4491 ctdb_ctrl_getdbname(ctdb
, TIMELIMIT(), options
.pnn
,
4492 dbmap
->dbs
[i
].dbid
, ctdb
, &name
);
4493 ctdb_ctrl_getdbhealth(ctdb
, TIMELIMIT(), options
.pnn
,
4494 dbmap
->dbs
[i
].dbid
, ctdb
, &health
);
4495 persistent
= dbmap
->dbs
[i
].flags
& CTDB_DB_FLAGS_PERSISTENT
;
4496 readonly
= dbmap
->dbs
[i
].flags
& CTDB_DB_FLAGS_READONLY
;
4497 sticky
= dbmap
->dbs
[i
].flags
& CTDB_DB_FLAGS_STICKY
;
4498 printf(":0x%08X:%s:%s:%d:%d:%d:%d:\n",
4499 dbmap
->dbs
[i
].dbid
, name
, path
,
4500 !!(persistent
), !!(sticky
),
4501 !!(health
), !!(readonly
));
4506 printf("Number of databases:%d\n", dbmap
->num
);
4507 for(i
=0;i
<dbmap
->num
;i
++){
4515 ctdb_ctrl_getdbpath(ctdb
, TIMELIMIT(), options
.pnn
, dbmap
->dbs
[i
].dbid
, ctdb
, &path
);
4516 ctdb_ctrl_getdbname(ctdb
, TIMELIMIT(), options
.pnn
, dbmap
->dbs
[i
].dbid
, ctdb
, &name
);
4517 ctdb_ctrl_getdbhealth(ctdb
, TIMELIMIT(), options
.pnn
, dbmap
->dbs
[i
].dbid
, ctdb
, &health
);
4518 persistent
= dbmap
->dbs
[i
].flags
& CTDB_DB_FLAGS_PERSISTENT
;
4519 readonly
= dbmap
->dbs
[i
].flags
& CTDB_DB_FLAGS_READONLY
;
4520 sticky
= dbmap
->dbs
[i
].flags
& CTDB_DB_FLAGS_STICKY
;
4521 printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
4522 dbmap
->dbs
[i
].dbid
, name
, path
,
4523 persistent
?" PERSISTENT":"",
4524 sticky
?" STICKY":"",
4525 readonly
?" READONLY":"",
4526 health
?" UNHEALTHY":"");
4533 display the status of a database on a remote ctdb
4535 static int control_getdbstatus(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4537 const char *db_name
;
4549 if (!db_exists(ctdb
, db_name
, &db_id
, &flags
)) {
4553 ctdb_ctrl_getdbpath(ctdb
, TIMELIMIT(), options
.pnn
, db_id
, ctdb
, &path
);
4554 ctdb_ctrl_getdbhealth(ctdb
, TIMELIMIT(), options
.pnn
, db_id
, ctdb
, &health
);
4555 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
4556 db_id
, db_name
, path
,
4557 (flags
& CTDB_DB_FLAGS_PERSISTENT
? "yes" : "no"),
4558 (flags
& CTDB_DB_FLAGS_STICKY
? "yes" : "no"),
4559 (flags
& CTDB_DB_FLAGS_READONLY
? "yes" : "no"),
4560 (health
? health
: "OK"));
4566 check if the local node is recmaster or not
4567 it will return 1 if this node is the recmaster and 0 if it is not
4568 or if the local ctdb daemon could not be contacted
4570 static int control_isnotrecmaster(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4572 uint32_t mypnn
, recmaster
;
4575 assert_single_node_only();
4577 mypnn
= getpnn(ctdb
);
4579 ret
= ctdb_ctrl_getrecmaster(ctdb
, ctdb
, TIMELIMIT(), options
.pnn
, &recmaster
);
4581 printf("Failed to get the recmaster\n");
4585 if (recmaster
!= mypnn
) {
4586 printf("this node is not the recmaster\n");
4590 printf("this node is the recmaster\n");
4597 static int control_ping(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4600 struct timeval tv
= timeval_current();
4601 ret
= ctdb_ctrl_ping(ctdb
, options
.pnn
);
4603 printf("Unable to get ping response from node %u\n", options
.pnn
);
4606 printf("response from %u time=%.6f sec (%d clients)\n",
4607 options
.pnn
, timeval_elapsed(&tv
), ret
);
4614 get a node's runstate
4616 static int control_runstate(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4619 enum ctdb_runstate runstate
;
4621 ret
= ctdb_ctrl_get_runstate(ctdb
, TIMELIMIT(), options
.pnn
, &runstate
);
4623 printf("Unable to get runstate response from node %u\n",
4628 enum ctdb_runstate t
;
4630 for (i
=0; i
<argc
; i
++) {
4632 t
= runstate_from_string(argv
[i
]);
4633 if (t
== CTDB_RUNSTATE_UNKNOWN
) {
4634 printf("Invalid run state (%s)\n", argv
[i
]);
4638 if (t
== runstate
) {
4645 printf("CTDB not in required run state (got %s)\n",
4646 runstate_to_string((enum ctdb_runstate
)runstate
));
4651 printf("%s\n", runstate_to_string(runstate
));
4659 static int control_getvar(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4670 ret
= ctdb_ctrl_get_tunable(ctdb
, TIMELIMIT(), options
.pnn
, name
, &value
);
4672 DEBUG(DEBUG_ERR
, ("Unable to get tunable variable '%s'\n", name
));
4676 printf("%-23s = %u\n", name
, value
);
4683 static int control_setvar(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4694 value
= strtoul(argv
[1], NULL
, 0);
4696 ret
= ctdb_ctrl_set_tunable(ctdb
, TIMELIMIT(), options
.pnn
, name
, value
);
4698 DEBUG(DEBUG_ERR
, ("Unable to set tunable variable '%s'\n", name
));
4707 static int control_listvars(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4713 ret
= ctdb_ctrl_list_tunables(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &list
, &count
);
4715 DEBUG(DEBUG_ERR
, ("Unable to list tunable variables\n"));
4719 for (i
=0;i
<count
;i
++) {
4720 control_getvar(ctdb
, 1, &list
[i
]);
4729 display debug level on a node
4731 static int control_getdebug(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4736 ret
= ctdb_ctrl_get_debuglevel(ctdb
, options
.pnn
, &level
);
4738 DEBUG(DEBUG_ERR
, ("Unable to get debuglevel response from node %u\n", options
.pnn
));
4741 if (options
.machinereadable
){
4742 printf(":Name:Level:\n");
4743 printf(":%s:%d:\n",get_debug_by_level(level
),level
);
4745 printf("Node %u is at debug level %s (%d)\n", options
.pnn
, get_debug_by_level(level
), level
);
4752 display reclock file of a node
4754 static int control_getreclock(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4757 const char *reclock
;
4759 ret
= ctdb_ctrl_getreclock(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &reclock
);
4761 DEBUG(DEBUG_ERR
, ("Unable to get reclock file from node %u\n", options
.pnn
));
4764 if (options
.machinereadable
){
4765 if (reclock
!= NULL
) {
4766 printf("%s", reclock
);
4769 if (reclock
== NULL
) {
4770 printf("No reclock file used.\n");
4772 printf("Reclock file:%s\n", reclock
);
4780 set the reclock file of a node
4782 static int control_setreclock(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4785 const char *reclock
;
4789 } else if (argc
== 1) {
4795 ret
= ctdb_ctrl_setreclock(ctdb
, TIMELIMIT(), options
.pnn
, reclock
);
4797 DEBUG(DEBUG_ERR
, ("Unable to get reclock file from node %u\n", options
.pnn
));
4804 set the natgw state on/off
4806 static int control_setnatgwstate(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4809 uint32_t natgwstate
;
4815 if (!strcmp(argv
[0], "on")) {
4817 } else if (!strcmp(argv
[0], "off")) {
4823 ret
= ctdb_ctrl_setnatgwstate(ctdb
, TIMELIMIT(), options
.pnn
, natgwstate
);
4825 DEBUG(DEBUG_ERR
, ("Unable to set the natgw state for node %u\n", options
.pnn
));
4833 set the lmaster role on/off
4835 static int control_setlmasterrole(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4838 uint32_t lmasterrole
;
4844 if (!strcmp(argv
[0], "on")) {
4846 } else if (!strcmp(argv
[0], "off")) {
4852 ret
= ctdb_ctrl_setlmasterrole(ctdb
, TIMELIMIT(), options
.pnn
, lmasterrole
);
4854 DEBUG(DEBUG_ERR
, ("Unable to set the lmaster role for node %u\n", options
.pnn
));
4862 set the recmaster role on/off
4864 static int control_setrecmasterrole(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4867 uint32_t recmasterrole
;
4873 if (!strcmp(argv
[0], "on")) {
4875 } else if (!strcmp(argv
[0], "off")) {
4881 ret
= ctdb_ctrl_setrecmasterrole(ctdb
, TIMELIMIT(), options
.pnn
, recmasterrole
);
4883 DEBUG(DEBUG_ERR
, ("Unable to set the recmaster role for node %u\n", options
.pnn
));
4891 set debug level on a node or all nodes
4893 static int control_setdebug(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4899 printf("You must specify the debug level. Valid levels are:\n");
4900 for (i
=0; debug_levels
[i
].description
!= NULL
; i
++) {
4901 printf("%s (%d)\n", debug_levels
[i
].description
, debug_levels
[i
].level
);
4907 if (isalpha(argv
[0][0]) || argv
[0][0] == '-') {
4908 level
= get_debug_by_desc(argv
[0]);
4910 level
= strtol(argv
[0], NULL
, 0);
4913 for (i
=0; debug_levels
[i
].description
!= NULL
; i
++) {
4914 if (level
== debug_levels
[i
].level
) {
4918 if (debug_levels
[i
].description
== NULL
) {
4919 printf("Invalid debug level, must be one of\n");
4920 for (i
=0; debug_levels
[i
].description
!= NULL
; i
++) {
4921 printf("%s (%d)\n", debug_levels
[i
].description
, debug_levels
[i
].level
);
4926 ret
= ctdb_ctrl_set_debuglevel(ctdb
, options
.pnn
, level
);
4928 DEBUG(DEBUG_ERR
, ("Unable to set debug level on node %u\n", options
.pnn
));
4937 static int control_thaw(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4943 priority
= strtol(argv
[0], NULL
, 0);
4947 DEBUG(DEBUG_ERR
,("Thaw by priority %u\n", priority
));
4949 ret
= ctdb_ctrl_thaw_priority(ctdb
, TIMELIMIT(), options
.pnn
, priority
);
4951 DEBUG(DEBUG_ERR
, ("Unable to thaw node %u\n", options
.pnn
));
4958 attach to a database
4960 static int control_attach(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4962 const char *db_name
;
4963 struct ctdb_db_context
*ctdb_db
;
4964 bool persistent
= false;
4974 if (strcmp(argv
[1], "persistent") != 0) {
4980 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, persistent
, 0);
4981 if (ctdb_db
== NULL
) {
4982 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
4992 static int control_setdbprio(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4994 struct ctdb_db_priority db_prio
;
5001 db_prio
.db_id
= strtoul(argv
[0], NULL
, 0);
5002 db_prio
.priority
= strtoul(argv
[1], NULL
, 0);
5004 ret
= ctdb_ctrl_set_db_priority(ctdb
, TIMELIMIT(), options
.pnn
, &db_prio
);
5006 DEBUG(DEBUG_ERR
,("Unable to set db prio\n"));
5016 static int control_getdbprio(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5018 uint32_t db_id
, priority
;
5025 if (!db_exists(ctdb
, argv
[0], &db_id
, NULL
)) {
5029 ret
= ctdb_ctrl_get_db_priority(ctdb
, TIMELIMIT(), options
.pnn
, db_id
, &priority
);
5031 DEBUG(DEBUG_ERR
,("Unable to get db prio\n"));
5035 DEBUG(DEBUG_ERR
,("Priority:%u\n", priority
));
5041 set the sticky records capability for a database
5043 static int control_setdbsticky(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5045 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5053 if (!db_exists(ctdb
, argv
[0], &db_id
, NULL
)) {
5057 ret
= ctdb_ctrl_set_db_sticky(ctdb
, options
.pnn
, db_id
);
5059 DEBUG(DEBUG_ERR
,("Unable to set db to support sticky records\n"));
5060 talloc_free(tmp_ctx
);
5064 talloc_free(tmp_ctx
);
5069 set the readonly capability for a database
5071 static int control_setdbreadonly(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5073 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5081 if (!db_exists(ctdb
, argv
[0], &db_id
, NULL
)) {
5085 ret
= ctdb_ctrl_set_db_readonly(ctdb
, options
.pnn
, db_id
);
5087 DEBUG(DEBUG_ERR
,("Unable to set db to support readonly\n"));
5088 talloc_free(tmp_ctx
);
5092 talloc_free(tmp_ctx
);
5099 static int control_getdbseqnum(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5109 if (!db_exists(ctdb
, argv
[0], &db_id
, NULL
)) {
5113 ret
= ctdb_ctrl_getdbseqnum(ctdb
, TIMELIMIT(), options
.pnn
, db_id
, &seqnum
);
5115 DEBUG(DEBUG_ERR
, ("Unable to get seqnum from node."));
5119 printf("Sequence number:%lld\n", (long long)seqnum
);
5125 run an eventscript on a node
5127 static int control_eventscript(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5133 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5136 DEBUG(DEBUG_ERR
,("Invalid arguments\n"));
5140 data
.dptr
= (unsigned char *)discard_const(argv
[0]);
5141 data
.dsize
= strlen((char *)data
.dptr
) + 1;
5143 DEBUG(DEBUG_ERR
, ("Running eventscripts with arguments \"%s\" on node %u\n", data
.dptr
, options
.pnn
));
5145 ret
= ctdb_control(ctdb
, options
.pnn
, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS
,
5146 0, data
, tmp_ctx
, NULL
, &res
, NULL
, &errmsg
);
5147 if (ret
!= 0 || res
!= 0) {
5148 DEBUG(DEBUG_ERR
,("Failed to run eventscripts - %s\n", errmsg
));
5149 talloc_free(tmp_ctx
);
5152 talloc_free(tmp_ctx
);
5156 #define DB_VERSION 1
5157 #define MAX_DB_NAME 64
5158 struct db_file_header
{
5159 unsigned long version
;
5161 unsigned long persistent
;
5163 const char name
[MAX_DB_NAME
];
5166 struct backup_data
{
5167 struct ctdb_marshall_buffer
*records
;
5170 bool traverse_error
;
5173 static int backup_traverse(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA data
, void *private)
5175 struct backup_data
*bd
= talloc_get_type(private, struct backup_data
);
5176 struct ctdb_rec_data
*rec
;
5178 /* add the record */
5179 rec
= ctdb_marshall_record(bd
->records
, 0, key
, NULL
, data
);
5181 bd
->traverse_error
= true;
5182 DEBUG(DEBUG_ERR
,("Failed to marshall record\n"));
5185 bd
->records
= talloc_realloc_size(NULL
, bd
->records
, rec
->length
+ bd
->len
);
5186 if (bd
->records
== NULL
) {
5187 DEBUG(DEBUG_ERR
,("Failed to expand marshalling buffer\n"));
5188 bd
->traverse_error
= true;
5191 bd
->records
->count
++;
5192 memcpy(bd
->len
+(uint8_t *)bd
->records
, rec
, rec
->length
);
5193 bd
->len
+= rec
->length
;
5201 * backup a database to a file
5203 static int control_backupdb(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5206 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5207 struct db_file_header dbhdr
;
5208 struct ctdb_db_context
*ctdb_db
;
5209 struct backup_data
*bd
;
5212 const char *reason
= NULL
;
5216 assert_single_node_only();
5219 DEBUG(DEBUG_ERR
,("Invalid arguments\n"));
5223 if (!db_exists(ctdb
, argv
[0], &db_id
, &flags
)) {
5227 ret
= ctdb_ctrl_getdbhealth(ctdb
, TIMELIMIT(), options
.pnn
,
5228 db_id
, tmp_ctx
, &reason
);
5230 DEBUG(DEBUG_ERR
,("Unable to get dbhealth for database '%s'\n",
5232 talloc_free(tmp_ctx
);
5236 uint32_t allow_unhealthy
= 0;
5238 ctdb_ctrl_get_tunable(ctdb
, TIMELIMIT(), options
.pnn
,
5239 "AllowUnhealthyDBRead",
5242 if (allow_unhealthy
!= 1) {
5243 DEBUG(DEBUG_ERR
,("database '%s' is unhealthy: %s\n",
5246 DEBUG(DEBUG_ERR
,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
5248 talloc_free(tmp_ctx
);
5252 DEBUG(DEBUG_WARNING
,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
5254 DEBUG(DEBUG_WARNING
,("WARNING! allow backup of unhealthy database: "
5255 "tunnable AllowUnhealthyDBRead = %u\n",
5259 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), argv
[0], flags
& CTDB_DB_FLAGS_PERSISTENT
, 0);
5260 if (ctdb_db
== NULL
) {
5261 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", argv
[0]));
5262 talloc_free(tmp_ctx
);
5267 ret
= tdb_transaction_start(ctdb_db
->ltdb
->tdb
);
5269 DEBUG(DEBUG_ERR
,("Failed to start transaction\n"));
5270 talloc_free(tmp_ctx
);
5275 bd
= talloc_zero(tmp_ctx
, struct backup_data
);
5277 DEBUG(DEBUG_ERR
,("Failed to allocate backup_data\n"));
5278 talloc_free(tmp_ctx
);
5282 bd
->records
= talloc_zero(bd
, struct ctdb_marshall_buffer
);
5283 if (bd
->records
== NULL
) {
5284 DEBUG(DEBUG_ERR
,("Failed to allocate ctdb_marshall_buffer\n"));
5285 talloc_free(tmp_ctx
);
5289 bd
->len
= offsetof(struct ctdb_marshall_buffer
, data
);
5290 bd
->records
->db_id
= ctdb_db
->db_id
;
5291 /* traverse the database collecting all records */
5292 if (tdb_traverse_read(ctdb_db
->ltdb
->tdb
, backup_traverse
, bd
) == -1 ||
5293 bd
->traverse_error
) {
5294 DEBUG(DEBUG_ERR
,("Traverse error\n"));
5295 talloc_free(tmp_ctx
);
5299 tdb_transaction_cancel(ctdb_db
->ltdb
->tdb
);
5302 fh
= open(argv
[1], O_RDWR
|O_CREAT
, 0600);
5304 DEBUG(DEBUG_ERR
,("Failed to open file '%s'\n", argv
[1]));
5305 talloc_free(tmp_ctx
);
5309 dbhdr
.version
= DB_VERSION
;
5310 dbhdr
.timestamp
= time(NULL
);
5311 dbhdr
.persistent
= flags
& CTDB_DB_FLAGS_PERSISTENT
;
5312 dbhdr
.size
= bd
->len
;
5313 if (strlen(argv
[0]) >= MAX_DB_NAME
) {
5314 DEBUG(DEBUG_ERR
,("Too long dbname\n"));
5317 strncpy(discard_const(dbhdr
.name
), argv
[0], MAX_DB_NAME
);
5318 ret
= write(fh
, &dbhdr
, sizeof(dbhdr
));
5320 DEBUG(DEBUG_ERR
,("write failed: %s\n", strerror(errno
)));
5323 ret
= write(fh
, bd
->records
, bd
->len
);
5325 DEBUG(DEBUG_ERR
,("write failed: %s\n", strerror(errno
)));
5334 DEBUG(DEBUG_ERR
,("close failed: %s\n", strerror(errno
)));
5338 DEBUG(DEBUG_ERR
,("Database backed up to %s\n", argv
[1]));
5340 talloc_free(tmp_ctx
);
5345 * restore a database from a file
5347 static int control_restoredb(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5350 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5353 struct db_file_header dbhdr
;
5354 struct ctdb_db_context
*ctdb_db
;
5355 struct ctdb_node_map
*nodemap
=NULL
;
5356 struct ctdb_vnn_map
*vnnmap
=NULL
;
5358 struct ctdb_control_wipe_database w
;
5360 uint32_t generation
;
5365 assert_single_node_only();
5367 if (argc
< 1 || argc
> 2) {
5368 DEBUG(DEBUG_ERR
,("Invalid arguments\n"));
5372 fh
= open(argv
[0], O_RDONLY
);
5374 DEBUG(DEBUG_ERR
,("Failed to open file '%s'\n", argv
[0]));
5375 talloc_free(tmp_ctx
);
5379 read(fh
, &dbhdr
, sizeof(dbhdr
));
5380 if (dbhdr
.version
!= DB_VERSION
) {
5381 DEBUG(DEBUG_ERR
,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr
.version
, DB_VERSION
));
5382 talloc_free(tmp_ctx
);
5386 dbname
= discard_const(dbhdr
.name
);
5388 dbname
= discard_const(argv
[1]);
5391 outdata
.dsize
= dbhdr
.size
;
5392 outdata
.dptr
= talloc_size(tmp_ctx
, outdata
.dsize
);
5393 if (outdata
.dptr
== NULL
) {
5394 DEBUG(DEBUG_ERR
,("Failed to allocate data of size '%lu'\n", dbhdr
.size
));
5396 talloc_free(tmp_ctx
);
5399 read(fh
, outdata
.dptr
, outdata
.dsize
);
5402 tm
= localtime(&dbhdr
.timestamp
);
5403 strftime(tbuf
,sizeof(tbuf
)-1,"%Y/%m/%d %H:%M:%S", tm
);
5404 printf("Restoring database '%s' from backup @ %s\n",
5408 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), dbname
, dbhdr
.persistent
, 0);
5409 if (ctdb_db
== NULL
) {
5410 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", dbname
));
5411 talloc_free(tmp_ctx
);
5415 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &nodemap
);
5417 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
5418 talloc_free(tmp_ctx
);
5423 ret
= ctdb_ctrl_getvnnmap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &vnnmap
);
5425 DEBUG(DEBUG_ERR
, ("Unable to get vnnmap from node %u\n", options
.pnn
));
5426 talloc_free(tmp_ctx
);
5430 /* freeze all nodes */
5431 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5432 for (i
=1; i
<=NUM_DB_PRIORITIES
; i
++) {
5433 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_FREEZE
,
5439 DEBUG(DEBUG_ERR
, ("Unable to freeze nodes.\n"));
5440 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5441 talloc_free(tmp_ctx
);
5446 generation
= vnnmap
->generation
;
5447 data
.dptr
= (void *)&generation
;
5448 data
.dsize
= sizeof(generation
);
5450 /* start a cluster wide transaction */
5451 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5452 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_TRANSACTION_START
,
5454 TIMELIMIT(), false, data
,
5457 DEBUG(DEBUG_ERR
, ("Unable to start cluster wide transactions.\n"));
5462 w
.db_id
= ctdb_db
->db_id
;
5463 w
.transaction_id
= generation
;
5465 data
.dptr
= (void *)&w
;
5466 data
.dsize
= sizeof(w
);
5468 /* wipe all the remote databases. */
5469 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5470 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_WIPE_DATABASE
,
5472 TIMELIMIT(), false, data
,
5475 DEBUG(DEBUG_ERR
, ("Unable to wipe database.\n"));
5476 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5477 talloc_free(tmp_ctx
);
5481 /* push the database */
5482 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5483 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_PUSH_DB
,
5485 TIMELIMIT(), false, outdata
,
5488 DEBUG(DEBUG_ERR
, ("Failed to push database.\n"));
5489 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5490 talloc_free(tmp_ctx
);
5494 data
.dptr
= (void *)&ctdb_db
->db_id
;
5495 data
.dsize
= sizeof(ctdb_db
->db_id
);
5497 /* mark the database as healthy */
5498 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5499 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_DB_SET_HEALTHY
,
5501 TIMELIMIT(), false, data
,
5504 DEBUG(DEBUG_ERR
, ("Failed to mark database as healthy.\n"));
5505 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5506 talloc_free(tmp_ctx
);
5510 data
.dptr
= (void *)&generation
;
5511 data
.dsize
= sizeof(generation
);
5513 /* commit all the changes */
5514 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_TRANSACTION_COMMIT
,
5516 TIMELIMIT(), false, data
,
5519 DEBUG(DEBUG_ERR
, ("Unable to commit databases.\n"));
5520 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5521 talloc_free(tmp_ctx
);
5526 /* thaw all nodes */
5527 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5528 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_THAW
,
5534 DEBUG(DEBUG_ERR
, ("Unable to thaw nodes.\n"));
5535 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5536 talloc_free(tmp_ctx
);
5541 talloc_free(tmp_ctx
);
5546 * dump a database backup from a file
5548 static int control_dumpdbbackup(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5550 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5552 struct db_file_header dbhdr
;
5556 struct ctdb_rec_data
*rec
= NULL
;
5557 struct ctdb_marshall_buffer
*m
;
5558 struct ctdb_dump_db_context c
;
5560 assert_single_node_only();
5563 DEBUG(DEBUG_ERR
,("Invalid arguments\n"));
5567 fh
= open(argv
[0], O_RDONLY
);
5569 DEBUG(DEBUG_ERR
,("Failed to open file '%s'\n", argv
[0]));
5570 talloc_free(tmp_ctx
);
5574 read(fh
, &dbhdr
, sizeof(dbhdr
));
5575 if (dbhdr
.version
!= DB_VERSION
) {
5576 DEBUG(DEBUG_ERR
,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr
.version
, DB_VERSION
));
5577 talloc_free(tmp_ctx
);
5581 outdata
.dsize
= dbhdr
.size
;
5582 outdata
.dptr
= talloc_size(tmp_ctx
, outdata
.dsize
);
5583 if (outdata
.dptr
== NULL
) {
5584 DEBUG(DEBUG_ERR
,("Failed to allocate data of size '%lu'\n", dbhdr
.size
));
5586 talloc_free(tmp_ctx
);
5589 read(fh
, outdata
.dptr
, outdata
.dsize
);
5591 m
= (struct ctdb_marshall_buffer
*)outdata
.dptr
;
5593 tm
= localtime(&dbhdr
.timestamp
);
5594 strftime(tbuf
,sizeof(tbuf
)-1,"%Y/%m/%d %H:%M:%S", tm
);
5595 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
5596 dbhdr
.name
, m
->db_id
, tbuf
);
5600 c
.printemptyrecords
= (bool)options
.printemptyrecords
;
5601 c
.printdatasize
= (bool)options
.printdatasize
;
5602 c
.printlmaster
= false;
5603 c
.printhash
= (bool)options
.printhash
;
5604 c
.printrecordflags
= (bool)options
.printrecordflags
;
5606 for (i
=0; i
< m
->count
; i
++) {
5610 /* we do not want the header splitted, so we pass NULL*/
5611 rec
= ctdb_marshall_loop_next(m
, rec
, &reqid
,
5614 ctdb_dumpdb_record(ctdb
, key
, data
, &c
);
5617 printf("Dumped %d records\n", i
);
5618 talloc_free(tmp_ctx
);
5623 * wipe a database from a file
5625 static int control_wipedb(struct ctdb_context
*ctdb
, int argc
,
5629 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5631 struct ctdb_db_context
*ctdb_db
;
5632 struct ctdb_node_map
*nodemap
= NULL
;
5633 struct ctdb_vnn_map
*vnnmap
= NULL
;
5635 struct ctdb_control_wipe_database w
;
5637 uint32_t generation
;
5640 assert_single_node_only();
5643 DEBUG(DEBUG_ERR
,("Invalid arguments\n"));
5647 if (!db_exists(ctdb
, argv
[0], NULL
, &flags
)) {
5651 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), argv
[0], flags
& CTDB_DB_FLAGS_PERSISTENT
, 0);
5652 if (ctdb_db
== NULL
) {
5653 DEBUG(DEBUG_ERR
, ("Unable to attach to database '%s'\n",
5655 talloc_free(tmp_ctx
);
5659 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
,
5662 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n",
5664 talloc_free(tmp_ctx
);
5668 ret
= ctdb_ctrl_getvnnmap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
,
5671 DEBUG(DEBUG_ERR
, ("Unable to get vnnmap from node %u\n",
5673 talloc_free(tmp_ctx
);
5677 /* freeze all nodes */
5678 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5679 for (i
=1; i
<=NUM_DB_PRIORITIES
; i
++) {
5680 ret
= ctdb_client_async_control(ctdb
, CTDB_CONTROL_FREEZE
,
5687 DEBUG(DEBUG_ERR
, ("Unable to freeze nodes.\n"));
5688 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
,
5689 CTDB_RECOVERY_ACTIVE
);
5690 talloc_free(tmp_ctx
);
5695 generation
= vnnmap
->generation
;
5696 data
.dptr
= (void *)&generation
;
5697 data
.dsize
= sizeof(generation
);
5699 /* start a cluster wide transaction */
5700 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5701 ret
= ctdb_client_async_control(ctdb
, CTDB_CONTROL_TRANSACTION_START
,
5703 TIMELIMIT(), false, data
,
5707 DEBUG(DEBUG_ERR
, ("Unable to start cluster wide "
5708 "transactions.\n"));
5712 w
.db_id
= ctdb_db
->db_id
;
5713 w
.transaction_id
= generation
;
5715 data
.dptr
= (void *)&w
;
5716 data
.dsize
= sizeof(w
);
5718 /* wipe all the remote databases. */
5719 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5720 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_WIPE_DATABASE
,
5722 TIMELIMIT(), false, data
,
5725 DEBUG(DEBUG_ERR
, ("Unable to wipe database.\n"));
5726 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5727 talloc_free(tmp_ctx
);
5731 data
.dptr
= (void *)&ctdb_db
->db_id
;
5732 data
.dsize
= sizeof(ctdb_db
->db_id
);
5734 /* mark the database as healthy */
5735 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5736 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_DB_SET_HEALTHY
,
5738 TIMELIMIT(), false, data
,
5741 DEBUG(DEBUG_ERR
, ("Failed to mark database as healthy.\n"));
5742 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5743 talloc_free(tmp_ctx
);
5747 data
.dptr
= (void *)&generation
;
5748 data
.dsize
= sizeof(generation
);
5750 /* commit all the changes */
5751 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_TRANSACTION_COMMIT
,
5753 TIMELIMIT(), false, data
,
5756 DEBUG(DEBUG_ERR
, ("Unable to commit databases.\n"));
5757 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5758 talloc_free(tmp_ctx
);
5762 /* thaw all nodes */
5763 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5764 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_THAW
,
5770 DEBUG(DEBUG_ERR
, ("Unable to thaw nodes.\n"));
5771 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5772 talloc_free(tmp_ctx
);
5776 DEBUG(DEBUG_ERR
, ("Database wiped.\n"));
5778 talloc_free(tmp_ctx
);
5785 static int control_dumpmemory(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5791 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5792 ret
= ctdb_control(ctdb
, options
.pnn
, 0, CTDB_CONTROL_DUMP_MEMORY
,
5793 0, tdb_null
, tmp_ctx
, &data
, &res
, NULL
, &errmsg
);
5794 if (ret
!= 0 || res
!= 0) {
5795 DEBUG(DEBUG_ERR
,("Failed to dump memory - %s\n", errmsg
));
5796 talloc_free(tmp_ctx
);
5799 write(1, data
.dptr
, data
.dsize
);
5800 talloc_free(tmp_ctx
);
5805 handler for memory dumps
5807 static void mem_dump_handler(struct ctdb_context
*ctdb
, uint64_t srvid
,
5808 TDB_DATA data
, void *private_data
)
5810 write(1, data
.dptr
, data
.dsize
);
5815 dump memory usage on the recovery daemon
5817 static int control_rddumpmemory(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5821 struct srvid_request rd
;
5823 rd
.pnn
= ctdb_get_pnn(ctdb
);
5824 rd
.srvid
= getpid();
5826 /* register a message port for receiveing the reply so that we
5827 can receive the reply
5829 ctdb_client_set_message_handler(ctdb
, rd
.srvid
, mem_dump_handler
, NULL
);
5832 data
.dptr
= (uint8_t *)&rd
;
5833 data
.dsize
= sizeof(rd
);
5835 ret
= ctdb_client_send_message(ctdb
, options
.pnn
, CTDB_SRVID_MEM_DUMP
, data
);
5837 DEBUG(DEBUG_ERR
,("Failed to send memdump request message to %u\n", options
.pnn
));
5841 /* this loop will terminate when we have received the reply */
5843 event_loop_once(ctdb
->ev
);
5850 send a message to a srvid
5852 static int control_msgsend(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5854 unsigned long srvid
;
5862 srvid
= strtoul(argv
[0], NULL
, 0);
5864 data
.dptr
= (uint8_t *)discard_const(argv
[1]);
5865 data
.dsize
= strlen(argv
[1]);
5867 ret
= ctdb_client_send_message(ctdb
, CTDB_BROADCAST_CONNECTED
, srvid
, data
);
5869 DEBUG(DEBUG_ERR
,("Failed to send memdump request message to %u\n", options
.pnn
));
5877 handler for msglisten
5879 static void msglisten_handler(struct ctdb_context
*ctdb
, uint64_t srvid
,
5880 TDB_DATA data
, void *private_data
)
5884 printf("Message received: ");
5885 for (i
=0;i
<data
.dsize
;i
++) {
5886 printf("%c", data
.dptr
[i
]);
5892 listen for messages on a messageport
5894 static int control_msglisten(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5900 /* register a message port and listen for messages
5902 ctdb_client_set_message_handler(ctdb
, srvid
, msglisten_handler
, NULL
);
5903 printf("Listening for messages on srvid:%d\n", (int)srvid
);
5906 event_loop_once(ctdb
->ev
);
5913 list all nodes in the cluster
5914 we parse the nodes file directly
5916 static int control_listnodes(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5918 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
5919 struct pnn_node
*pnn_nodes
;
5920 struct pnn_node
*pnn_node
;
5922 assert_single_node_only();
5924 pnn_nodes
= read_nodes_file(mem_ctx
);
5925 if (pnn_nodes
== NULL
) {
5926 DEBUG(DEBUG_ERR
,("Failed to read nodes file\n"));
5927 talloc_free(mem_ctx
);
5931 for(pnn_node
=pnn_nodes
;pnn_node
;pnn_node
=pnn_node
->next
) {
5932 ctdb_sock_addr addr
;
5933 if (parse_ip(pnn_node
->addr
, NULL
, 63999, &addr
) == 0) {
5934 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s' in nodes file\n", pnn_node
->addr
));
5935 talloc_free(mem_ctx
);
5938 if (options
.machinereadable
){
5939 printf(":%d:%s:\n", pnn_node
->pnn
, pnn_node
->addr
);
5941 printf("%s\n", pnn_node
->addr
);
5944 talloc_free(mem_ctx
);
5950 reload the nodes file on the local node
5952 static int control_reload_nodes_file(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5956 struct ctdb_node_map
*nodemap
=NULL
;
5958 assert_single_node_only();
5960 mypnn
= ctdb_get_pnn(ctdb
);
5962 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, ctdb
, &nodemap
);
5964 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from local node\n"));
5968 /* reload the nodes file on all remote nodes */
5969 for (i
=0;i
<nodemap
->num
;i
++) {
5970 if (nodemap
->nodes
[i
].pnn
== mypnn
) {
5973 DEBUG(DEBUG_NOTICE
, ("Reloading nodes file on node %u\n", nodemap
->nodes
[i
].pnn
));
5974 ret
= ctdb_ctrl_reload_nodes_file(ctdb
, TIMELIMIT(),
5975 nodemap
->nodes
[i
].pnn
);
5977 DEBUG(DEBUG_ERR
, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap
->nodes
[i
].pnn
));
5981 /* reload the nodes file on the local node */
5982 DEBUG(DEBUG_NOTICE
, ("Reloading nodes file on node %u\n", mypnn
));
5983 ret
= ctdb_ctrl_reload_nodes_file(ctdb
, TIMELIMIT(), mypnn
);
5985 DEBUG(DEBUG_ERR
, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn
));
5988 /* initiate a recovery */
5989 control_recover(ctdb
, argc
, argv
);
5995 static const struct {
5997 int (*fn
)(struct ctdb_context
*, int, const char **);
5999 bool without_daemon
; /* can be run without daemon running ? */
6002 } ctdb_commands
[] = {
6003 { "version", control_version
, true, true, "show version of ctdb" },
6004 { "status", control_status
, true, false, "show node status" },
6005 { "uptime", control_uptime
, true, false, "show node uptime" },
6006 { "ping", control_ping
, true, false, "ping all nodes" },
6007 { "runstate", control_runstate
, true, false, "get/check runstate of a node", "[setup|first_recovery|startup|running]" },
6008 { "getvar", control_getvar
, true, false, "get a tunable variable", "<name>"},
6009 { "setvar", control_setvar
, true, false, "set a tunable variable", "<name> <value>"},
6010 { "listvars", control_listvars
, true, false, "list tunable variables"},
6011 { "statistics", control_statistics
, false, false, "show statistics" },
6012 { "statisticsreset", control_statistics_reset
, true, false, "reset statistics"},
6013 { "stats", control_stats
, false, false, "show rolling statistics", "[number of history records]" },
6014 { "ip", control_ip
, false, false, "show which public ip's that ctdb manages" },
6015 { "ipinfo", control_ipinfo
, true, false, "show details about a public ip that ctdb manages", "<ip>" },
6016 { "ifaces", control_ifaces
, true, false, "show which interfaces that ctdb manages" },
6017 { "setifacelink", control_setifacelink
, true, false, "set interface link status", "<iface> <status>" },
6018 { "process-exists", control_process_exists
, true, false, "check if a process exists on a node", "<pid>"},
6019 { "getdbmap", control_getdbmap
, true, false, "show the database map" },
6020 { "getdbstatus", control_getdbstatus
, true, false, "show the status of a database", "<dbname|dbid>" },
6021 { "catdb", control_catdb
, true, false, "dump a ctdb database" , "<dbname|dbid>"},
6022 { "cattdb", control_cattdb
, true, false, "dump a local tdb database" , "<dbname|dbid>"},
6023 { "getmonmode", control_getmonmode
, true, false, "show monitoring mode" },
6024 { "getcapabilities", control_getcapabilities
, true, false, "show node capabilities" },
6025 { "pnn", control_pnn
, true, false, "show the pnn of the currnet node" },
6026 { "lvs", control_lvs
, true, false, "show lvs configuration" },
6027 { "lvsmaster", control_lvsmaster
, true, false, "show which node is the lvs master" },
6028 { "disablemonitor", control_disable_monmode
,true, false, "set monitoring mode to DISABLE" },
6029 { "enablemonitor", control_enable_monmode
, true, false, "set monitoring mode to ACTIVE" },
6030 { "setdebug", control_setdebug
, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
6031 { "getdebug", control_getdebug
, true, false, "get debug level" },
6032 { "getlog", control_getlog
, true, false, "get the log data from the in memory ringbuffer", "[<level>] [recoverd]" },
6033 { "clearlog", control_clearlog
, true, false, "clear the log data from the in memory ringbuffer", "[recoverd]" },
6034 { "attach", control_attach
, true, false, "attach to a database", "<dbname> [persistent]" },
6035 { "dumpmemory", control_dumpmemory
, true, false, "dump memory map to stdout" },
6036 { "rddumpmemory", control_rddumpmemory
, true, false, "dump memory map from the recovery daemon to stdout" },
6037 { "getpid", control_getpid
, true, false, "get ctdbd process ID" },
6038 { "disable", control_disable
, true, false, "disable a nodes public IP" },
6039 { "enable", control_enable
, true, false, "enable a nodes public IP" },
6040 { "stop", control_stop
, true, false, "stop a node" },
6041 { "continue", control_continue
, true, false, "re-start a stopped node" },
6042 { "ban", control_ban
, true, false, "ban a node from the cluster", "<bantime>"},
6043 { "unban", control_unban
, true, false, "unban a node" },
6044 { "showban", control_showban
, true, false, "show ban information"},
6045 { "shutdown", control_shutdown
, true, false, "shutdown ctdbd" },
6046 { "recover", control_recover
, true, false, "force recovery" },
6047 { "sync", control_ipreallocate
, false, false, "wait until ctdbd has synced all state changes" },
6048 { "ipreallocate", control_ipreallocate
, false, false, "force the recovery daemon to perform a ip reallocation procedure" },
6049 { "thaw", control_thaw
, true, false, "thaw databases", "[priority:1-3]" },
6050 { "isnotrecmaster", control_isnotrecmaster
, false, false, "check if the local node is recmaster or not" },
6051 { "killtcp", kill_tcp
, false, false, "kill a tcp connection.", "[<srcip:port> <dstip:port>]" },
6052 { "gratiousarp", control_gratious_arp
, false, false, "send a gratious arp", "<ip> <interface>" },
6053 { "tickle", tickle_tcp
, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
6054 { "gettickles", control_get_tickles
, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
6055 { "addtickle", control_add_tickle
, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
6057 { "deltickle", control_del_tickle
, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
6059 { "regsrvid", regsrvid
, false, false, "register a server id", "<pnn> <type> <id>" },
6060 { "unregsrvid", unregsrvid
, false, false, "unregister a server id", "<pnn> <type> <id>" },
6061 { "chksrvid", chksrvid
, false, false, "check if a server id exists", "<pnn> <type> <id>" },
6062 { "getsrvids", getsrvids
, false, false, "get a list of all server ids"},
6063 { "check_srvids", check_srvids
, false, false, "check if a srvid exists", "<id>+" },
6064 { "repack", ctdb_repack
, false, false, "repack all databases", "[max_freelist]"},
6065 { "listnodes", control_listnodes
, false, true, "list all nodes in the cluster"},
6066 { "reloadnodes", control_reload_nodes_file
, false, false, "reload the nodes file and restart the transport on all nodes"},
6067 { "moveip", control_moveip
, false, false, "move/failover an ip address to another node", "<ip> <node>"},
6068 { "rebalanceip", control_rebalanceip
, false, false, "release an ip from the node and let recd rebalance it", "<ip>"},
6069 { "addip", control_addip
, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
6070 { "delip", control_delip
, false, false, "delete an ip address from a node", "<ip>"},
6071 { "eventscript", control_eventscript
, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
6072 { "backupdb", control_backupdb
, false, false, "backup the database into a file.", "<dbname|dbid> <file>"},
6073 { "restoredb", control_restoredb
, false, false, "restore the database from a file.", "<file> [dbname]"},
6074 { "dumpdbbackup", control_dumpdbbackup
, false, true, "dump database backup from a file.", "<file>"},
6075 { "wipedb", control_wipedb
, false, false, "wipe the contents of a database.", "<dbname|dbid>"},
6076 { "recmaster", control_recmaster
, true, false, "show the pnn for the recovery master."},
6077 { "scriptstatus", control_scriptstatus
, true, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
6078 { "enablescript", control_enablescript
, true, false, "enable an eventscript", "<script>"},
6079 { "disablescript", control_disablescript
, true, false, "disable an eventscript", "<script>"},
6080 { "natgwlist", control_natgwlist
, true, false, "show the nodes belonging to this natgw configuration"},
6081 { "xpnn", control_xpnn
, false, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
6082 { "getreclock", control_getreclock
, true, false, "Show the reclock file of a node"},
6083 { "setreclock", control_setreclock
, true, false, "Set/clear the reclock file of a node", "[filename]"},
6084 { "setnatgwstate", control_setnatgwstate
, false, false, "Set NATGW state to on/off", "{on|off}"},
6085 { "setlmasterrole", control_setlmasterrole
, false, false, "Set LMASTER role to on/off", "{on|off}"},
6086 { "setrecmasterrole", control_setrecmasterrole
, false, false, "Set RECMASTER role to on/off", "{on|off}"},
6087 { "setdbprio", control_setdbprio
, false, false, "Set DB priority", "<dbname|dbid> <prio:1-3>"},
6088 { "getdbprio", control_getdbprio
, false, false, "Get DB priority", "<dbname|dbid>"},
6089 { "setdbreadonly", control_setdbreadonly
, false, false, "Set DB readonly capable", "<dbname|dbid>"},
6090 { "setdbsticky", control_setdbsticky
, false, false, "Set DB sticky-records capable", "<dbname|dbid>"},
6091 { "msglisten", control_msglisten
, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
6092 { "msgsend", control_msgsend
, false, false, "Send a message to srvid", "<srvid> <message>"},
6093 { "pfetch", control_pfetch
, false, false, "fetch a record from a persistent database", "<dbname|dbid> <key> [<file>]" },
6094 { "pstore", control_pstore
, false, false, "write a record to a persistent database", "<dbname|dbid> <key> <file containing record>" },
6095 { "pdelete", control_pdelete
, false, false, "delete a record from a persistent database", "<dbname|dbid> <key>" },
6096 { "tfetch", control_tfetch
, false, true, "fetch a record from a [c]tdb-file [-v]", "<tdb-file> <key> [<file>]" },
6097 { "tstore", control_tstore
, false, true, "store a record (including ltdb header)", "<tdb-file> <key> <data+header>" },
6098 { "readkey", control_readkey
, true, false, "read the content off a database key", "<tdb-file> <key>" },
6099 { "writekey", control_writekey
, true, false, "write to a database key", "<tdb-file> <key> <value>" },
6100 { "checktcpport", control_chktcpport
, false, true, "check if a service is bound to a specific tcp port or not", "<port>" },
6101 { "rebalancenode", control_rebalancenode
, false, false, "mark nodes as forced IP rebalancing targets", "[<pnn-list>]"},
6102 { "getdbseqnum", control_getdbseqnum
, false, false, "get the sequence number off a database", "<dbname|dbid>" },
6103 { "nodestatus", control_nodestatus
, true, false, "show and return node status", "[<pnn-list>]" },
6104 { "dbstatistics", control_dbstatistics
, false, false, "show db statistics", "<dbname|dbid>" },
6105 { "reloadips", control_reloadips
, false, false, "reload the public addresses file on specified nodes" , "[<pnn-list>]" },
6106 { "ipiface", control_ipiface
, false, true, "Find which interface an ip address is hosted on", "<ip>" },
6112 static void usage(void)
6116 "Usage: ctdb [options] <control>\n" \
6118 " -n <node> choose node number, or 'all' (defaults to local node)\n"
6119 " -Y generate machinereadable output\n"
6120 " -v generate verbose output\n"
6121 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options
.timelimit
);
6122 printf("Controls:\n");
6123 for (i
=0;i
<ARRAY_SIZE(ctdb_commands
);i
++) {
6124 printf(" %-15s %-27s %s\n",
6125 ctdb_commands
[i
].name
,
6126 ctdb_commands
[i
].args
?ctdb_commands
[i
].args
:"",
6127 ctdb_commands
[i
].msg
);
6133 static void ctdb_alarm(int sig
)
6135 printf("Maximum runtime exceeded - exiting\n");
6142 int main(int argc
, const char *argv
[])
6144 struct ctdb_context
*ctdb
;
6145 char *nodestring
= NULL
;
6146 struct poptOption popt_options
[] = {
6149 { "timelimit", 't', POPT_ARG_INT
, &options
.timelimit
, 0, "timelimit", "integer" },
6150 { "node", 'n', POPT_ARG_STRING
, &nodestring
, 0, "node", "integer|all" },
6151 { "machinereadable", 'Y', POPT_ARG_NONE
, &options
.machinereadable
, 0, "enable machinereadable output", NULL
},
6152 { "verbose", 'v', POPT_ARG_NONE
, &options
.verbose
, 0, "enable verbose output", NULL
},
6153 { "maxruntime", 'T', POPT_ARG_INT
, &options
.maxruntime
, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
6154 { "print-emptyrecords", 0, POPT_ARG_NONE
, &options
.printemptyrecords
, 0, "print the empty records when dumping databases (catdb, cattdb, dumpdbbackup)", NULL
},
6155 { "print-datasize", 0, POPT_ARG_NONE
, &options
.printdatasize
, 0, "do not print record data when dumping databases, only the data size", NULL
},
6156 { "print-lmaster", 0, POPT_ARG_NONE
, &options
.printlmaster
, 0, "print the record's lmaster in catdb", NULL
},
6157 { "print-hash", 0, POPT_ARG_NONE
, &options
.printhash
, 0, "print the record's hash when dumping databases", NULL
},
6158 { "print-recordflags", 0, POPT_ARG_NONE
, &options
.printrecordflags
, 0, "print the record flags in catdb and dumpdbbackup", NULL
},
6162 const char **extra_argv
;
6166 struct event_context
*ev
;
6167 const char *control
;
6171 /* set some defaults */
6172 options
.maxruntime
= 0;
6173 options
.timelimit
= 10;
6174 options
.pnn
= CTDB_CURRENT_NODE
;
6176 pc
= poptGetContext(argv
[0], argc
, argv
, popt_options
, POPT_CONTEXT_KEEP_FIRST
);
6178 while ((opt
= poptGetNextOpt(pc
)) != -1) {
6181 DEBUG(DEBUG_ERR
, ("Invalid option %s: %s\n",
6182 poptBadOption(pc
, 0), poptStrerror(opt
)));
6187 /* setup the remaining options for the main program to use */
6188 extra_argv
= poptGetArgs(pc
);
6191 while (extra_argv
[extra_argc
]) extra_argc
++;
6194 if (extra_argc
< 1) {
6198 if (options
.maxruntime
== 0) {
6199 const char *ctdb_timeout
;
6200 ctdb_timeout
= getenv("CTDB_TIMEOUT");
6201 if (ctdb_timeout
!= NULL
) {
6202 options
.maxruntime
= strtoul(ctdb_timeout
, NULL
, 0);
6204 /* default timeout is 120 seconds */
6205 options
.maxruntime
= 120;
6209 signal(SIGALRM
, ctdb_alarm
);
6210 alarm(options
.maxruntime
);
6212 control
= extra_argv
[0];
6214 /* Default value for CTDB_BASE - don't override */
6215 setenv("CTDB_BASE", ETCDIR
"/ctdb", 0);
6217 ev
= event_context_init(NULL
);
6219 DEBUG(DEBUG_ERR
, ("Failed to initialize event system\n"));
6223 for (i
=0;i
<ARRAY_SIZE(ctdb_commands
);i
++) {
6224 if (strcmp(control
, ctdb_commands
[i
].name
) == 0) {
6229 if (i
== ARRAY_SIZE(ctdb_commands
)) {
6230 DEBUG(DEBUG_ERR
, ("Unknown control '%s'\n", control
));
6234 if (ctdb_commands
[i
].without_daemon
== true) {
6235 if (nodestring
!= NULL
) {
6236 DEBUG(DEBUG_ERR
, ("Can't specify node(s) with \"ctdb %s\"\n", control
));
6240 return ctdb_commands
[i
].fn(NULL
, extra_argc
-1, extra_argv
+1);
6243 /* initialise ctdb */
6244 ctdb
= ctdb_cmdline_client(ev
, TIMELIMIT());
6247 DEBUG(DEBUG_ERR
, ("Failed to init ctdb\n"));
6251 /* setup the node number(s) to contact */
6252 if (!parse_nodestring(ctdb
, ctdb
, nodestring
, CTDB_CURRENT_NODE
, false,
6253 &options
.nodes
, &options
.pnn
)) {
6257 if (options
.pnn
== CTDB_CURRENT_NODE
) {
6258 options
.pnn
= options
.nodes
[0];
6261 if (ctdb_commands
[i
].auto_all
&&
6262 ((options
.pnn
== CTDB_BROADCAST_ALL
) ||
6263 (options
.pnn
== CTDB_MULTICAST
))) {
6267 for (j
= 0; j
< talloc_array_length(options
.nodes
); j
++) {
6268 options
.pnn
= options
.nodes
[j
];
6269 ret
|= ctdb_commands
[i
].fn(ctdb
, extra_argc
-1, extra_argv
+1);
6272 ret
= ctdb_commands
[i
].fn(ctdb
, extra_argc
-1, extra_argv
+1);
6277 (void)poptFreeContext(pc
);