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"
33 #include "lib/util/dlinklist.h"
35 #define ERR_TIMEOUT 20 /* timed out trying to reach node */
36 #define ERR_NONODE 21 /* node does not exist */
37 #define ERR_DISNODE 22 /* node is disconnected */
39 static void usage(void);
48 int printemptyrecords
;
55 #define LONGTIMEOUT options.timelimit*10
57 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
58 #define LONGTIMELIMIT() timeval_current_ofs(LONGTIMEOUT, 0)
60 static int control_version(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
62 printf("CTDB version: %s\n", CTDB_VERSION_STRING
);
66 #define CTDB_NOMEM_ABORT(p) do { if (!(p)) { \
67 DEBUG(DEBUG_ALERT,("ctdb fatal error: %s\n", \
68 "Out of memory in " __location__ )); \
72 static uint32_t getpnn(struct ctdb_context
*ctdb
)
74 if ((options
.pnn
== CTDB_BROADCAST_ALL
) ||
75 (options
.pnn
== CTDB_MULTICAST
)) {
77 ("Cannot get PNN for node %u\n", options
.pnn
));
81 if (options
.pnn
== CTDB_CURRENT_NODE
) {
82 return ctdb_get_pnn(ctdb
);
88 static void assert_single_node_only(void)
90 if ((options
.pnn
== CTDB_BROADCAST_ALL
) ||
91 (options
.pnn
== CTDB_MULTICAST
)) {
93 ("This control can not be applied to multiple PNNs\n"));
98 /* Pretty print the flags to a static buffer in human-readable format.
99 * This never returns NULL!
101 static const char *pretty_print_flags(uint32_t flags
)
104 static const struct {
108 { NODE_FLAGS_DISCONNECTED
, "DISCONNECTED" },
109 { NODE_FLAGS_PERMANENTLY_DISABLED
, "DISABLED" },
110 { NODE_FLAGS_BANNED
, "BANNED" },
111 { NODE_FLAGS_UNHEALTHY
, "UNHEALTHY" },
112 { NODE_FLAGS_DELETED
, "DELETED" },
113 { NODE_FLAGS_STOPPED
, "STOPPED" },
114 { NODE_FLAGS_INACTIVE
, "INACTIVE" },
116 static char flags_str
[512]; /* Big enough to contain all flag names */
119 for (j
=0;j
<ARRAY_SIZE(flag_names
);j
++) {
120 if (flags
& flag_names
[j
].flag
) {
121 if (flags_str
[0] == '\0') {
122 (void) strcpy(flags_str
, flag_names
[j
].name
);
124 (void) strncat(flags_str
, "|", sizeof(flags_str
)-1);
125 (void) strncat(flags_str
, flag_names
[j
].name
,
126 sizeof(flags_str
)-1);
130 if (flags_str
[0] == '\0') {
131 (void) strcpy(flags_str
, "OK");
137 static int h2i(char h
)
139 if (h
>= 'a' && h
<= 'f') return h
- 'a' + 10;
140 if (h
>= 'A' && h
<= 'F') return h
- 'f' + 10;
144 static TDB_DATA
hextodata(TALLOC_CTX
*mem_ctx
, const char *str
)
147 TDB_DATA key
= {NULL
, 0};
151 DEBUG(DEBUG_ERR
,("Key specified with odd number of hexadecimal digits\n"));
156 key
.dptr
= talloc_size(mem_ctx
, key
.dsize
);
158 for (i
=0; i
< len
/2; i
++) {
159 key
.dptr
[i
] = h2i(str
[i
*2]) << 4 | h2i(str
[i
*2+1]);
164 /* Parse a nodestring. Parameter dd_ok controls what happens to nodes
165 * that are disconnected or deleted. If dd_ok is true those nodes are
166 * included in the output list of nodes. If dd_ok is false, those
167 * nodes are filtered from the "all" case and cause an error if
168 * explicitly specified.
170 static bool parse_nodestring(struct ctdb_context
*ctdb
,
172 const char * nodestring
,
173 uint32_t current_pnn
,
178 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
181 struct ctdb_node_map
*nodemap
;
186 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, tmp_ctx
, &nodemap
);
188 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from local node\n"));
189 talloc_free(tmp_ctx
);
193 if (nodestring
!= NULL
) {
194 *nodes
= talloc_array(mem_ctx
, uint32_t, 0);
195 if (*nodes
== NULL
) {
201 if (strcmp(nodestring
, "all") == 0) {
202 *pnn_mode
= CTDB_BROADCAST_ALL
;
205 for (i
= 0; i
< nodemap
->num
; i
++) {
206 if ((nodemap
->nodes
[i
].flags
&
207 (NODE_FLAGS_DISCONNECTED
|
208 NODE_FLAGS_DELETED
)) && !dd_ok
) {
211 *nodes
= talloc_realloc(mem_ctx
, *nodes
,
213 if (*nodes
== NULL
) {
223 ns
= talloc_strdup(tmp_ctx
, nodestring
);
224 tok
= strtok(ns
, ",");
225 while (tok
!= NULL
) {
228 i
= (uint32_t)strtoul(tok
, &endptr
, 0);
229 if (i
== 0 && tok
== endptr
) {
231 ("Invalid node %s\n", tok
));
232 talloc_free(tmp_ctx
);
235 if (i
>= nodemap
->num
) {
236 DEBUG(DEBUG_ERR
, ("Node %u does not exist\n", i
));
237 talloc_free(tmp_ctx
);
240 if ((nodemap
->nodes
[i
].flags
&
241 (NODE_FLAGS_DISCONNECTED
|
242 NODE_FLAGS_DELETED
)) && !dd_ok
) {
243 DEBUG(DEBUG_ERR
, ("Node %u has status %s\n", i
, pretty_print_flags(nodemap
->nodes
[i
].flags
)));
244 talloc_free(tmp_ctx
);
247 if ((pnn
= ctdb_ctrl_getpnn(ctdb
, TIMELIMIT(), i
)) < 0) {
248 DEBUG(DEBUG_ERR
, ("Can not access node %u. Node is not operational.\n", i
));
249 talloc_free(tmp_ctx
);
253 *nodes
= talloc_realloc(mem_ctx
, *nodes
,
255 if (*nodes
== NULL
) {
262 tok
= strtok(NULL
, ",");
267 *pnn_mode
= (*nodes
)[0];
269 *pnn_mode
= CTDB_MULTICAST
;
273 /* default - no nodes specified */
274 *nodes
= talloc_array(mem_ctx
, uint32_t, 1);
275 if (*nodes
== NULL
) {
278 *pnn_mode
= CTDB_CURRENT_NODE
;
280 if (((*nodes
)[0] = ctdb_ctrl_getpnn(ctdb
, TIMELIMIT(), current_pnn
)) < 0) {
285 talloc_free(tmp_ctx
);
289 talloc_free(tmp_ctx
);
294 check if a database exists
296 static bool db_exists(struct ctdb_context
*ctdb
, const char *dbarg
,
297 uint32_t *dbid
, const char **dbname
, uint8_t *flags
)
300 struct ctdb_dbid_map
*dbmap
=NULL
;
301 bool dbid_given
= false, found
= false;
303 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
306 ret
= ctdb_ctrl_getdbmap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &dbmap
);
308 DEBUG(DEBUG_ERR
, ("Unable to get dbids from node %u\n", options
.pnn
));
312 if (strncmp(dbarg
, "0x", 2) == 0) {
313 id
= strtoul(dbarg
, NULL
, 0);
317 for(i
=0; i
<dbmap
->num
; i
++) {
319 if (id
== dbmap
->dbs
[i
].dbid
) {
324 ret
= ctdb_ctrl_getdbname(ctdb
, TIMELIMIT(), options
.pnn
, dbmap
->dbs
[i
].dbid
, tmp_ctx
, &name
);
326 DEBUG(DEBUG_ERR
, ("Unable to get dbname from dbid %u\n", dbmap
->dbs
[i
].dbid
));
330 if (strcmp(name
, dbarg
) == 0) {
331 id
= dbmap
->dbs
[i
].dbid
;
338 if (found
&& dbid_given
&& dbname
!= NULL
) {
339 ret
= ctdb_ctrl_getdbname(ctdb
, TIMELIMIT(), options
.pnn
, dbmap
->dbs
[i
].dbid
, tmp_ctx
, &name
);
341 DEBUG(DEBUG_ERR
, ("Unable to get dbname from dbid %u\n", dbmap
->dbs
[i
].dbid
));
348 if (dbid
) *dbid
= id
;
349 if (dbname
) *dbname
= talloc_strdup(ctdb
, name
);
350 if (flags
) *flags
= dbmap
->dbs
[i
].flags
;
352 DEBUG(DEBUG_ERR
,("No database matching '%s' found\n", dbarg
));
356 talloc_free(tmp_ctx
);
361 see if a process exists
363 static int control_process_exists(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
371 if (sscanf(argv
[0], "%u:%u", &pnn
, &pid
) != 2) {
372 DEBUG(DEBUG_ERR
, ("Badly formed pnn:pid\n"));
376 ret
= ctdb_ctrl_process_exists(ctdb
, pnn
, pid
);
378 printf("%u:%u exists\n", pnn
, pid
);
380 printf("%u:%u does not exist\n", pnn
, pid
);
386 display statistics structure
388 static void show_statistics(struct ctdb_statistics
*s
, int show_header
)
390 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
392 const char *prefix
=NULL
;
394 int tmp
, days
, hours
, minutes
, seconds
;
399 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
400 STATISTICS_FIELD(num_clients
),
401 STATISTICS_FIELD(frozen
),
402 STATISTICS_FIELD(recovering
),
403 STATISTICS_FIELD(num_recoveries
),
404 STATISTICS_FIELD(client_packets_sent
),
405 STATISTICS_FIELD(client_packets_recv
),
406 STATISTICS_FIELD(node_packets_sent
),
407 STATISTICS_FIELD(node_packets_recv
),
408 STATISTICS_FIELD(keepalive_packets_sent
),
409 STATISTICS_FIELD(keepalive_packets_recv
),
410 STATISTICS_FIELD(node
.req_call
),
411 STATISTICS_FIELD(node
.reply_call
),
412 STATISTICS_FIELD(node
.req_dmaster
),
413 STATISTICS_FIELD(node
.reply_dmaster
),
414 STATISTICS_FIELD(node
.reply_error
),
415 STATISTICS_FIELD(node
.req_message
),
416 STATISTICS_FIELD(node
.req_control
),
417 STATISTICS_FIELD(node
.reply_control
),
418 STATISTICS_FIELD(client
.req_call
),
419 STATISTICS_FIELD(client
.req_message
),
420 STATISTICS_FIELD(client
.req_control
),
421 STATISTICS_FIELD(timeouts
.call
),
422 STATISTICS_FIELD(timeouts
.control
),
423 STATISTICS_FIELD(timeouts
.traverse
),
424 STATISTICS_FIELD(locks
.num_calls
),
425 STATISTICS_FIELD(locks
.num_current
),
426 STATISTICS_FIELD(locks
.num_pending
),
427 STATISTICS_FIELD(locks
.num_failed
),
428 STATISTICS_FIELD(total_calls
),
429 STATISTICS_FIELD(pending_calls
),
430 STATISTICS_FIELD(childwrite_calls
),
431 STATISTICS_FIELD(pending_childwrite_calls
),
432 STATISTICS_FIELD(memory_used
),
433 STATISTICS_FIELD(max_hop_count
),
434 STATISTICS_FIELD(total_ro_delegations
),
435 STATISTICS_FIELD(total_ro_revokes
),
438 tmp
= s
->statistics_current_time
.tv_sec
- s
->statistics_start_time
.tv_sec
;
447 if (options
.machinereadable
){
449 printf("CTDB version:");
450 printf("Current time of statistics:");
451 printf("Statistics collected since:");
452 for (i
=0;i
<ARRAY_SIZE(fields
);i
++) {
453 printf("%s:", fields
[i
].name
);
455 printf("num_reclock_ctdbd_latency:");
456 printf("min_reclock_ctdbd_latency:");
457 printf("avg_reclock_ctdbd_latency:");
458 printf("max_reclock_ctdbd_latency:");
460 printf("num_reclock_recd_latency:");
461 printf("min_reclock_recd_latency:");
462 printf("avg_reclock_recd_latency:");
463 printf("max_reclock_recd_latency:");
465 printf("num_call_latency:");
466 printf("min_call_latency:");
467 printf("avg_call_latency:");
468 printf("max_call_latency:");
470 printf("num_lockwait_latency:");
471 printf("min_lockwait_latency:");
472 printf("avg_lockwait_latency:");
473 printf("max_lockwait_latency:");
475 printf("num_childwrite_latency:");
476 printf("min_childwrite_latency:");
477 printf("avg_childwrite_latency:");
478 printf("max_childwrite_latency:");
481 printf("%d:", CTDB_VERSION
);
482 printf("%d:", (int)s
->statistics_current_time
.tv_sec
);
483 printf("%d:", (int)s
->statistics_start_time
.tv_sec
);
484 for (i
=0;i
<ARRAY_SIZE(fields
);i
++) {
485 printf("%d:", *(uint32_t *)(fields
[i
].offset
+(uint8_t *)s
));
487 printf("%d:", s
->reclock
.ctdbd
.num
);
488 printf("%.6f:", s
->reclock
.ctdbd
.min
);
489 printf("%.6f:", s
->reclock
.ctdbd
.num
?s
->reclock
.ctdbd
.total
/s
->reclock
.ctdbd
.num
:0.0);
490 printf("%.6f:", s
->reclock
.ctdbd
.max
);
492 printf("%d:", s
->reclock
.recd
.num
);
493 printf("%.6f:", s
->reclock
.recd
.min
);
494 printf("%.6f:", s
->reclock
.recd
.num
?s
->reclock
.recd
.total
/s
->reclock
.recd
.num
:0.0);
495 printf("%.6f:", s
->reclock
.recd
.max
);
497 printf("%d:", s
->call_latency
.num
);
498 printf("%.6f:", s
->call_latency
.min
);
499 printf("%.6f:", s
->call_latency
.num
?s
->call_latency
.total
/s
->call_latency
.num
:0.0);
500 printf("%.6f:", s
->call_latency
.max
);
502 printf("%d:", s
->childwrite_latency
.num
);
503 printf("%.6f:", s
->childwrite_latency
.min
);
504 printf("%.6f:", s
->childwrite_latency
.num
?s
->childwrite_latency
.total
/s
->childwrite_latency
.num
:0.0);
505 printf("%.6f:", s
->childwrite_latency
.max
);
508 printf("CTDB version %u\n", CTDB_VERSION
);
509 printf("Current time of statistics : %s", ctime(&s
->statistics_current_time
.tv_sec
));
510 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s", days
, hours
, minutes
, seconds
, ctime(&s
->statistics_start_time
.tv_sec
));
512 for (i
=0;i
<ARRAY_SIZE(fields
);i
++) {
513 if (strchr(fields
[i
].name
, '.')) {
514 preflen
= strcspn(fields
[i
].name
, ".")+1;
515 if (!prefix
|| strncmp(prefix
, fields
[i
].name
, preflen
) != 0) {
516 prefix
= fields
[i
].name
;
517 printf(" %*.*s\n", preflen
-1, preflen
-1, fields
[i
].name
);
522 printf(" %*s%-22s%*s%10u\n",
524 fields
[i
].name
+preflen
,
526 *(uint32_t *)(fields
[i
].offset
+(uint8_t *)s
));
528 printf(" hop_count_buckets:");
529 for (i
=0;i
<MAX_COUNT_BUCKETS
;i
++) {
530 printf(" %d", s
->hop_count_bucket
[i
]);
533 printf(" lock_buckets:");
534 for (i
=0; i
<MAX_COUNT_BUCKETS
; i
++) {
535 printf(" %d", s
->locks
.buckets
[i
]);
538 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
);
540 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
);
542 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
);
544 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
);
545 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
);
548 talloc_free(tmp_ctx
);
552 display remote ctdb statistics combined from all nodes
554 static int control_statistics_all(struct ctdb_context
*ctdb
)
557 struct ctdb_statistics statistics
;
561 nodes
= ctdb_get_connected_nodes(ctdb
, TIMELIMIT(), ctdb
, &num_nodes
);
562 CTDB_NO_MEMORY(ctdb
, nodes
);
564 ZERO_STRUCT(statistics
);
566 for (i
=0;i
<num_nodes
;i
++) {
567 struct ctdb_statistics s1
;
569 uint32_t *v1
= (uint32_t *)&s1
;
570 uint32_t *v2
= (uint32_t *)&statistics
;
572 offsetof(struct ctdb_statistics
, __last_counter
) / sizeof(uint32_t);
573 ret
= ctdb_ctrl_statistics(ctdb
, nodes
[i
], &s1
);
575 DEBUG(DEBUG_ERR
, ("Unable to get statistics from node %u\n", nodes
[i
]));
578 for (j
=0;j
<num_ints
;j
++) {
581 statistics
.max_hop_count
=
582 MAX(statistics
.max_hop_count
, s1
.max_hop_count
);
583 statistics
.call_latency
.max
=
584 MAX(statistics
.call_latency
.max
, s1
.call_latency
.max
);
587 printf("Gathered statistics for %u nodes\n", num_nodes
);
588 show_statistics(&statistics
, 1);
593 display remote ctdb statistics
595 static int control_statistics(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
598 struct ctdb_statistics statistics
;
600 if (options
.pnn
== CTDB_BROADCAST_ALL
) {
601 return control_statistics_all(ctdb
);
604 ret
= ctdb_ctrl_statistics(ctdb
, options
.pnn
, &statistics
);
606 DEBUG(DEBUG_ERR
, ("Unable to get statistics from node %u\n", options
.pnn
));
609 show_statistics(&statistics
, 1);
615 reset remote ctdb statistics
617 static int control_statistics_reset(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
621 ret
= ctdb_statistics_reset(ctdb
, options
.pnn
);
623 DEBUG(DEBUG_ERR
, ("Unable to reset statistics on node %u\n", options
.pnn
));
631 display remote ctdb rolling statistics
633 static int control_stats(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
636 struct ctdb_statistics_wire
*stats
;
637 int i
, num_records
= -1;
639 assert_single_node_only();
642 num_records
= atoi(argv
[0]) - 1;
645 ret
= ctdb_ctrl_getstathistory(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &stats
);
647 DEBUG(DEBUG_ERR
, ("Unable to get rolling statistics from node %u\n", options
.pnn
));
650 for (i
=0;i
<stats
->num
;i
++) {
651 if (stats
->stats
[i
].statistics_start_time
.tv_sec
== 0) {
654 show_statistics(&stats
->stats
[i
], i
==0);
655 if (i
== num_records
) {
664 display remote ctdb db statistics
666 static int control_dbstatistics(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
668 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
669 struct ctdb_db_statistics
*dbstat
;
679 if (!db_exists(ctdb
, argv
[0], &db_id
, NULL
, NULL
)) {
683 ret
= ctdb_ctrl_dbstatistics(ctdb
, options
.pnn
, db_id
, tmp_ctx
, &dbstat
);
685 DEBUG(DEBUG_ERR
,("Failed to read db statistics from node\n"));
686 talloc_free(tmp_ctx
);
690 printf("DB Statistics: %s\n", argv
[0]);
691 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_delegations", 4, "",
692 dbstat
->db_ro_delegations
);
693 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_revokes", 4, "",
694 dbstat
->db_ro_delegations
);
695 printf(" %s\n", "locks");
696 printf(" %*s%-22s%*s%10u\n", 4, "", "total", 0, "",
697 dbstat
->locks
.num_calls
);
698 printf(" %*s%-22s%*s%10u\n", 4, "", "failed", 0, "",
699 dbstat
->locks
.num_failed
);
700 printf(" %*s%-22s%*s%10u\n", 4, "", "current", 0, "",
701 dbstat
->locks
.num_current
);
702 printf(" %*s%-22s%*s%10u\n", 4, "", "pending", 0, "",
703 dbstat
->locks
.num_pending
);
704 printf(" %s", "hop_count_buckets:");
705 for (i
=0; i
<MAX_COUNT_BUCKETS
; i
++) {
706 printf(" %d", dbstat
->hop_count_bucket
[i
]);
709 printf(" %s", "lock_buckets:");
710 for (i
=0; i
<MAX_COUNT_BUCKETS
; i
++) {
711 printf(" %d", dbstat
->locks
.buckets
[i
]);
714 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
715 "locks_latency MIN/AVG/MAX",
716 dbstat
->locks
.latency
.min
,
717 (dbstat
->locks
.latency
.num
?
718 dbstat
->locks
.latency
.total
/dbstat
->locks
.latency
.num
:
720 dbstat
->locks
.latency
.max
,
721 dbstat
->locks
.latency
.num
);
723 for (i
=0; i
<dbstat
->num_hot_keys
; i
++) {
724 if (dbstat
->hot_keys
[i
].count
> 0) {
728 dbstat
->num_hot_keys
= num_hot_keys
;
730 printf(" Num Hot Keys: %d\n", dbstat
->num_hot_keys
);
731 for (i
= 0; i
< dbstat
->num_hot_keys
; i
++) {
733 printf(" Count:%d Key:", dbstat
->hot_keys
[i
].count
);
734 for (j
= 0; j
< dbstat
->hot_keys
[i
].key
.dsize
; j
++) {
735 printf("%02x", dbstat
->hot_keys
[i
].key
.dptr
[j
]&0xff);
740 talloc_free(tmp_ctx
);
745 display uptime of remote node
747 static int control_uptime(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
750 struct ctdb_uptime
*uptime
= NULL
;
751 int tmp
, days
, hours
, minutes
, seconds
;
753 ret
= ctdb_ctrl_uptime(ctdb
, ctdb
, TIMELIMIT(), options
.pnn
, &uptime
);
755 DEBUG(DEBUG_ERR
, ("Unable to get uptime from node %u\n", options
.pnn
));
759 if (options
.machinereadable
){
760 printf(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
761 printf(":%u:%u:%u:%lf\n",
762 (unsigned int)uptime
->current_time
.tv_sec
,
763 (unsigned int)uptime
->ctdbd_start_time
.tv_sec
,
764 (unsigned int)uptime
->last_recovery_finished
.tv_sec
,
765 timeval_delta(&uptime
->last_recovery_finished
,
766 &uptime
->last_recovery_started
)
771 printf("Current time of node : %s", ctime(&uptime
->current_time
.tv_sec
));
773 tmp
= uptime
->current_time
.tv_sec
- uptime
->ctdbd_start_time
.tv_sec
;
781 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days
, hours
, minutes
, seconds
, ctime(&uptime
->ctdbd_start_time
.tv_sec
));
783 tmp
= uptime
->current_time
.tv_sec
- uptime
->last_recovery_finished
.tv_sec
;
791 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days
, hours
, minutes
, seconds
, ctime(&uptime
->last_recovery_finished
.tv_sec
));
793 printf("Duration of last recovery/failover: %lf seconds\n",
794 timeval_delta(&uptime
->last_recovery_finished
,
795 &uptime
->last_recovery_started
));
801 show the PNN of the current node
803 static int control_pnn(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
807 mypnn
= getpnn(ctdb
);
809 printf("PNN:%d\n", mypnn
);
815 struct pnn_node
*next
, *prev
;
820 static struct pnn_node
*read_pnn_node_file(TALLOC_CTX
*mem_ctx
,
826 struct pnn_node
*pnn_nodes
= NULL
;
827 struct pnn_node
*pnn_node
;
829 lines
= file_lines_load(file
, &nlines
, mem_ctx
);
833 for (i
=0, pnn
=0; i
<nlines
; i
++) {
837 /* strip leading spaces */
838 while((*node
== ' ') || (*node
== '\t')) {
845 if (strcmp(node
, "") == 0) {
848 pnn_node
= talloc(mem_ctx
, struct pnn_node
);
849 pnn_node
->pnn
= pnn
++;
851 if (!parse_ip(node
, NULL
, 0, &pnn_node
->addr
)) {
853 ("Invalid IP address '%s' in file %s\n",
855 /* Caller will free mem_ctx */
859 DLIST_ADD_END(pnn_nodes
, pnn_node
, NULL
);
865 static struct pnn_node
*read_nodes_file(TALLOC_CTX
*mem_ctx
)
867 const char *nodes_list
;
869 /* read the nodes file */
870 nodes_list
= getenv("CTDB_NODES");
871 if (nodes_list
== NULL
) {
872 nodes_list
= talloc_asprintf(mem_ctx
, "%s/nodes",
873 getenv("CTDB_BASE"));
874 if (nodes_list
== NULL
) {
875 DEBUG(DEBUG_ALERT
,(__location__
" Out of memory\n"));
880 return read_pnn_node_file(mem_ctx
, nodes_list
);
884 show the PNN of the current node
885 discover the pnn by loading the nodes file and try to bind to all
886 addresses one at a time until the ip address is found.
888 static int find_node_xpnn(void)
890 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
891 struct pnn_node
*pnn_nodes
;
892 struct pnn_node
*pnn_node
;
894 pnn_nodes
= read_nodes_file(mem_ctx
);
895 if (pnn_nodes
== NULL
) {
896 DEBUG(DEBUG_ERR
,("Failed to read nodes file\n"));
897 talloc_free(mem_ctx
);
901 for(pnn_node
=pnn_nodes
;pnn_node
;pnn_node
=pnn_node
->next
) {
902 if (ctdb_sys_have_ip(&pnn_node
->addr
)) {
903 talloc_free(mem_ctx
);
904 return pnn_node
->pnn
;
908 printf("Failed to detect which PNN this node is\n");
909 talloc_free(mem_ctx
);
913 static int control_xpnn(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
917 assert_single_node_only();
919 pnn
= find_node_xpnn();
924 printf("PNN:%d\n", pnn
);
928 /* Helpers for ctdb status
930 static bool is_partially_online(struct ctdb_context
*ctdb
, struct ctdb_node_and_flags
*node
)
932 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
936 if (node
->flags
== 0) {
937 struct ctdb_control_get_ifaces
*ifaces
;
939 if (ctdb_ctrl_get_ifaces(ctdb
, TIMELIMIT(), node
->pnn
,
940 tmp_ctx
, &ifaces
) == 0) {
941 for (j
=0; j
< ifaces
->num
; j
++) {
942 if (ifaces
->ifaces
[j
].link_state
!= 0) {
950 talloc_free(tmp_ctx
);
955 static void control_status_header_machine(void)
957 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
958 ":Inactive:PartiallyOnline:ThisNode:\n");
961 static int control_status_1_machine(struct ctdb_context
*ctdb
, int mypnn
,
962 struct ctdb_node_and_flags
*node
)
964 printf(":%d:%s:%d:%d:%d:%d:%d:%d:%d:%c:\n", node
->pnn
,
965 ctdb_addr_to_str(&node
->addr
),
966 !!(node
->flags
&NODE_FLAGS_DISCONNECTED
),
967 !!(node
->flags
&NODE_FLAGS_BANNED
),
968 !!(node
->flags
&NODE_FLAGS_PERMANENTLY_DISABLED
),
969 !!(node
->flags
&NODE_FLAGS_UNHEALTHY
),
970 !!(node
->flags
&NODE_FLAGS_STOPPED
),
971 !!(node
->flags
&NODE_FLAGS_INACTIVE
),
972 is_partially_online(ctdb
, node
) ? 1 : 0,
973 (node
->pnn
== mypnn
)?'Y':'N');
978 static int control_status_1_human(struct ctdb_context
*ctdb
, int mypnn
,
979 struct ctdb_node_and_flags
*node
)
981 printf("pnn:%d %-16s %s%s\n", node
->pnn
,
982 ctdb_addr_to_str(&node
->addr
),
983 is_partially_online(ctdb
, node
) ? "PARTIALLYONLINE" : pretty_print_flags(node
->flags
),
984 node
->pnn
== mypnn
?" (THIS NODE)":"");
990 display remote ctdb status
992 static int control_status(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
994 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
996 struct ctdb_vnn_map
*vnnmap
=NULL
;
997 struct ctdb_node_map
*nodemap
=NULL
;
998 uint32_t recmode
, recmaster
, mypnn
;
999 int num_deleted_nodes
= 0;
1002 mypnn
= getpnn(ctdb
);
1004 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &nodemap
);
1006 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
1007 talloc_free(tmp_ctx
);
1011 if (options
.machinereadable
) {
1012 control_status_header_machine();
1013 for (i
=0;i
<nodemap
->num
;i
++) {
1014 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DELETED
) {
1017 (void) control_status_1_machine(ctdb
, mypnn
,
1018 &nodemap
->nodes
[i
]);
1020 talloc_free(tmp_ctx
);
1024 for (i
=0; i
<nodemap
->num
; i
++) {
1025 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DELETED
) {
1026 num_deleted_nodes
++;
1029 if (num_deleted_nodes
== 0) {
1030 printf("Number of nodes:%d\n", nodemap
->num
);
1032 printf("Number of nodes:%d (including %d deleted nodes)\n",
1033 nodemap
->num
, num_deleted_nodes
);
1035 for(i
=0;i
<nodemap
->num
;i
++){
1036 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DELETED
) {
1039 (void) control_status_1_human(ctdb
, mypnn
, &nodemap
->nodes
[i
]);
1042 ret
= ctdb_ctrl_getvnnmap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &vnnmap
);
1044 DEBUG(DEBUG_ERR
, ("Unable to get vnnmap from node %u\n", options
.pnn
));
1045 talloc_free(tmp_ctx
);
1048 if (vnnmap
->generation
== INVALID_GENERATION
) {
1049 printf("Generation:INVALID\n");
1051 printf("Generation:%d\n",vnnmap
->generation
);
1053 printf("Size:%d\n",vnnmap
->size
);
1054 for(i
=0;i
<vnnmap
->size
;i
++){
1055 printf("hash:%d lmaster:%d\n", i
, vnnmap
->map
[i
]);
1058 ret
= ctdb_ctrl_getrecmode(ctdb
, tmp_ctx
, TIMELIMIT(), options
.pnn
, &recmode
);
1060 DEBUG(DEBUG_ERR
, ("Unable to get recmode from node %u\n", options
.pnn
));
1061 talloc_free(tmp_ctx
);
1064 printf("Recovery mode:%s (%d)\n",recmode
==CTDB_RECOVERY_NORMAL
?"NORMAL":"RECOVERY",recmode
);
1066 ret
= ctdb_ctrl_getrecmaster(ctdb
, tmp_ctx
, TIMELIMIT(), options
.pnn
, &recmaster
);
1068 DEBUG(DEBUG_ERR
, ("Unable to get recmaster from node %u\n", options
.pnn
));
1069 talloc_free(tmp_ctx
);
1072 printf("Recovery master:%d\n",recmaster
);
1074 talloc_free(tmp_ctx
);
1078 static int control_nodestatus(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1080 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1082 struct ctdb_node_map
*nodemap
=NULL
;
1084 uint32_t pnn_mode
, mypnn
;
1090 if (!parse_nodestring(ctdb
, tmp_ctx
, argc
== 1 ? argv
[0] : NULL
,
1091 options
.pnn
, true, &nodes
, &pnn_mode
)) {
1095 if (options
.machinereadable
) {
1096 control_status_header_machine();
1097 } else if (pnn_mode
== CTDB_BROADCAST_ALL
) {
1098 printf("Number of nodes:%d\n", (int) talloc_array_length(nodes
));
1101 mypnn
= getpnn(ctdb
);
1103 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &nodemap
);
1105 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
1106 talloc_free(tmp_ctx
);
1112 for (i
= 0; i
< talloc_array_length(nodes
); i
++) {
1113 if (options
.machinereadable
) {
1114 ret
|= control_status_1_machine(ctdb
, mypnn
,
1115 &nodemap
->nodes
[nodes
[i
]]);
1117 ret
|= control_status_1_human(ctdb
, mypnn
,
1118 &nodemap
->nodes
[nodes
[i
]]);
1122 talloc_free(tmp_ctx
);
1126 static struct pnn_node
*read_natgw_nodes_file(struct ctdb_context
*ctdb
,
1127 TALLOC_CTX
*mem_ctx
)
1129 const char *natgw_list
;
1130 struct pnn_node
*natgw_nodes
= NULL
;
1132 natgw_list
= getenv("CTDB_NATGW_NODES");
1133 if (natgw_list
== NULL
) {
1134 natgw_list
= talloc_asprintf(mem_ctx
, "%s/natgw_nodes",
1135 getenv("CTDB_BASE"));
1136 if (natgw_list
== NULL
) {
1137 DEBUG(DEBUG_ALERT
,(__location__
" Out of memory\n"));
1141 /* The PNNs will be junk but they're not used */
1142 natgw_nodes
= read_pnn_node_file(mem_ctx
, natgw_list
);
1143 if (natgw_nodes
== NULL
) {
1145 ("Failed to load natgw node list '%s'\n", natgw_list
));
1151 /* talloc off the existing nodemap... */
1152 static struct ctdb_node_map
*talloc_nodemap(struct ctdb_node_map
*nodemap
)
1154 return talloc_zero_size(nodemap
,
1155 offsetof(struct ctdb_node_map
, nodes
) +
1156 nodemap
->num
* sizeof(struct ctdb_node_and_flags
));
1159 static struct ctdb_node_map
*
1160 filter_nodemap_by_addrs(struct ctdb_context
*ctdb
,
1161 struct ctdb_node_map
*nodemap
,
1162 struct pnn_node
*nodes
)
1166 struct ctdb_node_map
*ret
;
1168 ret
= talloc_nodemap(nodemap
);
1169 CTDB_NO_MEMORY_NULL(ctdb
, ret
);
1173 for (i
= 0; i
< nodemap
->num
; i
++) {
1174 for(n
= nodes
; n
!= NULL
; n
= n
->next
) {
1175 if (ctdb_same_ip(&n
->addr
,
1176 &nodemap
->nodes
[i
].addr
)) {
1184 ret
->nodes
[ret
->num
] = nodemap
->nodes
[i
];
1191 static struct ctdb_node_map
*
1192 filter_nodemap_by_capabilities(struct ctdb_context
*ctdb
,
1193 struct ctdb_node_map
*nodemap
,
1194 uint32_t required_capabilities
,
1198 uint32_t capabilities
;
1199 struct ctdb_node_map
*ret
;
1201 ret
= talloc_nodemap(nodemap
);
1202 CTDB_NO_MEMORY_NULL(ctdb
, ret
);
1206 for (i
= 0; i
< nodemap
->num
; i
++) {
1209 /* Disconnected nodes have no capabilities! */
1210 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DISCONNECTED
) {
1214 res
= ctdb_ctrl_getcapabilities(ctdb
, TIMELIMIT(),
1215 nodemap
->nodes
[i
].pnn
,
1218 DEBUG(DEBUG_ERR
, ("Unable to get capabilities from node %u\n",
1219 nodemap
->nodes
[i
].pnn
));
1223 if (!(capabilities
& required_capabilities
)) {
1227 ret
->nodes
[ret
->num
] = nodemap
->nodes
[i
];
1237 static struct ctdb_node_map
*
1238 filter_nodemap_by_flags(struct ctdb_context
*ctdb
,
1239 struct ctdb_node_map
*nodemap
,
1240 uint32_t flags_mask
)
1243 struct ctdb_node_map
*ret
;
1245 ret
= talloc_nodemap(nodemap
);
1246 CTDB_NO_MEMORY_NULL(ctdb
, ret
);
1250 for (i
= 0; i
< nodemap
->num
; i
++) {
1251 if (nodemap
->nodes
[i
].flags
& flags_mask
) {
1255 ret
->nodes
[ret
->num
] = nodemap
->nodes
[i
];
1263 display the list of nodes belonging to this natgw configuration
1265 static int control_natgwlist(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1267 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1269 struct pnn_node
*natgw_nodes
= NULL
;
1270 struct ctdb_node_map
*orig_nodemap
=NULL
;
1271 struct ctdb_node_map
*nodemap
;
1272 uint32_t mypnn
, pnn
;
1275 /* When we have some nodes that could be the NATGW, make a
1276 * series of attempts to find the first node that doesn't have
1277 * certain status flags set.
1279 uint32_t exclude_flags
[] = {
1280 /* Look for a nice healthy node */
1281 NODE_FLAGS_DISCONNECTED
|NODE_FLAGS_STOPPED
|NODE_FLAGS_DELETED
|NODE_FLAGS_BANNED
|NODE_FLAGS_UNHEALTHY
,
1282 /* If not found, an UNHEALTHY/BANNED node will do */
1283 NODE_FLAGS_DISCONNECTED
|NODE_FLAGS_STOPPED
|NODE_FLAGS_DELETED
,
1284 /* If not found, a STOPPED node will do */
1285 NODE_FLAGS_DISCONNECTED
|NODE_FLAGS_DELETED
,
1289 /* read the natgw nodes file into a linked list */
1290 natgw_nodes
= read_natgw_nodes_file(ctdb
, tmp_ctx
);
1291 if (natgw_nodes
== NULL
) {
1296 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
,
1297 tmp_ctx
, &orig_nodemap
);
1299 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from local node.\n"));
1300 talloc_free(tmp_ctx
);
1304 /* Get a nodemap that includes only the nodes in the NATGW
1306 nodemap
= filter_nodemap_by_addrs(ctdb
, orig_nodemap
, natgw_nodes
);
1307 if (nodemap
== NULL
) {
1312 ret
= 2; /* matches ENOENT */
1315 /* For each flag mask... */
1316 for (i
= 0; exclude_flags
[i
] != 0; i
++) {
1317 /* ... get a nodemap that excludes nodes with with
1318 * masked flags... */
1319 struct ctdb_node_map
*t
=
1320 filter_nodemap_by_flags(ctdb
, nodemap
,
1328 /* ... and find the first node with the NATGW
1330 struct ctdb_node_map
*n
;
1331 n
= filter_nodemap_by_capabilities(ctdb
, t
,
1341 pnn
= n
->nodes
[0].pnn
;
1342 ip
= ctdb_addr_to_str(&n
->nodes
[0].addr
);
1349 if (options
.machinereadable
) {
1350 printf(":Node:IP:\n");
1351 printf(":%d:%s:\n", pnn
, ip
);
1353 printf("%d %s\n", pnn
, ip
);
1356 /* print the pruned list of nodes belonging to this natgw list */
1357 mypnn
= getpnn(ctdb
);
1358 if (options
.machinereadable
) {
1359 control_status_header_machine();
1361 printf("Number of nodes:%d\n", nodemap
->num
);
1363 for(i
=0;i
<nodemap
->num
;i
++){
1364 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DELETED
) {
1367 if (options
.machinereadable
) {
1368 control_status_1_machine(ctdb
, mypnn
, &(nodemap
->nodes
[i
]));
1370 control_status_1_human(ctdb
, mypnn
, &(nodemap
->nodes
[i
]));
1375 talloc_free(tmp_ctx
);
1380 display the status of the scripts for monitoring (or other events)
1382 static int control_one_scriptstatus(struct ctdb_context
*ctdb
,
1383 enum ctdb_eventscript_call type
)
1385 struct ctdb_scripts_wire
*script_status
;
1388 ret
= ctdb_ctrl_getscriptstatus(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, type
, &script_status
);
1390 DEBUG(DEBUG_ERR
, ("Unable to get script status from node %u\n", options
.pnn
));
1394 if (script_status
== NULL
) {
1395 if (!options
.machinereadable
) {
1396 printf("%s cycle never run\n",
1397 ctdb_eventscript_call_names
[type
]);
1402 if (!options
.machinereadable
) {
1404 for (i
=0; i
<script_status
->num_scripts
; i
++) {
1405 if (script_status
->scripts
[i
].status
!= -ENOEXEC
) {
1409 printf("%d scripts were executed last %s cycle\n",
1411 ctdb_eventscript_call_names
[type
]);
1413 for (i
=0; i
<script_status
->num_scripts
; i
++) {
1414 const char *status
= NULL
;
1416 switch (script_status
->scripts
[i
].status
) {
1418 status
= "TIMEDOUT";
1421 status
= "DISABLED";
1427 if (script_status
->scripts
[i
].status
> 0)
1431 if (options
.machinereadable
) {
1432 printf(":%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
1433 ctdb_eventscript_call_names
[type
],
1434 script_status
->scripts
[i
].name
,
1435 script_status
->scripts
[i
].status
,
1437 (long)script_status
->scripts
[i
].start
.tv_sec
,
1438 (long)script_status
->scripts
[i
].start
.tv_usec
,
1439 (long)script_status
->scripts
[i
].finished
.tv_sec
,
1440 (long)script_status
->scripts
[i
].finished
.tv_usec
,
1441 script_status
->scripts
[i
].output
);
1445 printf("%-20s Status:%s ",
1446 script_status
->scripts
[i
].name
, status
);
1448 /* Some other error, eg from stat. */
1449 printf("%-20s Status:CANNOT RUN (%s)",
1450 script_status
->scripts
[i
].name
,
1451 strerror(-script_status
->scripts
[i
].status
));
1453 if (script_status
->scripts
[i
].status
>= 0) {
1454 printf("Duration:%.3lf ",
1455 timeval_delta(&script_status
->scripts
[i
].finished
,
1456 &script_status
->scripts
[i
].start
));
1458 if (script_status
->scripts
[i
].status
!= -ENOEXEC
) {
1460 ctime(&script_status
->scripts
[i
].start
.tv_sec
));
1461 if (script_status
->scripts
[i
].status
!= 0) {
1462 printf(" OUTPUT:%s\n",
1463 script_status
->scripts
[i
].output
);
1473 static int control_scriptstatus(struct ctdb_context
*ctdb
,
1474 int argc
, const char **argv
)
1477 enum ctdb_eventscript_call type
, min
, max
;
1481 DEBUG(DEBUG_ERR
, ("Unknown arguments to scriptstatus\n"));
1486 arg
= ctdb_eventscript_call_names
[CTDB_EVENT_MONITOR
];
1490 for (type
= 0; type
< CTDB_EVENT_MAX
; type
++) {
1491 if (strcmp(arg
, ctdb_eventscript_call_names
[type
]) == 0) {
1497 if (type
== CTDB_EVENT_MAX
) {
1498 if (strcmp(arg
, "all") == 0) {
1500 max
= CTDB_EVENT_MAX
;
1502 DEBUG(DEBUG_ERR
, ("Unknown event type %s\n", argv
[0]));
1507 if (options
.machinereadable
) {
1508 printf(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1511 for (type
= min
; type
< max
; type
++) {
1512 ret
= control_one_scriptstatus(ctdb
, type
);
1522 enable an eventscript
1524 static int control_enablescript(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1532 ret
= ctdb_ctrl_enablescript(ctdb
, TIMELIMIT(), options
.pnn
, argv
[0]);
1534 DEBUG(DEBUG_ERR
, ("Unable to enable script %s on node %u\n", argv
[0], options
.pnn
));
1542 disable an eventscript
1544 static int control_disablescript(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1552 ret
= ctdb_ctrl_disablescript(ctdb
, TIMELIMIT(), options
.pnn
, argv
[0]);
1554 DEBUG(DEBUG_ERR
, ("Unable to disable script %s on node %u\n", argv
[0], options
.pnn
));
1562 display the pnn of the recovery master
1564 static int control_recmaster(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1569 ret
= ctdb_ctrl_getrecmaster(ctdb
, ctdb
, TIMELIMIT(), options
.pnn
, &recmaster
);
1571 DEBUG(DEBUG_ERR
, ("Unable to get recmaster from node %u\n", options
.pnn
));
1574 printf("%d\n",recmaster
);
1580 add a tickle to a public address
1582 static int control_add_tickle(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1584 struct ctdb_tcp_connection t
;
1588 assert_single_node_only();
1594 if (parse_ip_port(argv
[0], &t
.src_addr
) == 0) {
1595 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
1598 if (parse_ip_port(argv
[1], &t
.dst_addr
) == 0) {
1599 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[1]));
1603 data
.dptr
= (uint8_t *)&t
;
1604 data
.dsize
= sizeof(t
);
1606 /* tell all nodes about this tcp connection */
1607 ret
= ctdb_control(ctdb
, options
.pnn
, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE
,
1608 0, data
, ctdb
, NULL
, NULL
, NULL
, NULL
);
1610 DEBUG(DEBUG_ERR
,("Failed to add tickle\n"));
1619 delete a tickle from a node
1621 static int control_del_tickle(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1623 struct ctdb_tcp_connection t
;
1627 assert_single_node_only();
1633 if (parse_ip_port(argv
[0], &t
.src_addr
) == 0) {
1634 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
1637 if (parse_ip_port(argv
[1], &t
.dst_addr
) == 0) {
1638 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[1]));
1642 data
.dptr
= (uint8_t *)&t
;
1643 data
.dsize
= sizeof(t
);
1645 /* tell all nodes about this tcp connection */
1646 ret
= ctdb_control(ctdb
, options
.pnn
, 0, CTDB_CONTROL_TCP_REMOVE
,
1647 0, data
, ctdb
, NULL
, NULL
, NULL
, NULL
);
1649 DEBUG(DEBUG_ERR
,("Failed to remove tickle\n"));
1658 get a list of all tickles for this pnn
1660 static int control_get_tickles(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1662 struct ctdb_control_tcp_tickle_list
*list
;
1663 ctdb_sock_addr addr
;
1667 assert_single_node_only();
1674 port
= atoi(argv
[1]);
1677 if (parse_ip(argv
[0], NULL
, 0, &addr
) == 0) {
1678 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
1682 ret
= ctdb_ctrl_get_tcp_tickles(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &addr
, &list
);
1684 DEBUG(DEBUG_ERR
, ("Unable to list tickles\n"));
1688 if (options
.machinereadable
){
1689 printf(":source ip:port:destination ip:port:\n");
1690 for (i
=0;i
<list
->tickles
.num
;i
++) {
1691 if (port
&& port
!= ntohs(list
->tickles
.connections
[i
].dst_addr
.ip
.sin_port
)) {
1694 printf(":%s:%u", ctdb_addr_to_str(&list
->tickles
.connections
[i
].src_addr
), ntohs(list
->tickles
.connections
[i
].src_addr
.ip
.sin_port
));
1695 printf(":%s:%u:\n", ctdb_addr_to_str(&list
->tickles
.connections
[i
].dst_addr
), ntohs(list
->tickles
.connections
[i
].dst_addr
.ip
.sin_port
));
1698 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list
->addr
));
1699 printf("Num tickles:%u\n", list
->tickles
.num
);
1700 for (i
=0;i
<list
->tickles
.num
;i
++) {
1701 if (port
&& port
!= ntohs(list
->tickles
.connections
[i
].dst_addr
.ip
.sin_port
)) {
1704 printf("SRC: %s:%u ", ctdb_addr_to_str(&list
->tickles
.connections
[i
].src_addr
), ntohs(list
->tickles
.connections
[i
].src_addr
.ip
.sin_port
));
1705 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
));
1715 static int move_ip(struct ctdb_context
*ctdb
, ctdb_sock_addr
*addr
, uint32_t pnn
)
1717 struct ctdb_all_public_ips
*ips
;
1718 struct ctdb_public_ip ip
;
1721 uint32_t disable_time
;
1723 struct ctdb_node_map
*nodemap
=NULL
;
1724 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1727 data
.dptr
= (uint8_t*)&disable_time
;
1728 data
.dsize
= sizeof(disable_time
);
1729 ret
= ctdb_client_send_message(ctdb
, CTDB_BROADCAST_CONNECTED
, CTDB_SRVID_DISABLE_IP_CHECK
, data
);
1731 DEBUG(DEBUG_ERR
,("Failed to send message to disable ipcheck\n"));
1737 /* read the public ip list from the node */
1738 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), pnn
, ctdb
, &ips
);
1740 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from node %u\n", pnn
));
1741 talloc_free(tmp_ctx
);
1745 for (i
=0;i
<ips
->num
;i
++) {
1746 if (ctdb_same_ip(addr
, &ips
->ips
[i
].addr
)) {
1751 DEBUG(DEBUG_ERR
, ("Node %u can not host ip address '%s'\n",
1752 pnn
, ctdb_addr_to_str(addr
)));
1753 talloc_free(tmp_ctx
);
1760 data
.dptr
= (uint8_t *)&ip
;
1761 data
.dsize
= sizeof(ip
);
1763 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &nodemap
);
1765 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
1766 talloc_free(tmp_ctx
);
1770 nodes
= list_of_nodes(ctdb
, nodemap
, tmp_ctx
, NODE_FLAGS_INACTIVE
, pnn
);
1771 ret
= ctdb_client_async_control(ctdb
, CTDB_CONTROL_RELEASE_IP
,
1778 DEBUG(DEBUG_ERR
,("Failed to release IP on nodes\n"));
1779 talloc_free(tmp_ctx
);
1783 ret
= ctdb_ctrl_takeover_ip(ctdb
, LONGTIMELIMIT(), pnn
, &ip
);
1785 DEBUG(DEBUG_ERR
,("Failed to take over IP on node %d\n", pnn
));
1786 talloc_free(tmp_ctx
);
1790 /* update the recovery daemon so it now knows to expect the new
1791 node assignment for this ip.
1793 ret
= ctdb_client_send_message(ctdb
, CTDB_BROADCAST_CONNECTED
, CTDB_SRVID_RECD_UPDATE_IP
, data
);
1795 DEBUG(DEBUG_ERR
,("Failed to send message to update the ip on the recovery master.\n"));
1799 talloc_free(tmp_ctx
);
1805 * scans all other nodes and returns a pnn for another node that can host this
1809 find_other_host_for_public_ip(struct ctdb_context
*ctdb
, ctdb_sock_addr
*addr
)
1811 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1812 struct ctdb_all_public_ips
*ips
;
1813 struct ctdb_node_map
*nodemap
=NULL
;
1816 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, tmp_ctx
, &nodemap
);
1818 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
1819 talloc_free(tmp_ctx
);
1823 for(i
=0;i
<nodemap
->num
;i
++){
1824 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_INACTIVE
) {
1827 if (nodemap
->nodes
[i
].pnn
== options
.pnn
) {
1831 /* read the public ip list from this node */
1832 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), nodemap
->nodes
[i
].pnn
, tmp_ctx
, &ips
);
1834 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from node %u\n", nodemap
->nodes
[i
].pnn
));
1838 for (j
=0;j
<ips
->num
;j
++) {
1839 if (ctdb_same_ip(addr
, &ips
->ips
[j
].addr
)) {
1840 talloc_free(tmp_ctx
);
1841 return nodemap
->nodes
[i
].pnn
;
1847 talloc_free(tmp_ctx
);
1851 /* If pnn is -1 then try to find a node to move IP to... */
1852 static bool try_moveip(struct ctdb_context
*ctdb
, ctdb_sock_addr
*addr
, uint32_t pnn
)
1854 bool pnn_specified
= (pnn
== -1 ? false : true);
1857 while (retries
< 5) {
1858 if (!pnn_specified
) {
1859 pnn
= find_other_host_for_public_ip(ctdb
, addr
);
1864 ("Trying to move public IP to node %u\n", pnn
));
1867 if (move_ip(ctdb
, addr
, pnn
) == 0) {
1880 move/failover an ip address to a specific node
1882 static int control_moveip(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1885 ctdb_sock_addr addr
;
1887 assert_single_node_only();
1894 if (parse_ip(argv
[0], NULL
, 0, &addr
) == 0) {
1895 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
1900 if (sscanf(argv
[1], "%u", &pnn
) != 1) {
1901 DEBUG(DEBUG_ERR
, ("Badly formed pnn\n"));
1905 if (!try_moveip(ctdb
, &addr
, pnn
)) {
1906 DEBUG(DEBUG_ERR
,("Failed to move IP to node %d.\n", pnn
));
1913 static int rebalance_node(struct ctdb_context
*ctdb
, uint32_t pnn
)
1917 data
.dptr
= (uint8_t *)&pnn
;
1918 data
.dsize
= sizeof(uint32_t);
1919 if (ctdb_client_send_message(ctdb
, CTDB_BROADCAST_CONNECTED
, CTDB_SRVID_REBALANCE_NODE
, data
) != 0) {
1921 ("Failed to send message to force node %u to be a rebalancing target\n",
1931 rebalance a node by setting it to allow failback and triggering a
1934 static int control_rebalancenode(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1936 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1941 assert_single_node_only();
1947 /* Determine the nodes where IPs need to be reloaded */
1948 if (!parse_nodestring(ctdb
, tmp_ctx
, argc
== 1 ? argv
[0] : NULL
,
1949 options
.pnn
, true, &nodes
, &pnn_mode
)) {
1954 for (i
= 0; i
< talloc_array_length(nodes
); i
++) {
1955 if (!rebalance_node(ctdb
, nodes
[i
])) {
1961 talloc_free(tmp_ctx
);
1965 static int rebalance_ip(struct ctdb_context
*ctdb
, ctdb_sock_addr
*addr
)
1967 struct ctdb_public_ip ip
;
1970 uint32_t disable_time
;
1972 struct ctdb_node_map
*nodemap
=NULL
;
1973 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1976 data
.dptr
= (uint8_t*)&disable_time
;
1977 data
.dsize
= sizeof(disable_time
);
1978 ret
= ctdb_client_send_message(ctdb
, CTDB_BROADCAST_CONNECTED
, CTDB_SRVID_DISABLE_IP_CHECK
, data
);
1980 DEBUG(DEBUG_ERR
,("Failed to send message to disable ipcheck\n"));
1987 data
.dptr
= (uint8_t *)&ip
;
1988 data
.dsize
= sizeof(ip
);
1990 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &nodemap
);
1992 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
1993 talloc_free(tmp_ctx
);
1997 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
1998 ret
= ctdb_client_async_control(ctdb
, CTDB_CONTROL_RELEASE_IP
,
2005 DEBUG(DEBUG_ERR
,("Failed to release IP on nodes\n"));
2006 talloc_free(tmp_ctx
);
2010 talloc_free(tmp_ctx
);
2015 release an ip form all nodes and have it re-assigned by recd
2017 static int control_rebalanceip(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2019 ctdb_sock_addr addr
;
2021 assert_single_node_only();
2028 if (parse_ip(argv
[0], NULL
, 0, &addr
) == 0) {
2029 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
2033 if (rebalance_ip(ctdb
, &addr
) != 0) {
2034 DEBUG(DEBUG_ERR
,("Error when trying to reassign ip\n"));
2041 static int getips_store_callback(void *param
, void *data
)
2043 struct ctdb_public_ip
*node_ip
= (struct ctdb_public_ip
*)data
;
2044 struct ctdb_all_public_ips
*ips
= param
;
2048 ips
->ips
[i
].pnn
= node_ip
->pnn
;
2049 ips
->ips
[i
].addr
= node_ip
->addr
;
2053 static int getips_count_callback(void *param
, void *data
)
2055 uint32_t *count
= param
;
2062 static uint32_t *ip_key(ctdb_sock_addr
*ip
)
2064 static uint32_t key
[IP_KEYLEN
];
2066 bzero(key
, sizeof(key
));
2068 switch (ip
->sa
.sa_family
) {
2070 key
[0] = ip
->ip
.sin_addr
.s_addr
;
2073 uint32_t *s6_a32
= (uint32_t *)&(ip
->ip6
.sin6_addr
.s6_addr
);
2081 DEBUG(DEBUG_ERR
, (__location__
" ERROR, unknown family passed :%u\n", ip
->sa
.sa_family
));
2088 static void *add_ip_callback(void *parm
, void *data
)
2094 control_get_all_public_ips(struct ctdb_context
*ctdb
, TALLOC_CTX
*tmp_ctx
, struct ctdb_all_public_ips
**ips
)
2096 struct ctdb_all_public_ips
*tmp_ips
;
2097 struct ctdb_node_map
*nodemap
=NULL
;
2098 trbt_tree_t
*ip_tree
;
2102 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, tmp_ctx
, &nodemap
);
2104 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
2108 ip_tree
= trbt_create(tmp_ctx
, 0);
2110 for(i
=0;i
<nodemap
->num
;i
++){
2111 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DELETED
) {
2114 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DISCONNECTED
) {
2118 /* read the public ip list from this node */
2119 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), nodemap
->nodes
[i
].pnn
, tmp_ctx
, &tmp_ips
);
2121 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from node %u\n", nodemap
->nodes
[i
].pnn
));
2125 for (j
=0; j
<tmp_ips
->num
;j
++) {
2126 struct ctdb_public_ip
*node_ip
;
2128 node_ip
= talloc(tmp_ctx
, struct ctdb_public_ip
);
2129 node_ip
->pnn
= tmp_ips
->ips
[j
].pnn
;
2130 node_ip
->addr
= tmp_ips
->ips
[j
].addr
;
2132 trbt_insertarray32_callback(ip_tree
,
2133 IP_KEYLEN
, ip_key(&tmp_ips
->ips
[j
].addr
),
2137 talloc_free(tmp_ips
);
2142 trbt_traversearray32(ip_tree
, IP_KEYLEN
, getips_count_callback
, &count
);
2144 len
= offsetof(struct ctdb_all_public_ips
, ips
) +
2145 count
*sizeof(struct ctdb_public_ip
);
2146 tmp_ips
= talloc_zero_size(tmp_ctx
, len
);
2147 trbt_traversearray32(ip_tree
, IP_KEYLEN
, getips_store_callback
, tmp_ips
);
2155 static void ctdb_every_second(struct event_context
*ev
, struct timed_event
*te
, struct timeval t
, void *p
)
2157 struct ctdb_context
*ctdb
= talloc_get_type(p
, struct ctdb_context
);
2159 event_add_timed(ctdb
->ev
, ctdb
,
2160 timeval_current_ofs(1, 0),
2161 ctdb_every_second
, ctdb
);
2164 struct srvid_reply_handler_data
{
2168 const char *srvid_str
;
2171 static void srvid_broadcast_reply_handler(struct ctdb_context
*ctdb
,
2176 struct srvid_reply_handler_data
*d
=
2177 (struct srvid_reply_handler_data
*)private_data
;
2181 if (data
.dsize
!= sizeof(ret
)) {
2182 DEBUG(DEBUG_ERR
, (__location__
" Wrong reply size\n"));
2186 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
2187 ret
= *(int32_t *)data
.dptr
;
2190 ("%s failed with result %d\n", d
->srvid_str
, ret
));
2194 if (!d
->wait_for_all
) {
2199 /* Wait for all replies */
2201 for (i
= 0; i
< talloc_array_length(d
->nodes
); i
++) {
2202 if (d
->nodes
[i
] == ret
) {
2204 ("%s reply received from node %u\n",
2205 d
->srvid_str
, ret
));
2208 if (d
->nodes
[i
] != -1) {
2209 /* Found a node that hasn't yet replied */
2215 /* Broadcast the given SRVID to all connected nodes. Wait for 1 reply
2216 * or replies from all connected nodes. arg is the data argument to
2217 * pass in the srvid_request structure - pass 0 if this isn't needed.
2219 static int srvid_broadcast(struct ctdb_context
*ctdb
,
2220 uint64_t srvid
, uint32_t *arg
,
2221 const char *srvid_str
, bool wait_for_all
)
2226 uint64_t reply_srvid
;
2227 struct srvid_request request
;
2228 struct srvid_request_data request_data
;
2229 struct srvid_reply_handler_data reply_data
;
2232 ZERO_STRUCT(request
);
2234 /* Time ticks to enable timeouts to be processed */
2235 event_add_timed(ctdb
->ev
, ctdb
,
2236 timeval_current_ofs(1, 0),
2237 ctdb_every_second
, ctdb
);
2239 pnn
= ctdb_get_pnn(ctdb
);
2240 reply_srvid
= getpid();
2244 request
.srvid
= reply_srvid
;
2246 data
.dptr
= (uint8_t *)&request
;
2247 data
.dsize
= sizeof(request
);
2249 request_data
.pnn
= pnn
;
2250 request_data
.srvid
= reply_srvid
;
2251 request_data
.data
= *arg
;
2253 data
.dptr
= (uint8_t *)&request_data
;
2254 data
.dsize
= sizeof(request_data
);
2257 /* Register message port for reply from recovery master */
2258 ctdb_client_set_message_handler(ctdb
, reply_srvid
,
2259 srvid_broadcast_reply_handler
,
2262 reply_data
.wait_for_all
= wait_for_all
;
2263 reply_data
.nodes
= NULL
;
2264 reply_data
.srvid_str
= srvid_str
;
2267 reply_data
.done
= false;
2270 struct ctdb_node_map
*nodemap
;
2272 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(),
2273 CTDB_CURRENT_NODE
, ctdb
, &nodemap
);
2276 ("Unable to get nodemap from current node, try again\n"));
2281 if (reply_data
.nodes
!= NULL
) {
2282 talloc_free(reply_data
.nodes
);
2284 reply_data
.nodes
= list_of_connected_nodes(ctdb
, nodemap
,
2287 talloc_free(nodemap
);
2290 /* Send to all connected nodes. Only recmaster replies */
2291 ret
= ctdb_client_send_message(ctdb
, CTDB_BROADCAST_CONNECTED
,
2294 /* This can only happen if the socket is closed and
2295 * there's no way to recover from that, so don't try
2299 ("Failed to send %s request to connected nodes\n",
2304 tv
= timeval_current();
2305 /* This loop terminates the reply is received */
2306 while (timeval_elapsed(&tv
) < 5.0 && !reply_data
.done
) {
2307 event_loop_once(ctdb
->ev
);
2310 if (!reply_data
.done
) {
2312 ("Still waiting for confirmation of %s\n", srvid_str
));
2317 ctdb_client_remove_message_handler(ctdb
, reply_srvid
, &reply_data
);
2319 talloc_free(reply_data
.nodes
);
2324 static int ipreallocate(struct ctdb_context
*ctdb
)
2326 return srvid_broadcast(ctdb
, CTDB_SRVID_TAKEOVER_RUN
, NULL
,
2327 "IP reallocation", false);
2331 static int control_ipreallocate(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2333 return ipreallocate(ctdb
);
2337 add a public ip address to a node
2339 static int control_addip(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2342 int len
, retries
= 0;
2344 ctdb_sock_addr addr
;
2345 struct ctdb_control_ip_iface
*pub
;
2346 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
2347 struct ctdb_all_public_ips
*ips
;
2351 talloc_free(tmp_ctx
);
2355 if (!parse_ip_mask(argv
[0], argv
[1], &addr
, &mask
)) {
2356 DEBUG(DEBUG_ERR
, ("Badly formed ip/mask : %s\n", argv
[0]));
2357 talloc_free(tmp_ctx
);
2361 /* read the public ip list from the node */
2362 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &ips
);
2364 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from node %u\n", options
.pnn
));
2365 talloc_free(tmp_ctx
);
2368 for (i
=0;i
<ips
->num
;i
++) {
2369 if (ctdb_same_ip(&addr
, &ips
->ips
[i
].addr
)) {
2370 DEBUG(DEBUG_ERR
,("Can not add ip to node. Node already hosts this ip\n"));
2377 /* Dont timeout. This command waits for an ip reallocation
2378 which sometimes can take wuite a while if there has
2379 been a recent recovery
2383 len
= offsetof(struct ctdb_control_ip_iface
, iface
) + strlen(argv
[1]) + 1;
2384 pub
= talloc_size(tmp_ctx
, len
);
2385 CTDB_NO_MEMORY(ctdb
, pub
);
2389 pub
->len
= strlen(argv
[1])+1;
2390 memcpy(&pub
->iface
[0], argv
[1], strlen(argv
[1])+1);
2393 ret
= ctdb_ctrl_add_public_ip(ctdb
, TIMELIMIT(), options
.pnn
, pub
);
2395 DEBUG(DEBUG_ERR
, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options
.pnn
));
2399 } while (retries
< 5 && ret
!= 0);
2401 DEBUG(DEBUG_ERR
, ("Unable to add public ip to node %u. Giving up.\n", options
.pnn
));
2402 talloc_free(tmp_ctx
);
2406 if (rebalance_node(ctdb
, options
.pnn
) != 0) {
2407 DEBUG(DEBUG_ERR
,("Error when trying to rebalance node\n"));
2411 talloc_free(tmp_ctx
);
2416 add a public ip address to a node
2418 static int control_ipiface(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2420 ctdb_sock_addr addr
;
2426 if (!parse_ip(argv
[0], NULL
, 0, &addr
)) {
2427 printf("Badly formed ip : %s\n", argv
[0]);
2431 printf("IP on interface %s\n", ctdb_sys_find_ifname(&addr
));
2436 static int control_delip(struct ctdb_context
*ctdb
, int argc
, const char **argv
);
2438 static int control_delip_all(struct ctdb_context
*ctdb
, int argc
, const char **argv
, ctdb_sock_addr
*addr
)
2440 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
2441 struct ctdb_node_map
*nodemap
=NULL
;
2442 struct ctdb_all_public_ips
*ips
;
2445 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, tmp_ctx
, &nodemap
);
2447 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from current node\n"));
2451 /* remove it from the nodes that are not hosting the ip currently */
2452 for(i
=0;i
<nodemap
->num
;i
++){
2453 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_INACTIVE
) {
2456 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), nodemap
->nodes
[i
].pnn
, tmp_ctx
, &ips
);
2458 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from node %d\n", nodemap
->nodes
[i
].pnn
));
2462 for (j
=0;j
<ips
->num
;j
++) {
2463 if (ctdb_same_ip(addr
, &ips
->ips
[j
].addr
)) {
2471 if (ips
->ips
[j
].pnn
== nodemap
->nodes
[i
].pnn
) {
2475 options
.pnn
= nodemap
->nodes
[i
].pnn
;
2476 control_delip(ctdb
, argc
, argv
);
2480 /* remove it from every node (also the one hosting it) */
2481 for(i
=0;i
<nodemap
->num
;i
++){
2482 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_INACTIVE
) {
2485 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), nodemap
->nodes
[i
].pnn
, tmp_ctx
, &ips
);
2487 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from node %d\n", nodemap
->nodes
[i
].pnn
));
2491 for (j
=0;j
<ips
->num
;j
++) {
2492 if (ctdb_same_ip(addr
, &ips
->ips
[j
].addr
)) {
2500 options
.pnn
= nodemap
->nodes
[i
].pnn
;
2501 control_delip(ctdb
, argc
, argv
);
2504 talloc_free(tmp_ctx
);
2509 delete a public ip address from a node
2511 static int control_delip(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2514 ctdb_sock_addr addr
;
2515 struct ctdb_control_ip_iface pub
;
2516 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
2517 struct ctdb_all_public_ips
*ips
;
2520 talloc_free(tmp_ctx
);
2524 if (parse_ip(argv
[0], NULL
, 0, &addr
) == 0) {
2525 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
2529 if (options
.pnn
== CTDB_BROADCAST_ALL
) {
2530 return control_delip_all(ctdb
, argc
, argv
, &addr
);
2537 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &ips
);
2539 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from cluster\n"));
2540 talloc_free(tmp_ctx
);
2544 for (i
=0;i
<ips
->num
;i
++) {
2545 if (ctdb_same_ip(&addr
, &ips
->ips
[i
].addr
)) {
2551 DEBUG(DEBUG_ERR
, ("This node does not support this public address '%s'\n",
2552 ctdb_addr_to_str(&addr
)));
2553 talloc_free(tmp_ctx
);
2557 /* This is an optimisation. If this node is hosting the IP
2558 * then try to move it somewhere else without invoking a full
2559 * takeover run. We don't care if this doesn't work!
2561 if (ips
->ips
[i
].pnn
== options
.pnn
) {
2562 (void) try_moveip(ctdb
, &addr
, -1);
2565 ret
= ctdb_ctrl_del_public_ip(ctdb
, TIMELIMIT(), options
.pnn
, &pub
);
2567 DEBUG(DEBUG_ERR
, ("Unable to del public ip from node %u\n", options
.pnn
));
2568 talloc_free(tmp_ctx
);
2572 talloc_free(tmp_ctx
);
2576 static int kill_tcp_from_file(struct ctdb_context
*ctdb
,
2577 int argc
, const char **argv
)
2579 struct ctdb_control_killtcp
*killtcp
;
2580 int max_entries
, current
, i
;
2581 struct timeval timeout
;
2582 char line
[128], src
[128], dst
[128];
2585 struct client_async_data
*async_data
;
2586 struct ctdb_client_control_state
*state
;
2596 while (!feof(stdin
)) {
2597 if (fgets(line
, sizeof(line
), stdin
) == NULL
) {
2601 /* Silently skip empty lines */
2602 if (line
[0] == '\n') {
2606 if (sscanf(line
, "%s %s\n", src
, dst
) != 2) {
2607 DEBUG(DEBUG_ERR
, ("Bad line [%d]: '%s'\n",
2609 talloc_free(killtcp
);
2613 if (current
>= max_entries
) {
2614 max_entries
+= 1024;
2615 killtcp
= talloc_realloc(ctdb
, killtcp
,
2616 struct ctdb_control_killtcp
,
2618 CTDB_NO_MEMORY(ctdb
, killtcp
);
2621 if (!parse_ip_port(src
, &killtcp
[current
].src_addr
)) {
2622 DEBUG(DEBUG_ERR
, ("Bad IP:port on line [%d]: '%s'\n",
2624 talloc_free(killtcp
);
2628 if (!parse_ip_port(dst
, &killtcp
[current
].dst_addr
)) {
2629 DEBUG(DEBUG_ERR
, ("Bad IP:port on line [%d]: '%s'\n",
2631 talloc_free(killtcp
);
2638 async_data
= talloc_zero(ctdb
, struct client_async_data
);
2639 if (async_data
== NULL
) {
2640 talloc_free(killtcp
);
2644 for (i
= 0; i
< current
; i
++) {
2646 data
.dsize
= sizeof(struct ctdb_control_killtcp
);
2647 data
.dptr
= (unsigned char *)&killtcp
[i
];
2649 timeout
= TIMELIMIT();
2650 state
= ctdb_control_send(ctdb
, options
.pnn
, 0,
2651 CTDB_CONTROL_KILL_TCP
, 0, data
,
2652 async_data
, &timeout
, NULL
);
2654 if (state
== NULL
) {
2656 ("Failed to call async killtcp control to node %u\n",
2658 talloc_free(killtcp
);
2662 ctdb_client_async_add(async_data
, state
);
2665 if (ctdb_client_async_wait(ctdb
, async_data
) != 0) {
2666 DEBUG(DEBUG_ERR
,("killtcp failed\n"));
2667 talloc_free(killtcp
);
2671 talloc_free(killtcp
);
2677 kill a tcp connection
2679 static int kill_tcp(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2682 struct ctdb_control_killtcp killtcp
;
2684 assert_single_node_only();
2687 return kill_tcp_from_file(ctdb
, argc
, argv
);
2694 if (!parse_ip_port(argv
[0], &killtcp
.src_addr
)) {
2695 DEBUG(DEBUG_ERR
, ("Bad IP:port '%s'\n", argv
[0]));
2699 if (!parse_ip_port(argv
[1], &killtcp
.dst_addr
)) {
2700 DEBUG(DEBUG_ERR
, ("Bad IP:port '%s'\n", argv
[1]));
2704 ret
= ctdb_ctrl_killtcp(ctdb
, TIMELIMIT(), options
.pnn
, &killtcp
);
2706 DEBUG(DEBUG_ERR
, ("Unable to killtcp from node %u\n", options
.pnn
));
2717 static int control_gratious_arp(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2720 ctdb_sock_addr addr
;
2722 assert_single_node_only();
2728 if (!parse_ip(argv
[0], NULL
, 0, &addr
)) {
2729 DEBUG(DEBUG_ERR
, ("Bad IP '%s'\n", argv
[0]));
2733 ret
= ctdb_ctrl_gratious_arp(ctdb
, TIMELIMIT(), options
.pnn
, &addr
, argv
[1]);
2735 DEBUG(DEBUG_ERR
, ("Unable to send gratious_arp from node %u\n", options
.pnn
));
2743 register a server id
2745 static int regsrvid(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2748 struct ctdb_server_id server_id
;
2754 server_id
.pnn
= strtoul(argv
[0], NULL
, 0);
2755 server_id
.type
= strtoul(argv
[1], NULL
, 0);
2756 server_id
.server_id
= strtoul(argv
[2], NULL
, 0);
2758 ret
= ctdb_ctrl_register_server_id(ctdb
, TIMELIMIT(), &server_id
);
2760 DEBUG(DEBUG_ERR
, ("Unable to register server_id from node %u\n", options
.pnn
));
2763 DEBUG(DEBUG_ERR
,("Srvid registered. Sleeping for 999 seconds\n"));
2769 unregister a server id
2771 static int unregsrvid(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2774 struct ctdb_server_id server_id
;
2780 server_id
.pnn
= strtoul(argv
[0], NULL
, 0);
2781 server_id
.type
= strtoul(argv
[1], NULL
, 0);
2782 server_id
.server_id
= strtoul(argv
[2], NULL
, 0);
2784 ret
= ctdb_ctrl_unregister_server_id(ctdb
, TIMELIMIT(), &server_id
);
2786 DEBUG(DEBUG_ERR
, ("Unable to unregister server_id from node %u\n", options
.pnn
));
2793 check if a server id exists
2795 static int chksrvid(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2799 struct ctdb_server_id server_id
;
2805 server_id
.pnn
= strtoul(argv
[0], NULL
, 0);
2806 server_id
.type
= strtoul(argv
[1], NULL
, 0);
2807 server_id
.server_id
= strtoul(argv
[2], NULL
, 0);
2809 ret
= ctdb_ctrl_check_server_id(ctdb
, TIMELIMIT(), options
.pnn
, &server_id
, &status
);
2811 DEBUG(DEBUG_ERR
, ("Unable to check server_id from node %u\n", options
.pnn
));
2816 printf("Server id %d:%d:%d EXISTS\n", server_id
.pnn
, server_id
.type
, server_id
.server_id
);
2818 printf("Server id %d:%d:%d does NOT exist\n", server_id
.pnn
, server_id
.type
, server_id
.server_id
);
2824 get a list of all server ids that are registered on a node
2826 static int getsrvids(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2829 struct ctdb_server_id_list
*server_ids
;
2831 ret
= ctdb_ctrl_get_server_id_list(ctdb
, ctdb
, TIMELIMIT(), options
.pnn
, &server_ids
);
2833 DEBUG(DEBUG_ERR
, ("Unable to get server_id list from node %u\n", options
.pnn
));
2837 for (i
=0; i
<server_ids
->num
; i
++) {
2838 printf("Server id %d:%d:%d\n",
2839 server_ids
->server_ids
[i
].pnn
,
2840 server_ids
->server_ids
[i
].type
,
2841 server_ids
->server_ids
[i
].server_id
);
2848 check if a server id exists
2850 static int check_srvids(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2852 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
2858 talloc_free(tmp_ctx
);
2862 ids
= talloc_array(tmp_ctx
, uint64_t, argc
);
2863 result
= talloc_array(tmp_ctx
, uint8_t, argc
);
2865 for (i
= 0; i
< argc
; i
++) {
2866 ids
[i
] = strtoull(argv
[i
], NULL
, 0);
2869 if (!ctdb_client_check_message_handlers(ctdb
, ids
, argc
, result
)) {
2870 DEBUG(DEBUG_ERR
, ("Unable to check server_id from node %u\n",
2872 talloc_free(tmp_ctx
);
2876 for (i
=0; i
< argc
; i
++) {
2877 printf("Server id %d:%llu %s\n", options
.pnn
, (long long)ids
[i
],
2878 result
[i
] ? "exists" : "does not exist");
2881 talloc_free(tmp_ctx
);
2886 send a tcp tickle ack
2888 static int tickle_tcp(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2891 ctdb_sock_addr src
, dst
;
2897 if (!parse_ip_port(argv
[0], &src
)) {
2898 DEBUG(DEBUG_ERR
, ("Bad IP:port '%s'\n", argv
[0]));
2902 if (!parse_ip_port(argv
[1], &dst
)) {
2903 DEBUG(DEBUG_ERR
, ("Bad IP:port '%s'\n", argv
[1]));
2907 ret
= ctdb_sys_send_tcp(&src
, &dst
, 0, 0, 0);
2911 DEBUG(DEBUG_ERR
, ("Error while sending tickle ack\n"));
2918 display public ip status
2920 static int control_ip(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2923 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
2924 struct ctdb_all_public_ips
*ips
;
2926 if (options
.pnn
== CTDB_BROADCAST_ALL
) {
2927 /* read the list of public ips from all nodes */
2928 ret
= control_get_all_public_ips(ctdb
, tmp_ctx
, &ips
);
2930 /* read the public ip list from this node */
2931 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &ips
);
2934 DEBUG(DEBUG_ERR
, ("Unable to get public ips from node %u\n", options
.pnn
));
2935 talloc_free(tmp_ctx
);
2939 if (options
.machinereadable
){
2940 printf(":Public IP:Node:");
2941 if (options
.verbose
){
2942 printf("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2946 if (options
.pnn
== CTDB_BROADCAST_ALL
) {
2947 printf("Public IPs on ALL nodes\n");
2949 printf("Public IPs on node %u\n", options
.pnn
);
2953 for (i
=1;i
<=ips
->num
;i
++) {
2954 struct ctdb_control_public_ip_info
*info
= NULL
;
2956 char *aciface
= NULL
;
2957 char *avifaces
= NULL
;
2958 char *cifaces
= NULL
;
2960 if (options
.pnn
== CTDB_BROADCAST_ALL
) {
2961 pnn
= ips
->ips
[ips
->num
-i
].pnn
;
2967 ret
= ctdb_ctrl_get_public_ip_info(ctdb
, TIMELIMIT(), pnn
, ctdb
,
2968 &ips
->ips
[ips
->num
-i
].addr
, &info
);
2975 for (j
=0; j
< info
->num
; j
++) {
2976 if (cifaces
== NULL
) {
2977 cifaces
= talloc_strdup(info
,
2978 info
->ifaces
[j
].name
);
2980 cifaces
= talloc_asprintf_append(cifaces
,
2982 info
->ifaces
[j
].name
);
2985 if (info
->active_idx
== j
) {
2986 aciface
= info
->ifaces
[j
].name
;
2989 if (info
->ifaces
[j
].link_state
== 0) {
2993 if (avifaces
== NULL
) {
2994 avifaces
= talloc_strdup(info
, info
->ifaces
[j
].name
);
2996 avifaces
= talloc_asprintf_append(avifaces
,
2998 info
->ifaces
[j
].name
);
3003 if (options
.machinereadable
){
3005 ctdb_addr_to_str(&ips
->ips
[ips
->num
-i
].addr
),
3006 ips
->ips
[ips
->num
-i
].pnn
);
3007 if (options
.verbose
){
3010 avifaces
?avifaces
:"",
3011 cifaces
?cifaces
:"");
3015 if (options
.verbose
) {
3016 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
3017 ctdb_addr_to_str(&ips
->ips
[ips
->num
-i
].addr
),
3018 ips
->ips
[ips
->num
-i
].pnn
,
3020 avifaces
?avifaces
:"",
3021 cifaces
?cifaces
:"");
3024 ctdb_addr_to_str(&ips
->ips
[ips
->num
-i
].addr
),
3025 ips
->ips
[ips
->num
-i
].pnn
);
3031 talloc_free(tmp_ctx
);
3038 static int control_ipinfo(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3041 ctdb_sock_addr addr
;
3042 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3043 struct ctdb_control_public_ip_info
*info
;
3046 talloc_free(tmp_ctx
);
3050 if (parse_ip(argv
[0], NULL
, 0, &addr
) == 0) {
3051 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
3055 /* read the public ip info from this node */
3056 ret
= ctdb_ctrl_get_public_ip_info(ctdb
, TIMELIMIT(), options
.pnn
,
3057 tmp_ctx
, &addr
, &info
);
3059 DEBUG(DEBUG_ERR
, ("Unable to get public ip[%s]info from node %u\n",
3060 argv
[0], options
.pnn
));
3061 talloc_free(tmp_ctx
);
3065 printf("Public IP[%s] info on node %u\n",
3066 ctdb_addr_to_str(&info
->ip
.addr
),
3069 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
3070 ctdb_addr_to_str(&info
->ip
.addr
),
3071 info
->ip
.pnn
, info
->num
);
3073 for (i
=0; i
<info
->num
; i
++) {
3074 info
->ifaces
[i
].name
[CTDB_IFACE_SIZE
] = '\0';
3076 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
3077 i
+1, info
->ifaces
[i
].name
,
3078 info
->ifaces
[i
].link_state
?"up":"down",
3079 (unsigned int)info
->ifaces
[i
].references
,
3080 (i
==info
->active_idx
)?" (active)":"");
3083 talloc_free(tmp_ctx
);
3088 display interfaces status
3090 static int control_ifaces(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3092 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3094 struct ctdb_control_get_ifaces
*ifaces
;
3097 /* read the public ip list from this node */
3098 ret
= ctdb_ctrl_get_ifaces(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &ifaces
);
3100 DEBUG(DEBUG_ERR
, ("Unable to get interfaces from node %u\n",
3102 talloc_free(tmp_ctx
);
3106 if (options
.machinereadable
){
3107 printf(":Name:LinkStatus:References:\n");
3109 printf("Interfaces on node %u\n", options
.pnn
);
3112 for (i
=0; i
<ifaces
->num
; i
++) {
3113 if (options
.machinereadable
){
3114 printf(":%s:%s:%u\n",
3115 ifaces
->ifaces
[i
].name
,
3116 ifaces
->ifaces
[i
].link_state
?"1":"0",
3117 (unsigned int)ifaces
->ifaces
[i
].references
);
3119 printf("name:%s link:%s references:%u\n",
3120 ifaces
->ifaces
[i
].name
,
3121 ifaces
->ifaces
[i
].link_state
?"up":"down",
3122 (unsigned int)ifaces
->ifaces
[i
].references
);
3126 talloc_free(tmp_ctx
);
3132 set link status of an interface
3134 static int control_setifacelink(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3137 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3138 struct ctdb_control_iface_info info
;
3146 if (strlen(argv
[0]) > CTDB_IFACE_SIZE
) {
3147 DEBUG(DEBUG_ERR
, ("interfaces name '%s' too long\n",
3149 talloc_free(tmp_ctx
);
3152 strcpy(info
.name
, argv
[0]);
3154 if (strcmp(argv
[1], "up") == 0) {
3155 info
.link_state
= 1;
3156 } else if (strcmp(argv
[1], "down") == 0) {
3157 info
.link_state
= 0;
3159 DEBUG(DEBUG_ERR
, ("link state invalid '%s' should be 'up' or 'down'\n",
3161 talloc_free(tmp_ctx
);
3165 /* read the public ip list from this node */
3166 ret
= ctdb_ctrl_set_iface_link(ctdb
, TIMELIMIT(), options
.pnn
,
3169 DEBUG(DEBUG_ERR
, ("Unable to set link state for interfaces %s node %u\n",
3170 argv
[0], options
.pnn
));
3171 talloc_free(tmp_ctx
);
3175 talloc_free(tmp_ctx
);
3180 display pid of a ctdb daemon
3182 static int control_getpid(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3187 ret
= ctdb_ctrl_getpid(ctdb
, TIMELIMIT(), options
.pnn
, &pid
);
3189 DEBUG(DEBUG_ERR
, ("Unable to get daemon pid from node %u\n", options
.pnn
));
3192 printf("Pid:%d\n", pid
);
3197 typedef bool update_flags_handler_t(struct ctdb_context
*ctdb
, void *data
);
3199 static int update_flags_and_ipreallocate(struct ctdb_context
*ctdb
,
3201 update_flags_handler_t handler
,
3206 struct ctdb_node_map
*nodemap
= NULL
;
3210 /* Check if the node is already in the desired state */
3211 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, ctdb
, &nodemap
);
3213 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from local node\n"));
3216 flag_is_set
= nodemap
->nodes
[options
.pnn
].flags
& flag
;
3217 if (set_flag
== flag_is_set
) {
3218 DEBUG(DEBUG_NOTICE
, ("Node %d is %s %s\n", options
.pnn
,
3219 (set_flag
? "already" : "not"), desc
));
3224 if (!handler(ctdb
, data
)) {
3225 DEBUG(DEBUG_WARNING
,
3226 ("Failed to send control to set state %s on node %u, try again\n",
3227 desc
, options
.pnn
));
3232 /* Read the nodemap and verify the change took effect.
3233 * Even if the above control/hanlder timed out then it
3234 * could still have worked!
3236 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
,
3239 DEBUG(DEBUG_WARNING
,
3240 ("Unable to get nodemap from local node, try again\n"));
3242 flag_is_set
= nodemap
->nodes
[options
.pnn
].flags
& flag
;
3243 } while (nodemap
== NULL
|| (set_flag
!= flag_is_set
));
3245 return ipreallocate(ctdb
);
3248 /* Administratively disable a node */
3249 static bool update_flags_disabled(struct ctdb_context
*ctdb
, void *data
)
3253 ret
= ctdb_ctrl_modflags(ctdb
, TIMELIMIT(), options
.pnn
,
3254 NODE_FLAGS_PERMANENTLY_DISABLED
, 0);
3258 static int control_disable(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3260 return update_flags_and_ipreallocate(ctdb
, NULL
,
3261 update_flags_disabled
,
3262 NODE_FLAGS_PERMANENTLY_DISABLED
,
3264 true /* set_flag*/);
3267 /* Administratively re-enable a node */
3268 static bool update_flags_not_disabled(struct ctdb_context
*ctdb
, void *data
)
3272 ret
= ctdb_ctrl_modflags(ctdb
, TIMELIMIT(), options
.pnn
,
3273 0, NODE_FLAGS_PERMANENTLY_DISABLED
);
3277 static int control_enable(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3279 return update_flags_and_ipreallocate(ctdb
, NULL
,
3280 update_flags_not_disabled
,
3281 NODE_FLAGS_PERMANENTLY_DISABLED
,
3283 false /* set_flag*/);
3287 static bool update_flags_stopped(struct ctdb_context
*ctdb
, void *data
)
3291 ret
= ctdb_ctrl_stop_node(ctdb
, TIMELIMIT(), options
.pnn
);
3296 static int control_stop(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3298 return update_flags_and_ipreallocate(ctdb
, NULL
,
3299 update_flags_stopped
,
3302 true /* set_flag*/);
3305 /* Continue a stopped node */
3306 static bool update_flags_not_stopped(struct ctdb_context
*ctdb
, void *data
)
3310 ret
= ctdb_ctrl_continue_node(ctdb
, TIMELIMIT(), options
.pnn
);
3315 static int control_continue(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3317 return update_flags_and_ipreallocate(ctdb
, NULL
,
3318 update_flags_not_stopped
,
3321 false /* set_flag */);
3324 static uint32_t get_generation(struct ctdb_context
*ctdb
)
3326 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3327 struct ctdb_vnn_map
*vnnmap
=NULL
;
3329 uint32_t generation
;
3331 /* wait until the recmaster is not in recovery mode */
3333 uint32_t recmode
, recmaster
;
3335 if (vnnmap
!= NULL
) {
3336 talloc_free(vnnmap
);
3340 /* get the recmaster */
3341 ret
= ctdb_ctrl_getrecmaster(ctdb
, tmp_ctx
, TIMELIMIT(), CTDB_CURRENT_NODE
, &recmaster
);
3343 DEBUG(DEBUG_ERR
, ("Unable to get recmaster from node %u\n", options
.pnn
));
3344 talloc_free(tmp_ctx
);
3348 /* get recovery mode */
3349 ret
= ctdb_ctrl_getrecmode(ctdb
, tmp_ctx
, TIMELIMIT(), recmaster
, &recmode
);
3351 DEBUG(DEBUG_ERR
, ("Unable to get recmode from node %u\n", options
.pnn
));
3352 talloc_free(tmp_ctx
);
3356 /* get the current generation number */
3357 ret
= ctdb_ctrl_getvnnmap(ctdb
, TIMELIMIT(), recmaster
, tmp_ctx
, &vnnmap
);
3359 DEBUG(DEBUG_ERR
, ("Unable to get vnnmap from recmaster (%u)\n", recmaster
));
3360 talloc_free(tmp_ctx
);
3364 if ((recmode
== CTDB_RECOVERY_NORMAL
) && (vnnmap
->generation
!= 1)) {
3365 generation
= vnnmap
->generation
;
3366 talloc_free(tmp_ctx
);
3374 static bool update_state_banned(struct ctdb_context
*ctdb
, void *data
)
3376 struct ctdb_ban_time
*bantime
= (struct ctdb_ban_time
*)data
;
3379 ret
= ctdb_ctrl_set_ban(ctdb
, TIMELIMIT(), options
.pnn
, bantime
);
3384 static int control_ban(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3386 struct ctdb_ban_time bantime
;
3392 bantime
.pnn
= options
.pnn
;
3393 bantime
.time
= strtoul(argv
[0], NULL
, 0);
3395 if (bantime
.time
== 0) {
3396 DEBUG(DEBUG_ERR
, ("Invalid ban time specified - must be >0\n"));
3400 return update_flags_and_ipreallocate(ctdb
, &bantime
,
3401 update_state_banned
,
3404 true /* set_flag*/);
3409 static int control_unban(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3411 struct ctdb_ban_time bantime
;
3413 bantime
.pnn
= options
.pnn
;
3416 return update_flags_and_ipreallocate(ctdb
, &bantime
,
3417 update_state_banned
,
3420 false /* set_flag*/);
3424 show ban information for a node
3426 static int control_showban(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3429 struct ctdb_node_map
*nodemap
=NULL
;
3430 struct ctdb_ban_time
*bantime
;
3432 /* verify the node exists */
3433 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, ctdb
, &nodemap
);
3435 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from local node\n"));
3439 ret
= ctdb_ctrl_get_ban(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &bantime
);
3441 DEBUG(DEBUG_ERR
,("Showing ban info for node %d failed.\n", options
.pnn
));
3445 if (bantime
->time
== 0) {
3446 printf("Node %u is not banned\n", bantime
->pnn
);
3448 printf("Node %u is banned, %d seconds remaining\n",
3449 bantime
->pnn
, bantime
->time
);
3458 static int control_shutdown(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3462 ret
= ctdb_ctrl_shutdown(ctdb
, TIMELIMIT(), options
.pnn
);
3464 DEBUG(DEBUG_ERR
, ("Unable to shutdown node %u\n", options
.pnn
));
3474 static int control_recover(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3477 uint32_t generation
, next_generation
;
3479 /* record the current generation number */
3480 generation
= get_generation(ctdb
);
3482 ret
= ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
3484 DEBUG(DEBUG_ERR
, ("Unable to set recovery mode\n"));
3488 /* wait until we are in a new generation */
3490 next_generation
= get_generation(ctdb
);
3491 if (next_generation
!= generation
) {
3502 display monitoring mode of a remote node
3504 static int control_getmonmode(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3509 ret
= ctdb_ctrl_getmonmode(ctdb
, TIMELIMIT(), options
.pnn
, &monmode
);
3511 DEBUG(DEBUG_ERR
, ("Unable to get monmode from node %u\n", options
.pnn
));
3514 if (!options
.machinereadable
){
3515 printf("Monitoring mode:%s (%d)\n",monmode
==CTDB_MONITORING_ACTIVE
?"ACTIVE":"DISABLED",monmode
);
3518 printf(":%d:\n",monmode
);
3525 display capabilities of a remote node
3527 static int control_getcapabilities(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3529 uint32_t capabilities
;
3532 ret
= ctdb_ctrl_getcapabilities(ctdb
, TIMELIMIT(), options
.pnn
, &capabilities
);
3534 DEBUG(DEBUG_ERR
, ("Unable to get capabilities from node %u\n", options
.pnn
));
3538 if (!options
.machinereadable
){
3539 printf("RECMASTER: %s\n", (capabilities
&CTDB_CAP_RECMASTER
)?"YES":"NO");
3540 printf("LMASTER: %s\n", (capabilities
&CTDB_CAP_LMASTER
)?"YES":"NO");
3541 printf("LVS: %s\n", (capabilities
&CTDB_CAP_LVS
)?"YES":"NO");
3542 printf("NATGW: %s\n", (capabilities
&CTDB_CAP_NATGW
)?"YES":"NO");
3544 printf(":RECMASTER:LMASTER:LVS:NATGW:\n");
3545 printf(":%d:%d:%d:%d:\n",
3546 !!(capabilities
&CTDB_CAP_RECMASTER
),
3547 !!(capabilities
&CTDB_CAP_LMASTER
),
3548 !!(capabilities
&CTDB_CAP_LVS
),
3549 !!(capabilities
&CTDB_CAP_NATGW
));
3555 display lvs configuration
3558 static uint32_t lvs_exclude_flags
[] = {
3559 /* Look for a nice healthy node */
3560 NODE_FLAGS_INACTIVE
|NODE_FLAGS_DISABLED
,
3561 /* If not found, an UNHEALTHY node will do */
3562 NODE_FLAGS_INACTIVE
|NODE_FLAGS_PERMANENTLY_DISABLED
,
3566 static int control_lvs(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3568 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3569 struct ctdb_node_map
*orig_nodemap
=NULL
;
3570 struct ctdb_node_map
*nodemap
;
3573 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
,
3574 tmp_ctx
, &orig_nodemap
);
3576 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
3577 talloc_free(tmp_ctx
);
3581 nodemap
= filter_nodemap_by_capabilities(ctdb
, orig_nodemap
,
3582 CTDB_CAP_LVS
, false);
3583 if (nodemap
== NULL
) {
3591 for (i
= 0; lvs_exclude_flags
[i
] != 0; i
++) {
3592 struct ctdb_node_map
*t
=
3593 filter_nodemap_by_flags(ctdb
, nodemap
,
3594 lvs_exclude_flags
[i
]);
3601 /* At least 1 node without excluded flags */
3603 for (j
= 0; j
< t
->num
; j
++) {
3604 printf("%d:%s\n", t
->nodes
[j
].pnn
,
3605 ctdb_addr_to_str(&t
->nodes
[j
].addr
));
3612 talloc_free(tmp_ctx
);
3617 display who is the lvs master
3619 static int control_lvsmaster(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3621 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3622 struct ctdb_node_map
*nodemap
=NULL
;
3625 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
,
3628 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
3629 talloc_free(tmp_ctx
);
3633 for (i
= 0; lvs_exclude_flags
[i
] != 0; i
++) {
3634 struct ctdb_node_map
*t
=
3635 filter_nodemap_by_flags(ctdb
, nodemap
,
3636 lvs_exclude_flags
[i
]);
3643 struct ctdb_node_map
*n
;
3644 n
= filter_nodemap_by_capabilities(ctdb
,
3655 printf(options
.machinereadable
?
3656 "%d\n" : "Node %d is LVS master\n",
3664 printf("There is no LVS master\n");
3667 talloc_free(tmp_ctx
);
3672 disable monitoring on a node
3674 static int control_disable_monmode(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3679 ret
= ctdb_ctrl_disable_monmode(ctdb
, TIMELIMIT(), options
.pnn
);
3681 DEBUG(DEBUG_ERR
, ("Unable to disable monmode on node %u\n", options
.pnn
));
3684 printf("Monitoring mode:%s\n","DISABLED");
3690 enable monitoring on a node
3692 static int control_enable_monmode(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3697 ret
= ctdb_ctrl_enable_monmode(ctdb
, TIMELIMIT(), options
.pnn
);
3699 DEBUG(DEBUG_ERR
, ("Unable to enable monmode on node %u\n", options
.pnn
));
3702 printf("Monitoring mode:%s\n","ACTIVE");
3708 display remote list of keys/data for a db
3710 static int control_catdb(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3712 const char *db_name
;
3713 struct ctdb_db_context
*ctdb_db
;
3715 struct ctdb_dump_db_context c
;
3722 if (!db_exists(ctdb
, argv
[0], NULL
, &db_name
, &flags
)) {
3726 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, flags
& CTDB_DB_FLAGS_PERSISTENT
, 0);
3727 if (ctdb_db
== NULL
) {
3728 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
3732 if (options
.printlmaster
) {
3733 ret
= ctdb_ctrl_getvnnmap(ctdb
, TIMELIMIT(), options
.pnn
,
3734 ctdb
, &ctdb
->vnn_map
);
3736 DEBUG(DEBUG_ERR
, ("Unable to get vnnmap from node %u\n",
3744 c
.printemptyrecords
= (bool)options
.printemptyrecords
;
3745 c
.printdatasize
= (bool)options
.printdatasize
;
3746 c
.printlmaster
= (bool)options
.printlmaster
;
3747 c
.printhash
= (bool)options
.printhash
;
3748 c
.printrecordflags
= (bool)options
.printrecordflags
;
3750 /* traverse and dump the cluster tdb */
3751 ret
= ctdb_dump_db(ctdb_db
, &c
);
3753 DEBUG(DEBUG_ERR
, ("Unable to dump database\n"));
3754 DEBUG(DEBUG_ERR
, ("Maybe try 'ctdb getdbstatus %s'"
3755 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3759 talloc_free(ctdb_db
);
3761 printf("Dumped %d records\n", ret
);
3765 struct cattdb_data
{
3766 struct ctdb_context
*ctdb
;
3770 static int cattdb_traverse(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA data
, void *private_data
)
3772 struct cattdb_data
*d
= private_data
;
3773 struct ctdb_dump_db_context c
;
3779 c
.printemptyrecords
= (bool)options
.printemptyrecords
;
3780 c
.printdatasize
= (bool)options
.printdatasize
;
3781 c
.printlmaster
= false;
3782 c
.printhash
= (bool)options
.printhash
;
3783 c
.printrecordflags
= true;
3785 return ctdb_dumpdb_record(d
->ctdb
, key
, data
, &c
);
3789 cat the local tdb database using same format as catdb
3791 static int control_cattdb(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3793 const char *db_name
;
3794 struct ctdb_db_context
*ctdb_db
;
3795 struct cattdb_data d
;
3802 if (!db_exists(ctdb
, argv
[0], NULL
, &db_name
, &flags
)) {
3806 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, flags
& CTDB_DB_FLAGS_PERSISTENT
, 0);
3807 if (ctdb_db
== NULL
) {
3808 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
3812 /* traverse the local tdb */
3815 if (tdb_traverse_read(ctdb_db
->ltdb
->tdb
, cattdb_traverse
, &d
) == -1) {
3816 printf("Failed to cattdb data\n");
3819 talloc_free(ctdb_db
);
3821 printf("Dumped %d records\n", d
.count
);
3826 display the content of a database key
3828 static int control_readkey(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3830 const char *db_name
;
3831 struct ctdb_db_context
*ctdb_db
;
3832 struct ctdb_record_handle
*h
;
3833 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3841 if (!db_exists(ctdb
, argv
[0], NULL
, &db_name
, &flags
)) {
3845 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, flags
& CTDB_DB_FLAGS_PERSISTENT
, 0);
3846 if (ctdb_db
== NULL
) {
3847 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
3851 key
.dptr
= discard_const(argv
[1]);
3852 key
.dsize
= strlen((char *)key
.dptr
);
3854 h
= ctdb_fetch_lock(ctdb_db
, tmp_ctx
, key
, &data
);
3856 printf("Failed to fetch record '%s' on node %d\n",
3857 (const char *)key
.dptr
, ctdb_get_pnn(ctdb
));
3858 talloc_free(tmp_ctx
);
3862 printf("Data: size:%d ptr:[%.*s]\n", (int)data
.dsize
, (int)data
.dsize
, data
.dptr
);
3864 talloc_free(tmp_ctx
);
3865 talloc_free(ctdb_db
);
3870 display the content of a database key
3872 static int control_writekey(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3874 const char *db_name
;
3875 struct ctdb_db_context
*ctdb_db
;
3876 struct ctdb_record_handle
*h
;
3877 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3885 if (!db_exists(ctdb
, argv
[0], NULL
, &db_name
, &flags
)) {
3889 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, flags
& CTDB_DB_FLAGS_PERSISTENT
, 0);
3890 if (ctdb_db
== NULL
) {
3891 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
3895 key
.dptr
= discard_const(argv
[1]);
3896 key
.dsize
= strlen((char *)key
.dptr
);
3898 h
= ctdb_fetch_lock(ctdb_db
, tmp_ctx
, key
, &data
);
3900 printf("Failed to fetch record '%s' on node %d\n",
3901 (const char *)key
.dptr
, ctdb_get_pnn(ctdb
));
3902 talloc_free(tmp_ctx
);
3906 data
.dptr
= discard_const(argv
[2]);
3907 data
.dsize
= strlen((char *)data
.dptr
);
3909 if (ctdb_record_store(h
, data
) != 0) {
3910 printf("Failed to store record\n");
3914 talloc_free(tmp_ctx
);
3915 talloc_free(ctdb_db
);
3920 fetch a record from a persistent database
3922 static int control_pfetch(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3924 const char *db_name
;
3925 struct ctdb_db_context
*ctdb_db
;
3926 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3927 struct ctdb_transaction_handle
*h
;
3934 talloc_free(tmp_ctx
);
3938 if (!db_exists(ctdb
, argv
[0], NULL
, &db_name
, &flags
)) {
3939 talloc_free(tmp_ctx
);
3943 persistent
= flags
& CTDB_DB_FLAGS_PERSISTENT
;
3945 DEBUG(DEBUG_ERR
,("Database '%s' is not persistent\n", db_name
));
3946 talloc_free(tmp_ctx
);
3950 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, persistent
, 0);
3951 if (ctdb_db
== NULL
) {
3952 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
3953 talloc_free(tmp_ctx
);
3957 h
= ctdb_transaction_start(ctdb_db
, tmp_ctx
);
3959 DEBUG(DEBUG_ERR
,("Failed to start transaction on database %s\n", db_name
));
3960 talloc_free(tmp_ctx
);
3964 key
.dptr
= discard_const(argv
[1]);
3965 key
.dsize
= strlen(argv
[1]);
3966 ret
= ctdb_transaction_fetch(h
, tmp_ctx
, key
, &data
);
3968 DEBUG(DEBUG_ERR
,("Failed to fetch record\n"));
3969 talloc_free(tmp_ctx
);
3973 if (data
.dsize
== 0 || data
.dptr
== NULL
) {
3974 DEBUG(DEBUG_ERR
,("Record is empty\n"));
3975 talloc_free(tmp_ctx
);
3980 fd
= open(argv
[2], O_WRONLY
|O_CREAT
|O_TRUNC
, 0600);
3982 DEBUG(DEBUG_ERR
,("Failed to open output file %s\n", argv
[2]));
3983 talloc_free(tmp_ctx
);
3986 sys_write(fd
, data
.dptr
, data
.dsize
);
3989 sys_write(1, data
.dptr
, data
.dsize
);
3992 /* abort the transaction */
3996 talloc_free(tmp_ctx
);
4001 fetch a record from a tdb-file
4003 static int control_tfetch(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4005 const char *tdb_file
;
4008 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
4017 tdb
= tdb_open(tdb_file
, 0, 0, O_RDONLY
, 0);
4019 printf("Failed to open TDB file %s\n", tdb_file
);
4023 if (!strncmp(argv
[1], "0x", 2)) {
4024 key
= hextodata(tmp_ctx
, argv
[1] + 2);
4025 if (key
.dsize
== 0) {
4026 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv
[1]);
4030 key
.dptr
= discard_const(argv
[1]);
4031 key
.dsize
= strlen(argv
[1]);
4034 data
= tdb_fetch(tdb
, key
);
4035 if (data
.dptr
== NULL
|| data
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
4036 printf("Failed to read record %s from tdb %s\n", argv
[1], tdb_file
);
4044 fd
= open(argv
[2], O_WRONLY
|O_CREAT
|O_TRUNC
, 0600);
4046 printf("Failed to open output file %s\n", argv
[2]);
4049 if (options
.verbose
){
4050 sys_write(fd
, data
.dptr
, data
.dsize
);
4052 sys_write(fd
, data
.dptr
+sizeof(struct ctdb_ltdb_header
), data
.dsize
-sizeof(struct ctdb_ltdb_header
));
4056 if (options
.verbose
){
4057 sys_write(1, data
.dptr
, data
.dsize
);
4059 sys_write(1, data
.dptr
+sizeof(struct ctdb_ltdb_header
), data
.dsize
-sizeof(struct ctdb_ltdb_header
));
4063 talloc_free(tmp_ctx
);
4068 store a record and header to a tdb-file
4070 static int control_tstore(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4072 const char *tdb_file
;
4074 TDB_DATA key
, value
, data
;
4075 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
4076 struct ctdb_ltdb_header header
;
4084 tdb
= tdb_open(tdb_file
, 0, 0, O_RDWR
, 0);
4086 printf("Failed to open TDB file %s\n", tdb_file
);
4090 if (!strncmp(argv
[1], "0x", 2)) {
4091 key
= hextodata(tmp_ctx
, argv
[1] + 2);
4092 if (key
.dsize
== 0) {
4093 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv
[1]);
4097 key
.dptr
= discard_const(argv
[1]);
4098 key
.dsize
= strlen(argv
[1]);
4101 if (!strncmp(argv
[2], "0x", 2)) {
4102 value
= hextodata(tmp_ctx
, argv
[2] + 2);
4103 if (value
.dsize
== 0) {
4104 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv
[2]);
4108 value
.dptr
= discard_const(argv
[2]);
4109 value
.dsize
= strlen(argv
[2]);
4112 ZERO_STRUCT(header
);
4114 header
.rsn
= atoll(argv
[3]);
4117 header
.dmaster
= atoi(argv
[4]);
4120 header
.flags
= atoi(argv
[5]);
4123 data
.dsize
= sizeof(struct ctdb_ltdb_header
) + value
.dsize
;
4124 data
.dptr
= talloc_size(tmp_ctx
, data
.dsize
);
4125 if (data
.dptr
== NULL
) {
4126 printf("Failed to allocate header+value\n");
4130 *(struct ctdb_ltdb_header
*)data
.dptr
= header
;
4131 memcpy(data
.dptr
+ sizeof(struct ctdb_ltdb_header
), value
.dptr
, value
.dsize
);
4133 if (tdb_store(tdb
, key
, data
, TDB_REPLACE
) != 0) {
4134 printf("Failed to write record %s to tdb %s\n", argv
[1], tdb_file
);
4141 talloc_free(tmp_ctx
);
4146 write a record to a persistent database
4148 static int control_pstore(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4150 const char *db_name
;
4151 struct ctdb_db_context
*ctdb_db
;
4152 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
4153 struct ctdb_transaction_handle
*h
;
4159 talloc_free(tmp_ctx
);
4163 fd
= open(argv
[2], O_RDONLY
);
4165 DEBUG(DEBUG_ERR
,("Failed to open file containing record data : %s %s\n", argv
[2], strerror(errno
)));
4166 talloc_free(tmp_ctx
);
4170 ret
= fstat(fd
, &st
);
4172 DEBUG(DEBUG_ERR
,("fstat of file %s failed: %s\n", argv
[2], strerror(errno
)));
4174 talloc_free(tmp_ctx
);
4178 if (!S_ISREG(st
.st_mode
)) {
4179 DEBUG(DEBUG_ERR
,("Not a regular file %s\n", argv
[2]));
4181 talloc_free(tmp_ctx
);
4185 data
.dsize
= st
.st_size
;
4186 if (data
.dsize
== 0) {
4189 data
.dptr
= talloc_size(tmp_ctx
, data
.dsize
);
4190 if (data
.dptr
== NULL
) {
4191 DEBUG(DEBUG_ERR
,("Failed to talloc %d of memory to store record data\n", (int)data
.dsize
));
4193 talloc_free(tmp_ctx
);
4196 ret
= sys_read(fd
, data
.dptr
, data
.dsize
);
4197 if (ret
!= data
.dsize
) {
4198 DEBUG(DEBUG_ERR
,("Failed to read %d bytes of record data\n", (int)data
.dsize
));
4200 talloc_free(tmp_ctx
);
4209 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, true, 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
, data
);
4227 DEBUG(DEBUG_ERR
,("Failed to store 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
);
4240 talloc_free(tmp_ctx
);
4245 * delete a record from a persistent database
4247 static int control_pdelete(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4249 const char *db_name
;
4250 struct ctdb_db_context
*ctdb_db
;
4251 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
4252 struct ctdb_transaction_handle
*h
;
4259 talloc_free(tmp_ctx
);
4263 if (!db_exists(ctdb
, argv
[0], NULL
, &db_name
, &flags
)) {
4264 talloc_free(tmp_ctx
);
4268 persistent
= flags
& CTDB_DB_FLAGS_PERSISTENT
;
4270 DEBUG(DEBUG_ERR
, ("Database '%s' is not persistent\n", db_name
));
4271 talloc_free(tmp_ctx
);
4275 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, persistent
, 0);
4276 if (ctdb_db
== NULL
) {
4277 DEBUG(DEBUG_ERR
, ("Unable to attach to database '%s'\n", db_name
));
4278 talloc_free(tmp_ctx
);
4282 h
= ctdb_transaction_start(ctdb_db
, tmp_ctx
);
4284 DEBUG(DEBUG_ERR
, ("Failed to start transaction on database %s\n", db_name
));
4285 talloc_free(tmp_ctx
);
4289 key
.dptr
= discard_const(argv
[1]);
4290 key
.dsize
= strlen(argv
[1]);
4291 ret
= ctdb_transaction_store(h
, key
, tdb_null
);
4293 DEBUG(DEBUG_ERR
, ("Failed to delete record\n"));
4294 talloc_free(tmp_ctx
);
4298 ret
= ctdb_transaction_commit(h
);
4300 DEBUG(DEBUG_ERR
, ("Failed to commit transaction\n"));
4301 talloc_free(tmp_ctx
);
4305 talloc_free(tmp_ctx
);
4309 static const char *ptrans_parse_string(TALLOC_CTX
*mem_ctx
, const char *s
,
4314 const char *ret
; /* Next byte after successfully parsed value */
4316 /* Error, unless someone says otherwise */
4318 /* Indicates no value to parse */
4321 /* Skip whitespace */
4322 n
= strspn(s
, " \t");
4326 /* Quoted ASCII string - no wide characters! */
4328 n
= strcspn(t
, "\"");
4332 data
->dptr
= talloc_memdup(mem_ctx
, t
, n
);
4333 CTDB_NOMEM_ABORT(data
->dptr
);
4337 DEBUG(DEBUG_WARNING
,("Unmatched \" in input %s\n", s
));
4340 DEBUG(DEBUG_WARNING
,("Unsupported input format in %s\n", s
));
4346 static bool ptrans_get_key_value(TALLOC_CTX
*mem_ctx
, FILE *file
,
4347 TDB_DATA
*key
, TDB_DATA
*value
)
4349 char line
[1024]; /* FIXME: make this more flexible? */
4353 ptr
= fgets(line
, sizeof(line
), file
);
4360 t
= ptrans_parse_string(mem_ctx
, line
, key
);
4361 if (t
== NULL
|| key
->dptr
== NULL
) {
4362 /* Line Ignored but not EOF */
4367 t
= ptrans_parse_string(mem_ctx
, t
, value
);
4369 /* Line Ignored but not EOF */
4370 talloc_free(key
->dptr
);
4379 * Update a persistent database as per file/stdin
4381 static int control_ptrans(struct ctdb_context
*ctdb
,
4382 int argc
, const char **argv
)
4384 const char *db_name
;
4385 struct ctdb_db_context
*ctdb_db
;
4386 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
4387 struct ctdb_transaction_handle
*h
;
4388 TDB_DATA key
, value
;
4393 talloc_free(tmp_ctx
);
4399 file
= fopen(argv
[1], "r");
4401 DEBUG(DEBUG_ERR
,("Unable to open file for reading '%s'\n", argv
[1]));
4402 talloc_free(tmp_ctx
);
4409 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, true, 0);
4410 if (ctdb_db
== NULL
) {
4411 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
4415 h
= ctdb_transaction_start(ctdb_db
, tmp_ctx
);
4417 DEBUG(DEBUG_ERR
,("Failed to start transaction on database %s\n", db_name
));
4421 while (ptrans_get_key_value(tmp_ctx
, file
, &key
, &value
)) {
4422 if (key
.dsize
!= 0) {
4423 ret
= ctdb_transaction_store(h
, key
, value
);
4424 /* Minimise memory use */
4425 talloc_free(key
.dptr
);
4426 if (value
.dptr
!= NULL
) {
4427 talloc_free(value
.dptr
);
4430 DEBUG(DEBUG_ERR
,("Failed to store record\n"));
4431 ctdb_transaction_cancel(h
);
4437 ret
= ctdb_transaction_commit(h
);
4439 DEBUG(DEBUG_ERR
,("Failed to commit transaction\n"));
4443 if (file
!= stdin
) {
4446 talloc_free(tmp_ctx
);
4450 if (file
!= stdin
) {
4454 talloc_free(tmp_ctx
);
4459 check if a service is bound to a port or not
4461 static int control_chktcpport(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4466 struct sockaddr_in sin
;
4469 printf("Use: ctdb chktcport <port>\n");
4473 port
= atoi(argv
[0]);
4475 s
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
4477 printf("Failed to open local socket\n");
4481 v
= fcntl(s
, F_GETFL
, 0);
4482 if (v
== -1 || fcntl(s
, F_SETFL
, v
| O_NONBLOCK
) != 0) {
4483 printf("Unable to set socket non-blocking: %s\n", strerror(errno
));
4486 bzero(&sin
, sizeof(sin
));
4487 sin
.sin_family
= PF_INET
;
4488 sin
.sin_port
= htons(port
);
4489 ret
= bind(s
, (struct sockaddr
*)&sin
, sizeof(sin
));
4492 printf("Failed to bind to local socket: %d %s\n", errno
, strerror(errno
));
4501 static void log_handler(struct ctdb_context
*ctdb
, uint64_t srvid
,
4502 TDB_DATA data
, void *private_data
)
4504 DEBUG(DEBUG_ERR
,("Log data received\n"));
4505 if (data
.dsize
> 0) {
4506 printf("%s", data
.dptr
);
4513 display a list of log messages from the in memory ringbuffer
4515 static int control_getlog(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4519 struct ctdb_get_log_addr log_addr
;
4523 /* Process options */
4525 log_addr
.pnn
= ctdb_get_pnn(ctdb
);
4526 log_addr
.level
= DEBUG_NOTICE
;
4527 for (i
= 0; i
< argc
; i
++) {
4528 if (strcmp(argv
[i
], "recoverd") == 0) {
4529 main_daemon
= false;
4531 if (isalpha(argv
[i
][0]) || argv
[i
][0] == '-') {
4532 log_addr
.level
= get_debug_by_desc(argv
[i
]);
4534 log_addr
.level
= strtol(argv
[i
], NULL
, 0);
4539 /* Our message port is our PID */
4540 log_addr
.srvid
= getpid();
4542 data
.dptr
= (unsigned char *)&log_addr
;
4543 data
.dsize
= sizeof(log_addr
);
4545 DEBUG(DEBUG_ERR
, ("Pulling logs from node %u\n", options
.pnn
));
4547 ctdb_client_set_message_handler(ctdb
, log_addr
.srvid
, log_handler
, NULL
);
4550 DEBUG(DEBUG_ERR
,("Listen for response on %d\n", (int)log_addr
.srvid
));
4555 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
4557 ret
= ctdb_control(ctdb
, options
.pnn
, 0, CTDB_CONTROL_GET_LOG
,
4558 0, data
, tmp_ctx
, NULL
, &res
, NULL
, &errmsg
);
4559 if (ret
!= 0 || res
!= 0) {
4560 DEBUG(DEBUG_ERR
,("Failed to get logs - %s\n", errmsg
));
4561 talloc_free(tmp_ctx
);
4564 talloc_free(tmp_ctx
);
4566 ret
= ctdb_client_send_message(ctdb
, options
.pnn
,
4567 CTDB_SRVID_GETLOG
, data
);
4569 DEBUG(DEBUG_ERR
,("Failed to send getlog request message to %u\n", options
.pnn
));
4574 tv
= timeval_current();
4575 /* this loop will terminate when we have received the reply */
4576 while (timeval_elapsed(&tv
) < (double)options
.timelimit
) {
4577 event_loop_once(ctdb
->ev
);
4580 DEBUG(DEBUG_INFO
,("Timed out waiting for log data.\n"));
4586 clear the in memory log area
4588 static int control_clearlog(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4592 if (argc
== 0 || (argc
>= 1 && strcmp(argv
[0], "recoverd") != 0)) {
4593 /* "recoverd" not given - get logs from main daemon */
4596 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
4598 ret
= ctdb_control(ctdb
, options
.pnn
, 0, CTDB_CONTROL_CLEAR_LOG
,
4599 0, tdb_null
, tmp_ctx
, NULL
, &res
, NULL
, &errmsg
);
4600 if (ret
!= 0 || res
!= 0) {
4601 DEBUG(DEBUG_ERR
,("Failed to clear logs\n"));
4602 talloc_free(tmp_ctx
);
4606 talloc_free(tmp_ctx
);
4608 TDB_DATA data
; /* unused in recoverd... */
4611 ret
= ctdb_client_send_message(ctdb
, options
.pnn
, CTDB_SRVID_CLEARLOG
, data
);
4613 DEBUG(DEBUG_ERR
,("Failed to send clearlog request message to %u\n", options
.pnn
));
4621 /* Reload public IPs on a specified nodes */
4622 static int control_reloadips(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4624 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
4630 assert_single_node_only();
4636 /* Determine the nodes where IPs need to be reloaded */
4637 if (!parse_nodestring(ctdb
, tmp_ctx
, argc
== 1 ? argv
[0] : NULL
,
4638 options
.pnn
, true, &nodes
, &pnn_mode
)) {
4644 /* Disable takeover runs on all connected nodes. A reply
4645 * indicating success is needed from each node so all nodes
4646 * will need to be active. This will retry until maxruntime
4647 * is exceeded, hence no error handling.
4649 * A check could be added to not allow reloading of IPs when
4650 * there are disconnected nodes. However, this should
4651 * probably be left up to the administrator.
4653 timeout
= LONGTIMEOUT
;
4654 srvid_broadcast(ctdb
, CTDB_SRVID_DISABLE_TAKEOVER_RUNS
, &timeout
,
4655 "Disable takeover runs", true);
4657 /* Now tell all the desired nodes to reload their public IPs.
4658 * Keep trying this until it succeeds. This assumes all
4659 * failures are transient, which might not be true...
4661 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_RELOAD_PUBLIC_IPS
,
4662 nodes
, 0, LONGTIMELIMIT(),
4664 NULL
, NULL
, NULL
) != 0) {
4666 ("Unable to reload IPs on some nodes, try again.\n"));
4670 /* It isn't strictly necessary to wait until takeover runs are
4671 * re-enabled but doing so can't hurt.
4674 srvid_broadcast(ctdb
, CTDB_SRVID_DISABLE_TAKEOVER_RUNS
, &timeout
,
4675 "Enable takeover runs", true);
4681 talloc_free(tmp_ctx
);
4686 display a list of the databases on a remote ctdb
4688 static int control_getdbmap(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4691 struct ctdb_dbid_map
*dbmap
=NULL
;
4693 ret
= ctdb_ctrl_getdbmap(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &dbmap
);
4695 DEBUG(DEBUG_ERR
, ("Unable to get dbids from node %u\n", options
.pnn
));
4699 if(options
.machinereadable
){
4700 printf(":ID:Name:Path:Persistent:Sticky:Unhealthy:ReadOnly:\n");
4701 for(i
=0;i
<dbmap
->num
;i
++){
4709 ctdb_ctrl_getdbpath(ctdb
, TIMELIMIT(), options
.pnn
,
4710 dbmap
->dbs
[i
].dbid
, ctdb
, &path
);
4711 ctdb_ctrl_getdbname(ctdb
, TIMELIMIT(), options
.pnn
,
4712 dbmap
->dbs
[i
].dbid
, ctdb
, &name
);
4713 ctdb_ctrl_getdbhealth(ctdb
, TIMELIMIT(), options
.pnn
,
4714 dbmap
->dbs
[i
].dbid
, ctdb
, &health
);
4715 persistent
= dbmap
->dbs
[i
].flags
& CTDB_DB_FLAGS_PERSISTENT
;
4716 readonly
= dbmap
->dbs
[i
].flags
& CTDB_DB_FLAGS_READONLY
;
4717 sticky
= dbmap
->dbs
[i
].flags
& CTDB_DB_FLAGS_STICKY
;
4718 printf(":0x%08X:%s:%s:%d:%d:%d:%d:\n",
4719 dbmap
->dbs
[i
].dbid
, name
, path
,
4720 !!(persistent
), !!(sticky
),
4721 !!(health
), !!(readonly
));
4726 printf("Number of databases:%d\n", dbmap
->num
);
4727 for(i
=0;i
<dbmap
->num
;i
++){
4735 ctdb_ctrl_getdbpath(ctdb
, TIMELIMIT(), options
.pnn
, dbmap
->dbs
[i
].dbid
, ctdb
, &path
);
4736 ctdb_ctrl_getdbname(ctdb
, TIMELIMIT(), options
.pnn
, dbmap
->dbs
[i
].dbid
, ctdb
, &name
);
4737 ctdb_ctrl_getdbhealth(ctdb
, TIMELIMIT(), options
.pnn
, dbmap
->dbs
[i
].dbid
, ctdb
, &health
);
4738 persistent
= dbmap
->dbs
[i
].flags
& CTDB_DB_FLAGS_PERSISTENT
;
4739 readonly
= dbmap
->dbs
[i
].flags
& CTDB_DB_FLAGS_READONLY
;
4740 sticky
= dbmap
->dbs
[i
].flags
& CTDB_DB_FLAGS_STICKY
;
4741 printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
4742 dbmap
->dbs
[i
].dbid
, name
, path
,
4743 persistent
?" PERSISTENT":"",
4744 sticky
?" STICKY":"",
4745 readonly
?" READONLY":"",
4746 health
?" UNHEALTHY":"");
4753 display the status of a database on a remote ctdb
4755 static int control_getdbstatus(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4757 const char *db_name
;
4767 if (!db_exists(ctdb
, argv
[0], &db_id
, &db_name
, &flags
)) {
4771 ctdb_ctrl_getdbpath(ctdb
, TIMELIMIT(), options
.pnn
, db_id
, ctdb
, &path
);
4772 ctdb_ctrl_getdbhealth(ctdb
, TIMELIMIT(), options
.pnn
, db_id
, ctdb
, &health
);
4773 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
4774 db_id
, db_name
, path
,
4775 (flags
& CTDB_DB_FLAGS_PERSISTENT
? "yes" : "no"),
4776 (flags
& CTDB_DB_FLAGS_STICKY
? "yes" : "no"),
4777 (flags
& CTDB_DB_FLAGS_READONLY
? "yes" : "no"),
4778 (health
? health
: "OK"));
4784 check if the local node is recmaster or not
4785 it will return 1 if this node is the recmaster and 0 if it is not
4786 or if the local ctdb daemon could not be contacted
4788 static int control_isnotrecmaster(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4790 uint32_t mypnn
, recmaster
;
4793 assert_single_node_only();
4795 mypnn
= getpnn(ctdb
);
4797 ret
= ctdb_ctrl_getrecmaster(ctdb
, ctdb
, TIMELIMIT(), options
.pnn
, &recmaster
);
4799 printf("Failed to get the recmaster\n");
4803 if (recmaster
!= mypnn
) {
4804 printf("this node is not the recmaster\n");
4808 printf("this node is the recmaster\n");
4815 static int control_ping(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4818 struct timeval tv
= timeval_current();
4819 ret
= ctdb_ctrl_ping(ctdb
, options
.pnn
);
4821 printf("Unable to get ping response from node %u\n", options
.pnn
);
4824 printf("response from %u time=%.6f sec (%d clients)\n",
4825 options
.pnn
, timeval_elapsed(&tv
), ret
);
4832 get a node's runstate
4834 static int control_runstate(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4837 enum ctdb_runstate runstate
;
4839 ret
= ctdb_ctrl_get_runstate(ctdb
, TIMELIMIT(), options
.pnn
, &runstate
);
4841 printf("Unable to get runstate response from node %u\n",
4846 enum ctdb_runstate t
;
4848 for (i
=0; i
<argc
; i
++) {
4850 t
= runstate_from_string(argv
[i
]);
4851 if (t
== CTDB_RUNSTATE_UNKNOWN
) {
4852 printf("Invalid run state (%s)\n", argv
[i
]);
4856 if (t
== runstate
) {
4863 printf("CTDB not in required run state (got %s)\n",
4864 runstate_to_string((enum ctdb_runstate
)runstate
));
4869 printf("%s\n", runstate_to_string(runstate
));
4877 static int control_getvar(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4888 ret
= ctdb_ctrl_get_tunable(ctdb
, TIMELIMIT(), options
.pnn
, name
, &value
);
4890 DEBUG(DEBUG_ERR
, ("Unable to get tunable variable '%s'\n", name
));
4894 printf("%-23s = %u\n", name
, value
);
4901 static int control_setvar(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4912 value
= strtoul(argv
[1], NULL
, 0);
4914 ret
= ctdb_ctrl_set_tunable(ctdb
, TIMELIMIT(), options
.pnn
, name
, value
);
4916 DEBUG(DEBUG_ERR
, ("Unable to set tunable variable '%s'\n", name
));
4925 static int control_listvars(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4931 ret
= ctdb_ctrl_list_tunables(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &list
, &count
);
4933 DEBUG(DEBUG_ERR
, ("Unable to list tunable variables\n"));
4937 for (i
=0;i
<count
;i
++) {
4938 control_getvar(ctdb
, 1, &list
[i
]);
4947 display debug level on a node
4949 static int control_getdebug(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4954 ret
= ctdb_ctrl_get_debuglevel(ctdb
, options
.pnn
, &level
);
4956 DEBUG(DEBUG_ERR
, ("Unable to get debuglevel response from node %u\n", options
.pnn
));
4959 if (options
.machinereadable
){
4960 printf(":Name:Level:\n");
4961 printf(":%s:%d:\n",get_debug_by_level(level
),level
);
4963 printf("Node %u is at debug level %s (%d)\n", options
.pnn
, get_debug_by_level(level
), level
);
4970 display reclock file of a node
4972 static int control_getreclock(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4975 const char *reclock
;
4977 ret
= ctdb_ctrl_getreclock(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &reclock
);
4979 DEBUG(DEBUG_ERR
, ("Unable to get reclock file from node %u\n", options
.pnn
));
4982 if (options
.machinereadable
){
4983 if (reclock
!= NULL
) {
4984 printf("%s", reclock
);
4987 if (reclock
== NULL
) {
4988 printf("No reclock file used.\n");
4990 printf("Reclock file:%s\n", reclock
);
4998 set the reclock file of a node
5000 static int control_setreclock(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5003 const char *reclock
;
5007 } else if (argc
== 1) {
5013 ret
= ctdb_ctrl_setreclock(ctdb
, TIMELIMIT(), options
.pnn
, reclock
);
5015 DEBUG(DEBUG_ERR
, ("Unable to get reclock file from node %u\n", options
.pnn
));
5022 set the natgw state on/off
5024 static int control_setnatgwstate(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5027 uint32_t natgwstate
;
5033 if (!strcmp(argv
[0], "on")) {
5035 } else if (!strcmp(argv
[0], "off")) {
5041 ret
= ctdb_ctrl_setnatgwstate(ctdb
, TIMELIMIT(), options
.pnn
, natgwstate
);
5043 DEBUG(DEBUG_ERR
, ("Unable to set the natgw state for node %u\n", options
.pnn
));
5051 set the lmaster role on/off
5053 static int control_setlmasterrole(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5056 uint32_t lmasterrole
;
5062 if (!strcmp(argv
[0], "on")) {
5064 } else if (!strcmp(argv
[0], "off")) {
5070 ret
= ctdb_ctrl_setlmasterrole(ctdb
, TIMELIMIT(), options
.pnn
, lmasterrole
);
5072 DEBUG(DEBUG_ERR
, ("Unable to set the lmaster role for node %u\n", options
.pnn
));
5080 set the recmaster role on/off
5082 static int control_setrecmasterrole(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5085 uint32_t recmasterrole
;
5091 if (!strcmp(argv
[0], "on")) {
5093 } else if (!strcmp(argv
[0], "off")) {
5099 ret
= ctdb_ctrl_setrecmasterrole(ctdb
, TIMELIMIT(), options
.pnn
, recmasterrole
);
5101 DEBUG(DEBUG_ERR
, ("Unable to set the recmaster role for node %u\n", options
.pnn
));
5109 set debug level on a node or all nodes
5111 static int control_setdebug(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5117 printf("You must specify the debug level. Valid levels are:\n");
5118 for (i
=0; debug_levels
[i
].description
!= NULL
; i
++) {
5119 printf("%s (%d)\n", debug_levels
[i
].description
, debug_levels
[i
].level
);
5125 if (isalpha(argv
[0][0]) || argv
[0][0] == '-') {
5126 level
= get_debug_by_desc(argv
[0]);
5128 level
= strtol(argv
[0], NULL
, 0);
5131 for (i
=0; debug_levels
[i
].description
!= NULL
; i
++) {
5132 if (level
== debug_levels
[i
].level
) {
5136 if (debug_levels
[i
].description
== NULL
) {
5137 printf("Invalid debug level, must be one of\n");
5138 for (i
=0; debug_levels
[i
].description
!= NULL
; i
++) {
5139 printf("%s (%d)\n", debug_levels
[i
].description
, debug_levels
[i
].level
);
5144 ret
= ctdb_ctrl_set_debuglevel(ctdb
, options
.pnn
, level
);
5146 DEBUG(DEBUG_ERR
, ("Unable to set debug level on node %u\n", options
.pnn
));
5155 static int control_thaw(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5161 priority
= strtol(argv
[0], NULL
, 0);
5165 DEBUG(DEBUG_ERR
,("Thaw by priority %u\n", priority
));
5167 ret
= ctdb_ctrl_thaw_priority(ctdb
, TIMELIMIT(), options
.pnn
, priority
);
5169 DEBUG(DEBUG_ERR
, ("Unable to thaw node %u\n", options
.pnn
));
5176 attach to a database
5178 static int control_attach(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5180 const char *db_name
;
5181 struct ctdb_db_context
*ctdb_db
;
5182 bool persistent
= false;
5192 if (strcmp(argv
[1], "persistent") != 0) {
5198 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, persistent
, 0);
5199 if (ctdb_db
== NULL
) {
5200 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
5208 * detach from a database
5210 static int control_detach(struct ctdb_context
*ctdb
, int argc
,
5215 int ret
, i
, status
= 0;
5216 struct ctdb_node_map
*nodemap
= NULL
;
5217 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5224 assert_single_node_only();
5226 ret
= ctdb_ctrl_getrecmode(ctdb
, tmp_ctx
, TIMELIMIT(), options
.pnn
,
5229 DEBUG(DEBUG_ERR
, ("Database cannot be detached "
5230 "when recovery is active\n"));
5231 talloc_free(tmp_ctx
);
5235 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
,
5238 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n",
5240 talloc_free(tmp_ctx
);
5244 for (i
=0; i
<nodemap
->num
; i
++) {
5247 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DISCONNECTED
) {
5251 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DELETED
) {
5255 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_INACTIVE
) {
5256 DEBUG(DEBUG_ERR
, ("Database cannot be detached on "
5257 "inactive (stopped or banned) node "
5258 "%u\n", nodemap
->nodes
[i
].pnn
));
5259 talloc_free(tmp_ctx
);
5263 ret
= ctdb_ctrl_get_tunable(ctdb
, TIMELIMIT(),
5264 nodemap
->nodes
[i
].pnn
,
5265 "AllowClientDBAttach",
5268 DEBUG(DEBUG_ERR
, ("Unable to get tunable "
5269 "AllowClientDBAttach from node %u\n",
5270 nodemap
->nodes
[i
].pnn
));
5271 talloc_free(tmp_ctx
);
5276 DEBUG(DEBUG_ERR
, ("Database access is still active on "
5277 "node %u. Set AllowClientDBAttach=0 "
5279 nodemap
->nodes
[i
].pnn
));
5280 talloc_free(tmp_ctx
);
5285 talloc_free(tmp_ctx
);
5287 for (i
=0; i
<argc
; i
++) {
5288 if (!db_exists(ctdb
, argv
[i
], &db_id
, NULL
, &flags
)) {
5292 if (flags
& CTDB_DB_FLAGS_PERSISTENT
) {
5293 DEBUG(DEBUG_ERR
, ("Persistent database '%s' "
5294 "cannot be detached\n", argv
[i
]));
5299 ret
= ctdb_detach(ctdb
, db_id
);
5301 DEBUG(DEBUG_ERR
, ("Database '%s' detach failed\n",
5313 static int control_setdbprio(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5315 struct ctdb_db_priority db_prio
;
5322 db_prio
.db_id
= strtoul(argv
[0], NULL
, 0);
5323 db_prio
.priority
= strtoul(argv
[1], NULL
, 0);
5325 ret
= ctdb_ctrl_set_db_priority(ctdb
, TIMELIMIT(), options
.pnn
, &db_prio
);
5327 DEBUG(DEBUG_ERR
,("Unable to set db prio\n"));
5337 static int control_getdbprio(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5339 uint32_t db_id
, priority
;
5346 if (!db_exists(ctdb
, argv
[0], &db_id
, NULL
, NULL
)) {
5350 ret
= ctdb_ctrl_get_db_priority(ctdb
, TIMELIMIT(), options
.pnn
, db_id
, &priority
);
5352 DEBUG(DEBUG_ERR
,("Unable to get db prio\n"));
5356 DEBUG(DEBUG_ERR
,("Priority:%u\n", priority
));
5362 set the sticky records capability for a database
5364 static int control_setdbsticky(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5366 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5374 if (!db_exists(ctdb
, argv
[0], &db_id
, NULL
, NULL
)) {
5378 ret
= ctdb_ctrl_set_db_sticky(ctdb
, options
.pnn
, db_id
);
5380 DEBUG(DEBUG_ERR
,("Unable to set db to support sticky records\n"));
5381 talloc_free(tmp_ctx
);
5385 talloc_free(tmp_ctx
);
5390 set the readonly capability for a database
5392 static int control_setdbreadonly(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5394 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5402 if (!db_exists(ctdb
, argv
[0], &db_id
, NULL
, NULL
)) {
5406 ret
= ctdb_ctrl_set_db_readonly(ctdb
, options
.pnn
, db_id
);
5408 DEBUG(DEBUG_ERR
,("Unable to set db to support readonly\n"));
5409 talloc_free(tmp_ctx
);
5413 talloc_free(tmp_ctx
);
5420 static int control_getdbseqnum(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5430 if (!db_exists(ctdb
, argv
[0], &db_id
, NULL
, NULL
)) {
5434 ret
= ctdb_ctrl_getdbseqnum(ctdb
, TIMELIMIT(), options
.pnn
, db_id
, &seqnum
);
5436 DEBUG(DEBUG_ERR
, ("Unable to get seqnum from node."));
5440 printf("Sequence number:%lld\n", (long long)seqnum
);
5446 run an eventscript on a node
5448 static int control_eventscript(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5454 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5457 DEBUG(DEBUG_ERR
,("Invalid arguments\n"));
5461 data
.dptr
= (unsigned char *)discard_const(argv
[0]);
5462 data
.dsize
= strlen((char *)data
.dptr
) + 1;
5464 DEBUG(DEBUG_ERR
, ("Running eventscripts with arguments \"%s\" on node %u\n", data
.dptr
, options
.pnn
));
5466 ret
= ctdb_control(ctdb
, options
.pnn
, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS
,
5467 0, data
, tmp_ctx
, NULL
, &res
, NULL
, &errmsg
);
5468 if (ret
!= 0 || res
!= 0) {
5469 DEBUG(DEBUG_ERR
,("Failed to run eventscripts - %s\n", errmsg
));
5470 talloc_free(tmp_ctx
);
5473 talloc_free(tmp_ctx
);
5477 #define DB_VERSION 1
5478 #define MAX_DB_NAME 64
5479 struct db_file_header
{
5480 unsigned long version
;
5482 unsigned long persistent
;
5484 const char name
[MAX_DB_NAME
];
5487 struct backup_data
{
5488 struct ctdb_marshall_buffer
*records
;
5491 bool traverse_error
;
5494 static int backup_traverse(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA data
, void *private)
5496 struct backup_data
*bd
= talloc_get_type(private, struct backup_data
);
5497 struct ctdb_rec_data
*rec
;
5499 /* add the record */
5500 rec
= ctdb_marshall_record(bd
->records
, 0, key
, NULL
, data
);
5502 bd
->traverse_error
= true;
5503 DEBUG(DEBUG_ERR
,("Failed to marshall record\n"));
5506 bd
->records
= talloc_realloc_size(NULL
, bd
->records
, rec
->length
+ bd
->len
);
5507 if (bd
->records
== NULL
) {
5508 DEBUG(DEBUG_ERR
,("Failed to expand marshalling buffer\n"));
5509 bd
->traverse_error
= true;
5512 bd
->records
->count
++;
5513 memcpy(bd
->len
+(uint8_t *)bd
->records
, rec
, rec
->length
);
5514 bd
->len
+= rec
->length
;
5522 * backup a database to a file
5524 static int control_backupdb(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5526 const char *db_name
;
5528 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5529 struct db_file_header dbhdr
;
5530 struct ctdb_db_context
*ctdb_db
;
5531 struct backup_data
*bd
;
5534 const char *reason
= NULL
;
5538 assert_single_node_only();
5541 DEBUG(DEBUG_ERR
,("Invalid arguments\n"));
5545 if (!db_exists(ctdb
, argv
[0], &db_id
, &db_name
, &flags
)) {
5549 ret
= ctdb_ctrl_getdbhealth(ctdb
, TIMELIMIT(), options
.pnn
,
5550 db_id
, tmp_ctx
, &reason
);
5552 DEBUG(DEBUG_ERR
,("Unable to get dbhealth for database '%s'\n",
5554 talloc_free(tmp_ctx
);
5558 uint32_t allow_unhealthy
= 0;
5560 ctdb_ctrl_get_tunable(ctdb
, TIMELIMIT(), options
.pnn
,
5561 "AllowUnhealthyDBRead",
5564 if (allow_unhealthy
!= 1) {
5565 DEBUG(DEBUG_ERR
,("database '%s' is unhealthy: %s\n",
5568 DEBUG(DEBUG_ERR
,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
5570 talloc_free(tmp_ctx
);
5574 DEBUG(DEBUG_WARNING
,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
5576 DEBUG(DEBUG_WARNING
,("WARNING! allow backup of unhealthy database: "
5577 "tunnable AllowUnhealthyDBRead = %u\n",
5581 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, flags
& CTDB_DB_FLAGS_PERSISTENT
, 0);
5582 if (ctdb_db
== NULL
) {
5583 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", argv
[0]));
5584 talloc_free(tmp_ctx
);
5589 ret
= tdb_transaction_start(ctdb_db
->ltdb
->tdb
);
5591 DEBUG(DEBUG_ERR
,("Failed to start transaction\n"));
5592 talloc_free(tmp_ctx
);
5597 bd
= talloc_zero(tmp_ctx
, struct backup_data
);
5599 DEBUG(DEBUG_ERR
,("Failed to allocate backup_data\n"));
5600 talloc_free(tmp_ctx
);
5604 bd
->records
= talloc_zero(bd
, struct ctdb_marshall_buffer
);
5605 if (bd
->records
== NULL
) {
5606 DEBUG(DEBUG_ERR
,("Failed to allocate ctdb_marshall_buffer\n"));
5607 talloc_free(tmp_ctx
);
5611 bd
->len
= offsetof(struct ctdb_marshall_buffer
, data
);
5612 bd
->records
->db_id
= ctdb_db
->db_id
;
5613 /* traverse the database collecting all records */
5614 if (tdb_traverse_read(ctdb_db
->ltdb
->tdb
, backup_traverse
, bd
) == -1 ||
5615 bd
->traverse_error
) {
5616 DEBUG(DEBUG_ERR
,("Traverse error\n"));
5617 talloc_free(tmp_ctx
);
5621 tdb_transaction_cancel(ctdb_db
->ltdb
->tdb
);
5624 fh
= open(argv
[1], O_RDWR
|O_CREAT
, 0600);
5626 DEBUG(DEBUG_ERR
,("Failed to open file '%s'\n", argv
[1]));
5627 talloc_free(tmp_ctx
);
5632 dbhdr
.version
= DB_VERSION
;
5633 dbhdr
.timestamp
= time(NULL
);
5634 dbhdr
.persistent
= flags
& CTDB_DB_FLAGS_PERSISTENT
;
5635 dbhdr
.size
= bd
->len
;
5636 if (strlen(argv
[0]) >= MAX_DB_NAME
) {
5637 DEBUG(DEBUG_ERR
,("Too long dbname\n"));
5640 strncpy(discard_const(dbhdr
.name
), argv
[0], MAX_DB_NAME
-1);
5641 ret
= sys_write(fh
, &dbhdr
, sizeof(dbhdr
));
5643 DEBUG(DEBUG_ERR
,("write failed: %s\n", strerror(errno
)));
5646 ret
= sys_write(fh
, bd
->records
, bd
->len
);
5648 DEBUG(DEBUG_ERR
,("write failed: %s\n", strerror(errno
)));
5657 DEBUG(DEBUG_ERR
,("close failed: %s\n", strerror(errno
)));
5661 DEBUG(DEBUG_ERR
,("Database backed up to %s\n", argv
[1]));
5663 talloc_free(tmp_ctx
);
5668 * restore a database from a file
5670 static int control_restoredb(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5673 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5676 struct db_file_header dbhdr
;
5677 struct ctdb_db_context
*ctdb_db
;
5678 struct ctdb_node_map
*nodemap
=NULL
;
5679 struct ctdb_vnn_map
*vnnmap
=NULL
;
5681 struct ctdb_control_wipe_database w
;
5683 uint32_t generation
;
5688 assert_single_node_only();
5690 if (argc
< 1 || argc
> 2) {
5691 DEBUG(DEBUG_ERR
,("Invalid arguments\n"));
5695 fh
= open(argv
[0], O_RDONLY
);
5697 DEBUG(DEBUG_ERR
,("Failed to open file '%s'\n", argv
[0]));
5698 talloc_free(tmp_ctx
);
5702 sys_read(fh
, &dbhdr
, sizeof(dbhdr
));
5703 if (dbhdr
.version
!= DB_VERSION
) {
5704 DEBUG(DEBUG_ERR
,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr
.version
, DB_VERSION
));
5706 talloc_free(tmp_ctx
);
5710 dbname
= discard_const(dbhdr
.name
);
5712 dbname
= discard_const(argv
[1]);
5715 outdata
.dsize
= dbhdr
.size
;
5716 outdata
.dptr
= talloc_size(tmp_ctx
, outdata
.dsize
);
5717 if (outdata
.dptr
== NULL
) {
5718 DEBUG(DEBUG_ERR
,("Failed to allocate data of size '%lu'\n", dbhdr
.size
));
5720 talloc_free(tmp_ctx
);
5723 sys_read(fh
, outdata
.dptr
, outdata
.dsize
);
5726 tm
= localtime(&dbhdr
.timestamp
);
5727 strftime(tbuf
,sizeof(tbuf
)-1,"%Y/%m/%d %H:%M:%S", tm
);
5728 printf("Restoring database '%s' from backup @ %s\n",
5732 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), dbname
, dbhdr
.persistent
, 0);
5733 if (ctdb_db
== NULL
) {
5734 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", dbname
));
5735 talloc_free(tmp_ctx
);
5739 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &nodemap
);
5741 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
5742 talloc_free(tmp_ctx
);
5747 ret
= ctdb_ctrl_getvnnmap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &vnnmap
);
5749 DEBUG(DEBUG_ERR
, ("Unable to get vnnmap from node %u\n", options
.pnn
));
5750 talloc_free(tmp_ctx
);
5754 /* freeze all nodes */
5755 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5756 for (i
=1; i
<=NUM_DB_PRIORITIES
; i
++) {
5757 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_FREEZE
,
5763 DEBUG(DEBUG_ERR
, ("Unable to freeze nodes.\n"));
5764 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5765 talloc_free(tmp_ctx
);
5770 generation
= vnnmap
->generation
;
5771 data
.dptr
= (void *)&generation
;
5772 data
.dsize
= sizeof(generation
);
5774 /* start a cluster wide transaction */
5775 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5776 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_TRANSACTION_START
,
5778 TIMELIMIT(), false, data
,
5781 DEBUG(DEBUG_ERR
, ("Unable to start cluster wide transactions.\n"));
5786 w
.db_id
= ctdb_db
->db_id
;
5787 w
.transaction_id
= generation
;
5789 data
.dptr
= (void *)&w
;
5790 data
.dsize
= sizeof(w
);
5792 /* wipe all the remote databases. */
5793 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5794 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_WIPE_DATABASE
,
5796 TIMELIMIT(), false, data
,
5799 DEBUG(DEBUG_ERR
, ("Unable to wipe database.\n"));
5800 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5801 talloc_free(tmp_ctx
);
5805 /* push the database */
5806 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5807 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_PUSH_DB
,
5809 TIMELIMIT(), false, outdata
,
5812 DEBUG(DEBUG_ERR
, ("Failed to push database.\n"));
5813 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5814 talloc_free(tmp_ctx
);
5818 data
.dptr
= (void *)&ctdb_db
->db_id
;
5819 data
.dsize
= sizeof(ctdb_db
->db_id
);
5821 /* mark the database as healthy */
5822 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5823 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_DB_SET_HEALTHY
,
5825 TIMELIMIT(), false, data
,
5828 DEBUG(DEBUG_ERR
, ("Failed to mark database as healthy.\n"));
5829 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5830 talloc_free(tmp_ctx
);
5834 data
.dptr
= (void *)&generation
;
5835 data
.dsize
= sizeof(generation
);
5837 /* commit all the changes */
5838 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_TRANSACTION_COMMIT
,
5840 TIMELIMIT(), false, data
,
5843 DEBUG(DEBUG_ERR
, ("Unable to commit databases.\n"));
5844 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5845 talloc_free(tmp_ctx
);
5850 /* thaw all nodes */
5851 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5852 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_THAW
,
5858 DEBUG(DEBUG_ERR
, ("Unable to thaw nodes.\n"));
5859 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5860 talloc_free(tmp_ctx
);
5865 talloc_free(tmp_ctx
);
5870 * dump a database backup from a file
5872 static int control_dumpdbbackup(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5874 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5876 struct db_file_header dbhdr
;
5880 struct ctdb_rec_data
*rec
= NULL
;
5881 struct ctdb_marshall_buffer
*m
;
5882 struct ctdb_dump_db_context c
;
5884 assert_single_node_only();
5887 DEBUG(DEBUG_ERR
,("Invalid arguments\n"));
5891 fh
= open(argv
[0], O_RDONLY
);
5893 DEBUG(DEBUG_ERR
,("Failed to open file '%s'\n", argv
[0]));
5894 talloc_free(tmp_ctx
);
5898 sys_read(fh
, &dbhdr
, sizeof(dbhdr
));
5899 if (dbhdr
.version
!= DB_VERSION
) {
5900 DEBUG(DEBUG_ERR
,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr
.version
, DB_VERSION
));
5902 talloc_free(tmp_ctx
);
5906 outdata
.dsize
= dbhdr
.size
;
5907 outdata
.dptr
= talloc_size(tmp_ctx
, outdata
.dsize
);
5908 if (outdata
.dptr
== NULL
) {
5909 DEBUG(DEBUG_ERR
,("Failed to allocate data of size '%lu'\n", dbhdr
.size
));
5911 talloc_free(tmp_ctx
);
5914 sys_read(fh
, outdata
.dptr
, outdata
.dsize
);
5916 m
= (struct ctdb_marshall_buffer
*)outdata
.dptr
;
5918 tm
= localtime(&dbhdr
.timestamp
);
5919 strftime(tbuf
,sizeof(tbuf
)-1,"%Y/%m/%d %H:%M:%S", tm
);
5920 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
5921 dbhdr
.name
, m
->db_id
, tbuf
);
5925 c
.printemptyrecords
= (bool)options
.printemptyrecords
;
5926 c
.printdatasize
= (bool)options
.printdatasize
;
5927 c
.printlmaster
= false;
5928 c
.printhash
= (bool)options
.printhash
;
5929 c
.printrecordflags
= (bool)options
.printrecordflags
;
5931 for (i
=0; i
< m
->count
; i
++) {
5935 /* we do not want the header splitted, so we pass NULL*/
5936 rec
= ctdb_marshall_loop_next(m
, rec
, &reqid
,
5939 ctdb_dumpdb_record(ctdb
, key
, data
, &c
);
5942 printf("Dumped %d records\n", i
);
5943 talloc_free(tmp_ctx
);
5948 * wipe a database from a file
5950 static int control_wipedb(struct ctdb_context
*ctdb
, int argc
,
5953 const char *db_name
;
5955 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5957 struct ctdb_db_context
*ctdb_db
;
5958 struct ctdb_node_map
*nodemap
= NULL
;
5959 struct ctdb_vnn_map
*vnnmap
= NULL
;
5961 struct ctdb_control_wipe_database w
;
5963 uint32_t generation
;
5966 assert_single_node_only();
5969 DEBUG(DEBUG_ERR
,("Invalid arguments\n"));
5973 if (!db_exists(ctdb
, argv
[0], NULL
, &db_name
, &flags
)) {
5977 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, flags
& CTDB_DB_FLAGS_PERSISTENT
, 0);
5978 if (ctdb_db
== NULL
) {
5979 DEBUG(DEBUG_ERR
, ("Unable to attach to database '%s'\n",
5981 talloc_free(tmp_ctx
);
5985 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
,
5988 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n",
5990 talloc_free(tmp_ctx
);
5994 ret
= ctdb_ctrl_getvnnmap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
,
5997 DEBUG(DEBUG_ERR
, ("Unable to get vnnmap from node %u\n",
5999 talloc_free(tmp_ctx
);
6003 /* freeze all nodes */
6004 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
6005 for (i
=1; i
<=NUM_DB_PRIORITIES
; i
++) {
6006 ret
= ctdb_client_async_control(ctdb
, CTDB_CONTROL_FREEZE
,
6013 DEBUG(DEBUG_ERR
, ("Unable to freeze nodes.\n"));
6014 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
,
6015 CTDB_RECOVERY_ACTIVE
);
6016 talloc_free(tmp_ctx
);
6021 generation
= vnnmap
->generation
;
6022 data
.dptr
= (void *)&generation
;
6023 data
.dsize
= sizeof(generation
);
6025 /* start a cluster wide transaction */
6026 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
6027 ret
= ctdb_client_async_control(ctdb
, CTDB_CONTROL_TRANSACTION_START
,
6029 TIMELIMIT(), false, data
,
6033 DEBUG(DEBUG_ERR
, ("Unable to start cluster wide "
6034 "transactions.\n"));
6038 w
.db_id
= ctdb_db
->db_id
;
6039 w
.transaction_id
= generation
;
6041 data
.dptr
= (void *)&w
;
6042 data
.dsize
= sizeof(w
);
6044 /* wipe all the remote databases. */
6045 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
6046 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_WIPE_DATABASE
,
6048 TIMELIMIT(), false, data
,
6051 DEBUG(DEBUG_ERR
, ("Unable to wipe database.\n"));
6052 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
6053 talloc_free(tmp_ctx
);
6057 data
.dptr
= (void *)&ctdb_db
->db_id
;
6058 data
.dsize
= sizeof(ctdb_db
->db_id
);
6060 /* mark the database as healthy */
6061 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
6062 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_DB_SET_HEALTHY
,
6064 TIMELIMIT(), false, data
,
6067 DEBUG(DEBUG_ERR
, ("Failed to mark database as healthy.\n"));
6068 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
6069 talloc_free(tmp_ctx
);
6073 data
.dptr
= (void *)&generation
;
6074 data
.dsize
= sizeof(generation
);
6076 /* commit all the changes */
6077 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_TRANSACTION_COMMIT
,
6079 TIMELIMIT(), false, data
,
6082 DEBUG(DEBUG_ERR
, ("Unable to commit databases.\n"));
6083 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
6084 talloc_free(tmp_ctx
);
6088 /* thaw all nodes */
6089 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
6090 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_THAW
,
6096 DEBUG(DEBUG_ERR
, ("Unable to thaw nodes.\n"));
6097 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
6098 talloc_free(tmp_ctx
);
6102 DEBUG(DEBUG_ERR
, ("Database wiped.\n"));
6104 talloc_free(tmp_ctx
);
6111 static int control_dumpmemory(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
6117 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
6118 ret
= ctdb_control(ctdb
, options
.pnn
, 0, CTDB_CONTROL_DUMP_MEMORY
,
6119 0, tdb_null
, tmp_ctx
, &data
, &res
, NULL
, &errmsg
);
6120 if (ret
!= 0 || res
!= 0) {
6121 DEBUG(DEBUG_ERR
,("Failed to dump memory - %s\n", errmsg
));
6122 talloc_free(tmp_ctx
);
6125 sys_write(1, data
.dptr
, data
.dsize
);
6126 talloc_free(tmp_ctx
);
6131 handler for memory dumps
6133 static void mem_dump_handler(struct ctdb_context
*ctdb
, uint64_t srvid
,
6134 TDB_DATA data
, void *private_data
)
6136 sys_write(1, data
.dptr
, data
.dsize
);
6141 dump memory usage on the recovery daemon
6143 static int control_rddumpmemory(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
6147 struct srvid_request rd
;
6149 rd
.pnn
= ctdb_get_pnn(ctdb
);
6150 rd
.srvid
= getpid();
6152 /* register a message port for receiveing the reply so that we
6153 can receive the reply
6155 ctdb_client_set_message_handler(ctdb
, rd
.srvid
, mem_dump_handler
, NULL
);
6158 data
.dptr
= (uint8_t *)&rd
;
6159 data
.dsize
= sizeof(rd
);
6161 ret
= ctdb_client_send_message(ctdb
, options
.pnn
, CTDB_SRVID_MEM_DUMP
, data
);
6163 DEBUG(DEBUG_ERR
,("Failed to send memdump request message to %u\n", options
.pnn
));
6167 /* this loop will terminate when we have received the reply */
6169 event_loop_once(ctdb
->ev
);
6176 send a message to a srvid
6178 static int control_msgsend(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
6180 unsigned long srvid
;
6188 srvid
= strtoul(argv
[0], NULL
, 0);
6190 data
.dptr
= (uint8_t *)discard_const(argv
[1]);
6191 data
.dsize
= strlen(argv
[1]);
6193 ret
= ctdb_client_send_message(ctdb
, CTDB_BROADCAST_CONNECTED
, srvid
, data
);
6195 DEBUG(DEBUG_ERR
,("Failed to send memdump request message to %u\n", options
.pnn
));
6203 handler for msglisten
6205 static void msglisten_handler(struct ctdb_context
*ctdb
, uint64_t srvid
,
6206 TDB_DATA data
, void *private_data
)
6210 printf("Message received: ");
6211 for (i
=0;i
<data
.dsize
;i
++) {
6212 printf("%c", data
.dptr
[i
]);
6218 listen for messages on a messageport
6220 static int control_msglisten(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
6226 /* register a message port and listen for messages
6228 ctdb_client_set_message_handler(ctdb
, srvid
, msglisten_handler
, NULL
);
6229 printf("Listening for messages on srvid:%d\n", (int)srvid
);
6232 event_loop_once(ctdb
->ev
);
6239 list all nodes in the cluster
6240 we parse the nodes file directly
6242 static int control_listnodes(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
6244 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
6245 struct pnn_node
*pnn_nodes
;
6246 struct pnn_node
*pnn_node
;
6248 assert_single_node_only();
6250 pnn_nodes
= read_nodes_file(mem_ctx
);
6251 if (pnn_nodes
== NULL
) {
6252 DEBUG(DEBUG_ERR
,("Failed to read nodes file\n"));
6253 talloc_free(mem_ctx
);
6257 for(pnn_node
=pnn_nodes
;pnn_node
;pnn_node
=pnn_node
->next
) {
6258 const char *addr
= ctdb_addr_to_str(&pnn_node
->addr
);
6259 if (options
.machinereadable
){
6260 printf(":%d:%s:\n", pnn_node
->pnn
, addr
);
6262 printf("%s\n", addr
);
6265 talloc_free(mem_ctx
);
6271 reload the nodes file on the local node
6273 static int control_reload_nodes_file(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
6277 struct ctdb_node_map
*nodemap
=NULL
;
6279 assert_single_node_only();
6281 mypnn
= ctdb_get_pnn(ctdb
);
6283 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, ctdb
, &nodemap
);
6285 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from local node\n"));
6289 /* reload the nodes file on all remote nodes */
6290 for (i
=0;i
<nodemap
->num
;i
++) {
6291 if (nodemap
->nodes
[i
].pnn
== mypnn
) {
6294 DEBUG(DEBUG_NOTICE
, ("Reloading nodes file on node %u\n", nodemap
->nodes
[i
].pnn
));
6295 ret
= ctdb_ctrl_reload_nodes_file(ctdb
, TIMELIMIT(),
6296 nodemap
->nodes
[i
].pnn
);
6298 DEBUG(DEBUG_ERR
, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap
->nodes
[i
].pnn
));
6302 /* reload the nodes file on the local node */
6303 DEBUG(DEBUG_NOTICE
, ("Reloading nodes file on node %u\n", mypnn
));
6304 ret
= ctdb_ctrl_reload_nodes_file(ctdb
, TIMELIMIT(), mypnn
);
6306 DEBUG(DEBUG_ERR
, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn
));
6309 /* initiate a recovery */
6310 control_recover(ctdb
, argc
, argv
);
6316 static const struct {
6318 int (*fn
)(struct ctdb_context
*, int, const char **);
6320 bool without_daemon
; /* can be run without daemon running ? */
6323 } ctdb_commands
[] = {
6324 { "version", control_version
, true, true, "show version of ctdb" },
6325 { "status", control_status
, true, false, "show node status" },
6326 { "uptime", control_uptime
, true, false, "show node uptime" },
6327 { "ping", control_ping
, true, false, "ping all nodes" },
6328 { "runstate", control_runstate
, true, false, "get/check runstate of a node", "[setup|first_recovery|startup|running]" },
6329 { "getvar", control_getvar
, true, false, "get a tunable variable", "<name>"},
6330 { "setvar", control_setvar
, true, false, "set a tunable variable", "<name> <value>"},
6331 { "listvars", control_listvars
, true, false, "list tunable variables"},
6332 { "statistics", control_statistics
, false, false, "show statistics" },
6333 { "statisticsreset", control_statistics_reset
, true, false, "reset statistics"},
6334 { "stats", control_stats
, false, false, "show rolling statistics", "[number of history records]" },
6335 { "ip", control_ip
, false, false, "show which public ip's that ctdb manages" },
6336 { "ipinfo", control_ipinfo
, true, false, "show details about a public ip that ctdb manages", "<ip>" },
6337 { "ifaces", control_ifaces
, true, false, "show which interfaces that ctdb manages" },
6338 { "setifacelink", control_setifacelink
, true, false, "set interface link status", "<iface> <status>" },
6339 { "process-exists", control_process_exists
, true, false, "check if a process exists on a node", "<pid>"},
6340 { "getdbmap", control_getdbmap
, true, false, "show the database map" },
6341 { "getdbstatus", control_getdbstatus
, true, false, "show the status of a database", "<dbname|dbid>" },
6342 { "catdb", control_catdb
, true, false, "dump a ctdb database" , "<dbname|dbid>"},
6343 { "cattdb", control_cattdb
, true, false, "dump a local tdb database" , "<dbname|dbid>"},
6344 { "getmonmode", control_getmonmode
, true, false, "show monitoring mode" },
6345 { "getcapabilities", control_getcapabilities
, true, false, "show node capabilities" },
6346 { "pnn", control_pnn
, true, false, "show the pnn of the currnet node" },
6347 { "lvs", control_lvs
, true, false, "show lvs configuration" },
6348 { "lvsmaster", control_lvsmaster
, true, false, "show which node is the lvs master" },
6349 { "disablemonitor", control_disable_monmode
,true, false, "set monitoring mode to DISABLE" },
6350 { "enablemonitor", control_enable_monmode
, true, false, "set monitoring mode to ACTIVE" },
6351 { "setdebug", control_setdebug
, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
6352 { "getdebug", control_getdebug
, true, false, "get debug level" },
6353 { "getlog", control_getlog
, true, false, "get the log data from the in memory ringbuffer", "[<level>] [recoverd]" },
6354 { "clearlog", control_clearlog
, true, false, "clear the log data from the in memory ringbuffer", "[recoverd]" },
6355 { "attach", control_attach
, true, false, "attach to a database", "<dbname> [persistent]" },
6356 { "detach", control_detach
, false, false, "detach from a database", "<dbname|dbid> [<dbname|dbid> ...]" },
6357 { "dumpmemory", control_dumpmemory
, true, false, "dump memory map to stdout" },
6358 { "rddumpmemory", control_rddumpmemory
, true, false, "dump memory map from the recovery daemon to stdout" },
6359 { "getpid", control_getpid
, true, false, "get ctdbd process ID" },
6360 { "disable", control_disable
, true, false, "disable a nodes public IP" },
6361 { "enable", control_enable
, true, false, "enable a nodes public IP" },
6362 { "stop", control_stop
, true, false, "stop a node" },
6363 { "continue", control_continue
, true, false, "re-start a stopped node" },
6364 { "ban", control_ban
, true, false, "ban a node from the cluster", "<bantime>"},
6365 { "unban", control_unban
, true, false, "unban a node" },
6366 { "showban", control_showban
, true, false, "show ban information"},
6367 { "shutdown", control_shutdown
, true, false, "shutdown ctdbd" },
6368 { "recover", control_recover
, true, false, "force recovery" },
6369 { "sync", control_ipreallocate
, false, false, "wait until ctdbd has synced all state changes" },
6370 { "ipreallocate", control_ipreallocate
, false, false, "force the recovery daemon to perform a ip reallocation procedure" },
6371 { "thaw", control_thaw
, true, false, "thaw databases", "[priority:1-3]" },
6372 { "isnotrecmaster", control_isnotrecmaster
, false, false, "check if the local node is recmaster or not" },
6373 { "killtcp", kill_tcp
, false, false, "kill a tcp connection.", "[<srcip:port> <dstip:port>]" },
6374 { "gratiousarp", control_gratious_arp
, false, false, "send a gratious arp", "<ip> <interface>" },
6375 { "tickle", tickle_tcp
, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
6376 { "gettickles", control_get_tickles
, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
6377 { "addtickle", control_add_tickle
, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
6379 { "deltickle", control_del_tickle
, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
6381 { "regsrvid", regsrvid
, false, false, "register a server id", "<pnn> <type> <id>" },
6382 { "unregsrvid", unregsrvid
, false, false, "unregister a server id", "<pnn> <type> <id>" },
6383 { "chksrvid", chksrvid
, false, false, "check if a server id exists", "<pnn> <type> <id>" },
6384 { "getsrvids", getsrvids
, false, false, "get a list of all server ids"},
6385 { "check_srvids", check_srvids
, false, false, "check if a srvid exists", "<id>+" },
6386 { "repack", ctdb_repack
, false, false, "repack all databases", "[max_freelist]"},
6387 { "listnodes", control_listnodes
, false, true, "list all nodes in the cluster"},
6388 { "reloadnodes", control_reload_nodes_file
, false, false, "reload the nodes file and restart the transport on all nodes"},
6389 { "moveip", control_moveip
, false, false, "move/failover an ip address to another node", "<ip> <node>"},
6390 { "rebalanceip", control_rebalanceip
, false, false, "release an ip from the node and let recd rebalance it", "<ip>"},
6391 { "addip", control_addip
, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
6392 { "delip", control_delip
, false, false, "delete an ip address from a node", "<ip>"},
6393 { "eventscript", control_eventscript
, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
6394 { "backupdb", control_backupdb
, false, false, "backup the database into a file.", "<dbname|dbid> <file>"},
6395 { "restoredb", control_restoredb
, false, false, "restore the database from a file.", "<file> [dbname]"},
6396 { "dumpdbbackup", control_dumpdbbackup
, false, true, "dump database backup from a file.", "<file>"},
6397 { "wipedb", control_wipedb
, false, false, "wipe the contents of a database.", "<dbname|dbid>"},
6398 { "recmaster", control_recmaster
, true, false, "show the pnn for the recovery master."},
6399 { "scriptstatus", control_scriptstatus
, true, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
6400 { "enablescript", control_enablescript
, true, false, "enable an eventscript", "<script>"},
6401 { "disablescript", control_disablescript
, true, false, "disable an eventscript", "<script>"},
6402 { "natgwlist", control_natgwlist
, true, false, "show the nodes belonging to this natgw configuration"},
6403 { "xpnn", control_xpnn
, false, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
6404 { "getreclock", control_getreclock
, true, false, "Show the reclock file of a node"},
6405 { "setreclock", control_setreclock
, true, false, "Set/clear the reclock file of a node", "[filename]"},
6406 { "setnatgwstate", control_setnatgwstate
, false, false, "Set NATGW state to on/off", "{on|off}"},
6407 { "setlmasterrole", control_setlmasterrole
, false, false, "Set LMASTER role to on/off", "{on|off}"},
6408 { "setrecmasterrole", control_setrecmasterrole
, false, false, "Set RECMASTER role to on/off", "{on|off}"},
6409 { "setdbprio", control_setdbprio
, false, false, "Set DB priority", "<dbname|dbid> <prio:1-3>"},
6410 { "getdbprio", control_getdbprio
, false, false, "Get DB priority", "<dbname|dbid>"},
6411 { "setdbreadonly", control_setdbreadonly
, false, false, "Set DB readonly capable", "<dbname|dbid>"},
6412 { "setdbsticky", control_setdbsticky
, false, false, "Set DB sticky-records capable", "<dbname|dbid>"},
6413 { "msglisten", control_msglisten
, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
6414 { "msgsend", control_msgsend
, false, false, "Send a message to srvid", "<srvid> <message>"},
6415 { "pfetch", control_pfetch
, false, false, "fetch a record from a persistent database", "<dbname|dbid> <key> [<file>]" },
6416 { "pstore", control_pstore
, false, false, "write a record to a persistent database", "<dbname|dbid> <key> <file containing record>" },
6417 { "pdelete", control_pdelete
, false, false, "delete a record from a persistent database", "<dbname|dbid> <key>" },
6418 { "ptrans", control_ptrans
, false, false, "update a persistent database (from stdin)", "<dbname|dbid>" },
6419 { "tfetch", control_tfetch
, false, true, "fetch a record from a [c]tdb-file [-v]", "<tdb-file> <key> [<file>]" },
6420 { "tstore", control_tstore
, false, true, "store a record (including ltdb header)", "<tdb-file> <key> <data> [<rsn> <dmaster> <flags>]" },
6421 { "readkey", control_readkey
, true, false, "read the content off a database key", "<dbname|dbid> <key>" },
6422 { "writekey", control_writekey
, true, false, "write to a database key", "<dbname|dbid> <key> <value>" },
6423 { "checktcpport", control_chktcpport
, false, true, "check if a service is bound to a specific tcp port or not", "<port>" },
6424 { "rebalancenode", control_rebalancenode
, false, false, "mark nodes as forced IP rebalancing targets", "[<pnn-list>]"},
6425 { "getdbseqnum", control_getdbseqnum
, false, false, "get the sequence number off a database", "<dbname|dbid>" },
6426 { "nodestatus", control_nodestatus
, true, false, "show and return node status", "[<pnn-list>]" },
6427 { "dbstatistics", control_dbstatistics
, false, false, "show db statistics", "<dbname|dbid>" },
6428 { "reloadips", control_reloadips
, false, false, "reload the public addresses file on specified nodes" , "[<pnn-list>]" },
6429 { "ipiface", control_ipiface
, false, true, "Find which interface an ip address is hosted on", "<ip>" },
6435 static void usage(void)
6439 "Usage: ctdb [options] <control>\n" \
6441 " -n <node> choose node number, or 'all' (defaults to local node)\n"
6442 " -Y generate machinereadable output\n"
6443 " -v generate verbose output\n"
6444 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options
.timelimit
);
6445 printf("Controls:\n");
6446 for (i
=0;i
<ARRAY_SIZE(ctdb_commands
);i
++) {
6447 printf(" %-15s %-27s %s\n",
6448 ctdb_commands
[i
].name
,
6449 ctdb_commands
[i
].args
?ctdb_commands
[i
].args
:"",
6450 ctdb_commands
[i
].msg
);
6456 static void ctdb_alarm(int sig
)
6458 printf("Maximum runtime exceeded - exiting\n");
6465 int main(int argc
, const char *argv
[])
6467 struct ctdb_context
*ctdb
;
6468 char *nodestring
= NULL
;
6469 struct poptOption popt_options
[] = {
6472 { "timelimit", 't', POPT_ARG_INT
, &options
.timelimit
, 0, "timelimit", "integer" },
6473 { "node", 'n', POPT_ARG_STRING
, &nodestring
, 0, "node", "integer|all" },
6474 { "machinereadable", 'Y', POPT_ARG_NONE
, &options
.machinereadable
, 0, "enable machinereadable output", NULL
},
6475 { "verbose", 'v', POPT_ARG_NONE
, &options
.verbose
, 0, "enable verbose output", NULL
},
6476 { "maxruntime", 'T', POPT_ARG_INT
, &options
.maxruntime
, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
6477 { "print-emptyrecords", 0, POPT_ARG_NONE
, &options
.printemptyrecords
, 0, "print the empty records when dumping databases (catdb, cattdb, dumpdbbackup)", NULL
},
6478 { "print-datasize", 0, POPT_ARG_NONE
, &options
.printdatasize
, 0, "do not print record data when dumping databases, only the data size", NULL
},
6479 { "print-lmaster", 0, POPT_ARG_NONE
, &options
.printlmaster
, 0, "print the record's lmaster in catdb", NULL
},
6480 { "print-hash", 0, POPT_ARG_NONE
, &options
.printhash
, 0, "print the record's hash when dumping databases", NULL
},
6481 { "print-recordflags", 0, POPT_ARG_NONE
, &options
.printrecordflags
, 0, "print the record flags in catdb and dumpdbbackup", NULL
},
6485 const char **extra_argv
;
6489 struct event_context
*ev
;
6490 const char *control
;
6494 /* set some defaults */
6495 options
.maxruntime
= 0;
6496 options
.timelimit
= 10;
6497 options
.pnn
= CTDB_CURRENT_NODE
;
6499 pc
= poptGetContext(argv
[0], argc
, argv
, popt_options
, POPT_CONTEXT_KEEP_FIRST
);
6501 while ((opt
= poptGetNextOpt(pc
)) != -1) {
6504 DEBUG(DEBUG_ERR
, ("Invalid option %s: %s\n",
6505 poptBadOption(pc
, 0), poptStrerror(opt
)));
6510 /* setup the remaining options for the main program to use */
6511 extra_argv
= poptGetArgs(pc
);
6514 while (extra_argv
[extra_argc
]) extra_argc
++;
6517 if (extra_argc
< 1) {
6521 if (options
.maxruntime
== 0) {
6522 const char *ctdb_timeout
;
6523 ctdb_timeout
= getenv("CTDB_TIMEOUT");
6524 if (ctdb_timeout
!= NULL
) {
6525 options
.maxruntime
= strtoul(ctdb_timeout
, NULL
, 0);
6527 /* default timeout is 120 seconds */
6528 options
.maxruntime
= 120;
6532 signal(SIGALRM
, ctdb_alarm
);
6533 alarm(options
.maxruntime
);
6535 control
= extra_argv
[0];
6537 /* Default value for CTDB_BASE - don't override */
6538 setenv("CTDB_BASE", CTDB_ETCDIR
, 0);
6540 ev
= event_context_init(NULL
);
6542 DEBUG(DEBUG_ERR
, ("Failed to initialize event system\n"));
6546 for (i
=0;i
<ARRAY_SIZE(ctdb_commands
);i
++) {
6547 if (strcmp(control
, ctdb_commands
[i
].name
) == 0) {
6552 if (i
== ARRAY_SIZE(ctdb_commands
)) {
6553 DEBUG(DEBUG_ERR
, ("Unknown control '%s'\n", control
));
6557 if (ctdb_commands
[i
].without_daemon
== true) {
6558 if (nodestring
!= NULL
) {
6559 DEBUG(DEBUG_ERR
, ("Can't specify node(s) with \"ctdb %s\"\n", control
));
6562 return ctdb_commands
[i
].fn(NULL
, extra_argc
-1, extra_argv
+1);
6565 /* initialise ctdb */
6566 ctdb
= ctdb_cmdline_client(ev
, TIMELIMIT());
6570 DEBUG(DEBUG_ERR
, ("Failed to init ctdb\n"));
6572 pnn
= find_node_xpnn();
6575 ("Is this node part of a CTDB cluster?\n"));
6580 /* setup the node number(s) to contact */
6581 if (!parse_nodestring(ctdb
, ctdb
, nodestring
, CTDB_CURRENT_NODE
, false,
6582 &options
.nodes
, &options
.pnn
)) {
6586 if (options
.pnn
== CTDB_CURRENT_NODE
) {
6587 options
.pnn
= options
.nodes
[0];
6590 if (ctdb_commands
[i
].auto_all
&&
6591 ((options
.pnn
== CTDB_BROADCAST_ALL
) ||
6592 (options
.pnn
== CTDB_MULTICAST
))) {
6596 for (j
= 0; j
< talloc_array_length(options
.nodes
); j
++) {
6597 options
.pnn
= options
.nodes
[j
];
6598 ret
|= ctdb_commands
[i
].fn(ctdb
, extra_argc
-1, extra_argv
+1);
6601 ret
= ctdb_commands
[i
].fn(ctdb
, extra_argc
-1, extra_argv
+1);
6606 (void)poptFreeContext(pc
);