ctdb-tools-ctdb: Update LVS commands to use filter_nodemap_by_capabilities()
[Samba/wip.git] / ctdb / tools / ctdb.c
blob758fd9343af25a6ddf6fe1096aa5d1218193f23f
1 /*
2 ctdb control tool
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/>.
21 #include "includes.h"
22 #include "system/time.h"
23 #include "system/filesys.h"
24 #include "system/network.h"
25 #include "system/locale.h"
26 #include "popt.h"
27 #include "cmdline.h"
28 #include "../include/ctdb_version.h"
29 #include "../include/ctdb_client.h"
30 #include "../include/ctdb_private.h"
31 #include "../common/rb_tree.h"
32 #include "db_wrap.h"
34 #define ERR_TIMEOUT 20 /* timed out trying to reach node */
35 #define ERR_NONODE 21 /* node does not exist */
36 #define ERR_DISNODE 22 /* node is disconnected */
38 static void usage(void);
40 static struct {
41 int timelimit;
42 uint32_t pnn;
43 uint32_t *nodes;
44 int machinereadable;
45 int verbose;
46 int maxruntime;
47 int printemptyrecords;
48 int printdatasize;
49 int printlmaster;
50 int printhash;
51 int printrecordflags;
52 } options;
54 #define LONGTIMEOUT options.timelimit*10
56 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
57 #define LONGTIMELIMIT() timeval_current_ofs(LONGTIMEOUT, 0)
59 static int control_version(struct ctdb_context *ctdb, int argc, const char **argv)
61 printf("CTDB version: %s\n", CTDB_VERSION_STRING);
62 return 0;
65 #define CTDB_NOMEM_ABORT(p) do { if (!(p)) { \
66 DEBUG(DEBUG_ALERT,("ctdb fatal error: %s\n", \
67 "Out of memory in " __location__ )); \
68 abort(); \
69 }} while (0)
71 static uint32_t getpnn(struct ctdb_context *ctdb)
73 if ((options.pnn == CTDB_BROADCAST_ALL) ||
74 (options.pnn == CTDB_MULTICAST)) {
75 DEBUG(DEBUG_ERR,
76 ("Cannot get PNN for node %u\n", options.pnn));
77 exit(1);
80 if (options.pnn == CTDB_CURRENT_NODE) {
81 return ctdb_get_pnn(ctdb);
82 } else {
83 return options.pnn;
87 static void assert_single_node_only(void)
89 if ((options.pnn == CTDB_BROADCAST_ALL) ||
90 (options.pnn == CTDB_MULTICAST)) {
91 DEBUG(DEBUG_ERR,
92 ("This control can not be applied to multiple PNNs\n"));
93 exit(1);
97 /* Pretty print the flags to a static buffer in human-readable format.
98 * This never returns NULL!
100 static const char *pretty_print_flags(uint32_t flags)
102 int j;
103 static const struct {
104 uint32_t flag;
105 const char *name;
106 } flag_names[] = {
107 { NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
108 { NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
109 { NODE_FLAGS_BANNED, "BANNED" },
110 { NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
111 { NODE_FLAGS_DELETED, "DELETED" },
112 { NODE_FLAGS_STOPPED, "STOPPED" },
113 { NODE_FLAGS_INACTIVE, "INACTIVE" },
115 static char flags_str[512]; /* Big enough to contain all flag names */
117 flags_str[0] = '\0';
118 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
119 if (flags & flag_names[j].flag) {
120 if (flags_str[0] == '\0') {
121 (void) strcpy(flags_str, flag_names[j].name);
122 } else {
123 (void) strncat(flags_str, "|", sizeof(flags_str)-1);
124 (void) strncat(flags_str, flag_names[j].name,
125 sizeof(flags_str)-1);
129 if (flags_str[0] == '\0') {
130 (void) strcpy(flags_str, "OK");
133 return flags_str;
136 static int h2i(char h)
138 if (h >= 'a' && h <= 'f') return h - 'a' + 10;
139 if (h >= 'A' && h <= 'F') return h - 'f' + 10;
140 return h - '0';
143 static TDB_DATA hextodata(TALLOC_CTX *mem_ctx, const char *str)
145 int i, len;
146 TDB_DATA key = {NULL, 0};
148 len = strlen(str);
149 if (len & 0x01) {
150 DEBUG(DEBUG_ERR,("Key specified with odd number of hexadecimal digits\n"));
151 return key;
154 key.dsize = len>>1;
155 key.dptr = talloc_size(mem_ctx, key.dsize);
157 for (i=0; i < len/2; i++) {
158 key.dptr[i] = h2i(str[i*2]) << 4 | h2i(str[i*2+1]);
160 return key;
163 /* Parse a nodestring. Parameter dd_ok controls what happens to nodes
164 * that are disconnected or deleted. If dd_ok is true those nodes are
165 * included in the output list of nodes. If dd_ok is false, those
166 * nodes are filtered from the "all" case and cause an error if
167 * explicitly specified.
169 static bool parse_nodestring(struct ctdb_context *ctdb,
170 TALLOC_CTX *mem_ctx,
171 const char * nodestring,
172 uint32_t current_pnn,
173 bool dd_ok,
174 uint32_t **nodes,
175 uint32_t *pnn_mode)
177 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
178 int n;
179 uint32_t i;
180 struct ctdb_node_map *nodemap;
181 int ret;
183 *nodes = NULL;
185 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
186 if (ret != 0) {
187 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
188 talloc_free(tmp_ctx);
189 exit(10);
192 if (nodestring != NULL) {
193 *nodes = talloc_array(mem_ctx, uint32_t, 0);
194 if (*nodes == NULL) {
195 goto failed;
198 n = 0;
200 if (strcmp(nodestring, "all") == 0) {
201 *pnn_mode = CTDB_BROADCAST_ALL;
203 /* all */
204 for (i = 0; i < nodemap->num; i++) {
205 if ((nodemap->nodes[i].flags &
206 (NODE_FLAGS_DISCONNECTED |
207 NODE_FLAGS_DELETED)) && !dd_ok) {
208 continue;
210 *nodes = talloc_realloc(mem_ctx, *nodes,
211 uint32_t, n+1);
212 if (*nodes == NULL) {
213 goto failed;
215 (*nodes)[n] = i;
216 n++;
218 } else {
219 /* x{,y...} */
220 char *ns, *tok;
222 ns = talloc_strdup(tmp_ctx, nodestring);
223 tok = strtok(ns, ",");
224 while (tok != NULL) {
225 uint32_t pnn;
226 char *endptr;
227 i = (uint32_t)strtoul(tok, &endptr, 0);
228 if (i == 0 && tok == endptr) {
229 DEBUG(DEBUG_ERR,
230 ("Invalid node %s\n", tok));
231 talloc_free(tmp_ctx);
232 exit(ERR_NONODE);
234 if (i >= nodemap->num) {
235 DEBUG(DEBUG_ERR, ("Node %u does not exist\n", i));
236 talloc_free(tmp_ctx);
237 exit(ERR_NONODE);
239 if ((nodemap->nodes[i].flags &
240 (NODE_FLAGS_DISCONNECTED |
241 NODE_FLAGS_DELETED)) && !dd_ok) {
242 DEBUG(DEBUG_ERR, ("Node %u has status %s\n", i, pretty_print_flags(nodemap->nodes[i].flags)));
243 talloc_free(tmp_ctx);
244 exit(ERR_DISNODE);
246 if ((pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), i)) < 0) {
247 DEBUG(DEBUG_ERR, ("Can not access node %u. Node is not operational.\n", i));
248 talloc_free(tmp_ctx);
249 exit(10);
252 *nodes = talloc_realloc(mem_ctx, *nodes,
253 uint32_t, n+1);
254 if (*nodes == NULL) {
255 goto failed;
258 (*nodes)[n] = i;
259 n++;
261 tok = strtok(NULL, ",");
263 talloc_free(ns);
265 if (n == 1) {
266 *pnn_mode = (*nodes)[0];
267 } else {
268 *pnn_mode = CTDB_MULTICAST;
271 } else {
272 /* default - no nodes specified */
273 *nodes = talloc_array(mem_ctx, uint32_t, 1);
274 if (*nodes == NULL) {
275 goto failed;
277 *pnn_mode = CTDB_CURRENT_NODE;
279 if (((*nodes)[0] = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), current_pnn)) < 0) {
280 goto failed;
284 talloc_free(tmp_ctx);
285 return true;
287 failed:
288 talloc_free(tmp_ctx);
289 return false;
293 check if a database exists
295 static bool db_exists(struct ctdb_context *ctdb, const char *dbarg,
296 uint32_t *dbid, const char **dbname, uint8_t *flags)
298 int i, ret;
299 struct ctdb_dbid_map *dbmap=NULL;
300 bool dbid_given = false, found = false;
301 uint32_t id;
302 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
303 const char *name;
305 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
306 if (ret != 0) {
307 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
308 goto fail;
311 if (strncmp(dbarg, "0x", 2) == 0) {
312 id = strtoul(dbarg, NULL, 0);
313 dbid_given = true;
316 for(i=0; i<dbmap->num; i++) {
317 if (dbid_given) {
318 if (id == dbmap->dbs[i].dbid) {
319 found = true;
320 break;
322 } else {
323 ret = ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
324 if (ret != 0) {
325 DEBUG(DEBUG_ERR, ("Unable to get dbname from dbid %u\n", dbmap->dbs[i].dbid));
326 goto fail;
329 if (strcmp(name, dbarg) == 0) {
330 id = dbmap->dbs[i].dbid;
331 found = true;
332 break;
337 if (found && dbid_given && dbname != NULL) {
338 ret = ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
339 if (ret != 0) {
340 DEBUG(DEBUG_ERR, ("Unable to get dbname from dbid %u\n", dbmap->dbs[i].dbid));
341 found = false;
342 goto fail;
346 if (found) {
347 if (dbid) *dbid = id;
348 if (dbname) *dbname = talloc_strdup(ctdb, name);
349 if (flags) *flags = dbmap->dbs[i].flags;
350 } else {
351 DEBUG(DEBUG_ERR,("No database matching '%s' found\n", dbarg));
354 fail:
355 talloc_free(tmp_ctx);
356 return found;
360 see if a process exists
362 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
364 uint32_t pnn, pid;
365 int ret;
366 if (argc < 1) {
367 usage();
370 if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
371 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
372 return -1;
375 ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
376 if (ret == 0) {
377 printf("%u:%u exists\n", pnn, pid);
378 } else {
379 printf("%u:%u does not exist\n", pnn, pid);
381 return ret;
385 display statistics structure
387 static void show_statistics(struct ctdb_statistics *s, int show_header)
389 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
390 int i;
391 const char *prefix=NULL;
392 int preflen=0;
393 int tmp, days, hours, minutes, seconds;
394 const struct {
395 const char *name;
396 uint32_t offset;
397 } fields[] = {
398 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
399 STATISTICS_FIELD(num_clients),
400 STATISTICS_FIELD(frozen),
401 STATISTICS_FIELD(recovering),
402 STATISTICS_FIELD(num_recoveries),
403 STATISTICS_FIELD(client_packets_sent),
404 STATISTICS_FIELD(client_packets_recv),
405 STATISTICS_FIELD(node_packets_sent),
406 STATISTICS_FIELD(node_packets_recv),
407 STATISTICS_FIELD(keepalive_packets_sent),
408 STATISTICS_FIELD(keepalive_packets_recv),
409 STATISTICS_FIELD(node.req_call),
410 STATISTICS_FIELD(node.reply_call),
411 STATISTICS_FIELD(node.req_dmaster),
412 STATISTICS_FIELD(node.reply_dmaster),
413 STATISTICS_FIELD(node.reply_error),
414 STATISTICS_FIELD(node.req_message),
415 STATISTICS_FIELD(node.req_control),
416 STATISTICS_FIELD(node.reply_control),
417 STATISTICS_FIELD(client.req_call),
418 STATISTICS_FIELD(client.req_message),
419 STATISTICS_FIELD(client.req_control),
420 STATISTICS_FIELD(timeouts.call),
421 STATISTICS_FIELD(timeouts.control),
422 STATISTICS_FIELD(timeouts.traverse),
423 STATISTICS_FIELD(locks.num_calls),
424 STATISTICS_FIELD(locks.num_current),
425 STATISTICS_FIELD(locks.num_pending),
426 STATISTICS_FIELD(locks.num_failed),
427 STATISTICS_FIELD(total_calls),
428 STATISTICS_FIELD(pending_calls),
429 STATISTICS_FIELD(childwrite_calls),
430 STATISTICS_FIELD(pending_childwrite_calls),
431 STATISTICS_FIELD(memory_used),
432 STATISTICS_FIELD(max_hop_count),
433 STATISTICS_FIELD(total_ro_delegations),
434 STATISTICS_FIELD(total_ro_revokes),
437 tmp = s->statistics_current_time.tv_sec - s->statistics_start_time.tv_sec;
438 seconds = tmp%60;
439 tmp /= 60;
440 minutes = tmp%60;
441 tmp /= 60;
442 hours = tmp%24;
443 tmp /= 24;
444 days = tmp;
446 if (options.machinereadable){
447 if (show_header) {
448 printf("CTDB version:");
449 printf("Current time of statistics:");
450 printf("Statistics collected since:");
451 for (i=0;i<ARRAY_SIZE(fields);i++) {
452 printf("%s:", fields[i].name);
454 printf("num_reclock_ctdbd_latency:");
455 printf("min_reclock_ctdbd_latency:");
456 printf("avg_reclock_ctdbd_latency:");
457 printf("max_reclock_ctdbd_latency:");
459 printf("num_reclock_recd_latency:");
460 printf("min_reclock_recd_latency:");
461 printf("avg_reclock_recd_latency:");
462 printf("max_reclock_recd_latency:");
464 printf("num_call_latency:");
465 printf("min_call_latency:");
466 printf("avg_call_latency:");
467 printf("max_call_latency:");
469 printf("num_lockwait_latency:");
470 printf("min_lockwait_latency:");
471 printf("avg_lockwait_latency:");
472 printf("max_lockwait_latency:");
474 printf("num_childwrite_latency:");
475 printf("min_childwrite_latency:");
476 printf("avg_childwrite_latency:");
477 printf("max_childwrite_latency:");
478 printf("\n");
480 printf("%d:", CTDB_VERSION);
481 printf("%d:", (int)s->statistics_current_time.tv_sec);
482 printf("%d:", (int)s->statistics_start_time.tv_sec);
483 for (i=0;i<ARRAY_SIZE(fields);i++) {
484 printf("%d:", *(uint32_t *)(fields[i].offset+(uint8_t *)s));
486 printf("%d:", s->reclock.ctdbd.num);
487 printf("%.6f:", s->reclock.ctdbd.min);
488 printf("%.6f:", s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0);
489 printf("%.6f:", s->reclock.ctdbd.max);
491 printf("%d:", s->reclock.recd.num);
492 printf("%.6f:", s->reclock.recd.min);
493 printf("%.6f:", s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0);
494 printf("%.6f:", s->reclock.recd.max);
496 printf("%d:", s->call_latency.num);
497 printf("%.6f:", s->call_latency.min);
498 printf("%.6f:", s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0);
499 printf("%.6f:", s->call_latency.max);
501 printf("%d:", s->childwrite_latency.num);
502 printf("%.6f:", s->childwrite_latency.min);
503 printf("%.6f:", s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0);
504 printf("%.6f:", s->childwrite_latency.max);
505 printf("\n");
506 } else {
507 printf("CTDB version %u\n", CTDB_VERSION);
508 printf("Current time of statistics : %s", ctime(&s->statistics_current_time.tv_sec));
509 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&s->statistics_start_time.tv_sec));
511 for (i=0;i<ARRAY_SIZE(fields);i++) {
512 if (strchr(fields[i].name, '.')) {
513 preflen = strcspn(fields[i].name, ".")+1;
514 if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
515 prefix = fields[i].name;
516 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
518 } else {
519 preflen = 0;
521 printf(" %*s%-22s%*s%10u\n",
522 preflen?4:0, "",
523 fields[i].name+preflen,
524 preflen?0:4, "",
525 *(uint32_t *)(fields[i].offset+(uint8_t *)s));
527 printf(" hop_count_buckets:");
528 for (i=0;i<MAX_COUNT_BUCKETS;i++) {
529 printf(" %d", s->hop_count_bucket[i]);
531 printf("\n");
532 printf(" lock_buckets:");
533 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
534 printf(" %d", s->locks.buckets[i]);
536 printf("\n");
537 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n", "locks_latency MIN/AVG/MAX", s->locks.latency.min, s->locks.latency.num?s->locks.latency.total/s->locks.latency.num:0.0, s->locks.latency.max, s->locks.latency.num);
539 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n", "reclock_ctdbd MIN/AVG/MAX", s->reclock.ctdbd.min, s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0, s->reclock.ctdbd.max, s->reclock.ctdbd.num);
541 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n", "reclock_recd MIN/AVG/MAX", s->reclock.recd.min, s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0, s->reclock.recd.max, s->reclock.recd.num);
543 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n", "call_latency MIN/AVG/MAX", s->call_latency.min, s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0, s->call_latency.max, s->call_latency.num);
544 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n", "childwrite_latency MIN/AVG/MAX", s->childwrite_latency.min, s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0, s->childwrite_latency.max, s->childwrite_latency.num);
547 talloc_free(tmp_ctx);
551 display remote ctdb statistics combined from all nodes
553 static int control_statistics_all(struct ctdb_context *ctdb)
555 int ret, i;
556 struct ctdb_statistics statistics;
557 uint32_t *nodes;
558 uint32_t num_nodes;
560 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
561 CTDB_NO_MEMORY(ctdb, nodes);
563 ZERO_STRUCT(statistics);
565 for (i=0;i<num_nodes;i++) {
566 struct ctdb_statistics s1;
567 int j;
568 uint32_t *v1 = (uint32_t *)&s1;
569 uint32_t *v2 = (uint32_t *)&statistics;
570 uint32_t num_ints =
571 offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
572 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
573 if (ret != 0) {
574 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
575 return ret;
577 for (j=0;j<num_ints;j++) {
578 v2[j] += v1[j];
580 statistics.max_hop_count =
581 MAX(statistics.max_hop_count, s1.max_hop_count);
582 statistics.call_latency.max =
583 MAX(statistics.call_latency.max, s1.call_latency.max);
585 talloc_free(nodes);
586 printf("Gathered statistics for %u nodes\n", num_nodes);
587 show_statistics(&statistics, 1);
588 return 0;
592 display remote ctdb statistics
594 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
596 int ret;
597 struct ctdb_statistics statistics;
599 if (options.pnn == CTDB_BROADCAST_ALL) {
600 return control_statistics_all(ctdb);
603 ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
604 if (ret != 0) {
605 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
606 return ret;
608 show_statistics(&statistics, 1);
609 return 0;
614 reset remote ctdb statistics
616 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
618 int ret;
620 ret = ctdb_statistics_reset(ctdb, options.pnn);
621 if (ret != 0) {
622 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
623 return ret;
625 return 0;
630 display remote ctdb rolling statistics
632 static int control_stats(struct ctdb_context *ctdb, int argc, const char **argv)
634 int ret;
635 struct ctdb_statistics_wire *stats;
636 int i, num_records = -1;
638 assert_single_node_only();
640 if (argc ==1) {
641 num_records = atoi(argv[0]) - 1;
644 ret = ctdb_ctrl_getstathistory(ctdb, TIMELIMIT(), options.pnn, ctdb, &stats);
645 if (ret != 0) {
646 DEBUG(DEBUG_ERR, ("Unable to get rolling statistics from node %u\n", options.pnn));
647 return ret;
649 for (i=0;i<stats->num;i++) {
650 if (stats->stats[i].statistics_start_time.tv_sec == 0) {
651 continue;
653 show_statistics(&stats->stats[i], i==0);
654 if (i == num_records) {
655 break;
658 return 0;
663 display remote ctdb db statistics
665 static int control_dbstatistics(struct ctdb_context *ctdb, int argc, const char **argv)
667 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
668 struct ctdb_db_statistics *dbstat;
669 int i;
670 uint32_t db_id;
671 int num_hot_keys;
672 int ret;
674 if (argc < 1) {
675 usage();
678 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
679 return -1;
682 ret = ctdb_ctrl_dbstatistics(ctdb, options.pnn, db_id, tmp_ctx, &dbstat);
683 if (ret != 0) {
684 DEBUG(DEBUG_ERR,("Failed to read db statistics from node\n"));
685 talloc_free(tmp_ctx);
686 return -1;
689 printf("DB Statistics: %s\n", argv[0]);
690 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_delegations", 4, "",
691 dbstat->db_ro_delegations);
692 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_revokes", 4, "",
693 dbstat->db_ro_delegations);
694 printf(" %s\n", "locks");
695 printf(" %*s%-22s%*s%10u\n", 4, "", "total", 0, "",
696 dbstat->locks.num_calls);
697 printf(" %*s%-22s%*s%10u\n", 4, "", "failed", 0, "",
698 dbstat->locks.num_failed);
699 printf(" %*s%-22s%*s%10u\n", 4, "", "current", 0, "",
700 dbstat->locks.num_current);
701 printf(" %*s%-22s%*s%10u\n", 4, "", "pending", 0, "",
702 dbstat->locks.num_pending);
703 printf(" %s", "hop_count_buckets:");
704 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
705 printf(" %d", dbstat->hop_count_bucket[i]);
707 printf("\n");
708 printf(" %s", "lock_buckets:");
709 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
710 printf(" %d", dbstat->locks.buckets[i]);
712 printf("\n");
713 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
714 "locks_latency MIN/AVG/MAX",
715 dbstat->locks.latency.min,
716 (dbstat->locks.latency.num ?
717 dbstat->locks.latency.total /dbstat->locks.latency.num :
718 0.0),
719 dbstat->locks.latency.max,
720 dbstat->locks.latency.num);
721 num_hot_keys = 0;
722 for (i=0; i<dbstat->num_hot_keys; i++) {
723 if (dbstat->hot_keys[i].count > 0) {
724 num_hot_keys++;
727 dbstat->num_hot_keys = num_hot_keys;
729 printf(" Num Hot Keys: %d\n", dbstat->num_hot_keys);
730 for (i = 0; i < dbstat->num_hot_keys; i++) {
731 int j;
732 printf(" Count:%d Key:", dbstat->hot_keys[i].count);
733 for (j = 0; j < dbstat->hot_keys[i].key.dsize; j++) {
734 printf("%02x", dbstat->hot_keys[i].key.dptr[j]&0xff);
736 printf("\n");
739 talloc_free(tmp_ctx);
740 return 0;
744 display uptime of remote node
746 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
748 int ret;
749 struct ctdb_uptime *uptime = NULL;
750 int tmp, days, hours, minutes, seconds;
752 ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
753 if (ret != 0) {
754 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
755 return ret;
758 if (options.machinereadable){
759 printf(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
760 printf(":%u:%u:%u:%lf\n",
761 (unsigned int)uptime->current_time.tv_sec,
762 (unsigned int)uptime->ctdbd_start_time.tv_sec,
763 (unsigned int)uptime->last_recovery_finished.tv_sec,
764 timeval_delta(&uptime->last_recovery_finished,
765 &uptime->last_recovery_started)
767 return 0;
770 printf("Current time of node : %s", ctime(&uptime->current_time.tv_sec));
772 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
773 seconds = tmp%60;
774 tmp /= 60;
775 minutes = tmp%60;
776 tmp /= 60;
777 hours = tmp%24;
778 tmp /= 24;
779 days = tmp;
780 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
782 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
783 seconds = tmp%60;
784 tmp /= 60;
785 minutes = tmp%60;
786 tmp /= 60;
787 hours = tmp%24;
788 tmp /= 24;
789 days = tmp;
790 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
792 printf("Duration of last recovery/failover: %lf seconds\n",
793 timeval_delta(&uptime->last_recovery_finished,
794 &uptime->last_recovery_started));
796 return 0;
800 show the PNN of the current node
802 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
804 uint32_t mypnn;
806 mypnn = getpnn(ctdb);
808 printf("PNN:%d\n", mypnn);
809 return 0;
813 struct pnn_node {
814 struct pnn_node *next;
815 const char *addr;
816 int pnn;
819 static struct pnn_node *read_nodes_file(TALLOC_CTX *mem_ctx)
821 const char *nodes_list;
822 int nlines;
823 char **lines;
824 int i, pnn;
825 struct pnn_node *pnn_nodes = NULL;
826 struct pnn_node *pnn_node;
827 struct pnn_node *tmp_node;
829 /* read the nodes file */
830 nodes_list = getenv("CTDB_NODES");
831 if (nodes_list == NULL) {
832 nodes_list = talloc_asprintf(mem_ctx, "%s/nodes",
833 getenv("CTDB_BASE"));
834 if (nodes_list == NULL) {
835 DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
836 exit(1);
839 lines = file_lines_load(nodes_list, &nlines, mem_ctx);
840 if (lines == NULL) {
841 return NULL;
843 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
844 nlines--;
846 for (i=0, pnn=0; i<nlines; i++) {
847 char *node;
849 node = lines[i];
850 /* strip leading spaces */
851 while((*node == ' ') || (*node == '\t')) {
852 node++;
854 if (*node == '#') {
855 pnn++;
856 continue;
858 if (strcmp(node, "") == 0) {
859 continue;
861 pnn_node = talloc(mem_ctx, struct pnn_node);
862 pnn_node->pnn = pnn++;
863 pnn_node->addr = talloc_strdup(pnn_node, node);
864 pnn_node->next = pnn_nodes;
865 pnn_nodes = pnn_node;
868 /* swap them around so we return them in incrementing order */
869 pnn_node = pnn_nodes;
870 pnn_nodes = NULL;
871 while (pnn_node) {
872 tmp_node = pnn_node;
873 pnn_node = pnn_node->next;
875 tmp_node->next = pnn_nodes;
876 pnn_nodes = tmp_node;
879 return pnn_nodes;
883 show the PNN of the current node
884 discover the pnn by loading the nodes file and try to bind to all
885 addresses one at a time until the ip address is found.
887 static int control_xpnn(struct ctdb_context *ctdb, int argc, const char **argv)
889 TALLOC_CTX *mem_ctx = talloc_new(NULL);
890 struct pnn_node *pnn_nodes;
891 struct pnn_node *pnn_node;
893 assert_single_node_only();
895 pnn_nodes = read_nodes_file(mem_ctx);
896 if (pnn_nodes == NULL) {
897 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
898 talloc_free(mem_ctx);
899 return -1;
902 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
903 ctdb_sock_addr addr;
905 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
906 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
907 talloc_free(mem_ctx);
908 return -1;
911 if (ctdb_sys_have_ip(&addr)) {
912 printf("PNN:%d\n", pnn_node->pnn);
913 talloc_free(mem_ctx);
914 return 0;
918 printf("Failed to detect which PNN this node is\n");
919 talloc_free(mem_ctx);
920 return -1;
923 /* Helpers for ctdb status
925 static bool is_partially_online(struct ctdb_context *ctdb, struct ctdb_node_and_flags *node)
927 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
928 int j;
929 bool ret = false;
931 if (node->flags == 0) {
932 struct ctdb_control_get_ifaces *ifaces;
934 if (ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), node->pnn,
935 tmp_ctx, &ifaces) == 0) {
936 for (j=0; j < ifaces->num; j++) {
937 if (ifaces->ifaces[j].link_state != 0) {
938 continue;
940 ret = true;
941 break;
945 talloc_free(tmp_ctx);
947 return ret;
950 static void control_status_header_machine(void)
952 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
953 ":Inactive:PartiallyOnline:ThisNode:\n");
956 static int control_status_1_machine(struct ctdb_context *ctdb, int mypnn,
957 struct ctdb_node_and_flags *node)
959 printf(":%d:%s:%d:%d:%d:%d:%d:%d:%d:%c:\n", node->pnn,
960 ctdb_addr_to_str(&node->addr),
961 !!(node->flags&NODE_FLAGS_DISCONNECTED),
962 !!(node->flags&NODE_FLAGS_BANNED),
963 !!(node->flags&NODE_FLAGS_PERMANENTLY_DISABLED),
964 !!(node->flags&NODE_FLAGS_UNHEALTHY),
965 !!(node->flags&NODE_FLAGS_STOPPED),
966 !!(node->flags&NODE_FLAGS_INACTIVE),
967 is_partially_online(ctdb, node) ? 1 : 0,
968 (node->pnn == mypnn)?'Y':'N');
970 return node->flags;
973 static int control_status_1_human(struct ctdb_context *ctdb, int mypnn,
974 struct ctdb_node_and_flags *node)
976 printf("pnn:%d %-16s %s%s\n", node->pnn,
977 ctdb_addr_to_str(&node->addr),
978 is_partially_online(ctdb, node) ? "PARTIALLYONLINE" : pretty_print_flags(node->flags),
979 node->pnn == mypnn?" (THIS NODE)":"");
981 return node->flags;
985 display remote ctdb status
987 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
989 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
990 int i;
991 struct ctdb_vnn_map *vnnmap=NULL;
992 struct ctdb_node_map *nodemap=NULL;
993 uint32_t recmode, recmaster, mypnn;
994 int num_deleted_nodes = 0;
995 int ret;
997 mypnn = getpnn(ctdb);
999 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1000 if (ret != 0) {
1001 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1002 talloc_free(tmp_ctx);
1003 return -1;
1006 if (options.machinereadable) {
1007 control_status_header_machine();
1008 for (i=0;i<nodemap->num;i++) {
1009 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1010 continue;
1012 (void) control_status_1_machine(ctdb, mypnn,
1013 &nodemap->nodes[i]);
1015 talloc_free(tmp_ctx);
1016 return 0;
1019 for (i=0; i<nodemap->num; i++) {
1020 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1021 num_deleted_nodes++;
1024 if (num_deleted_nodes == 0) {
1025 printf("Number of nodes:%d\n", nodemap->num);
1026 } else {
1027 printf("Number of nodes:%d (including %d deleted nodes)\n",
1028 nodemap->num, num_deleted_nodes);
1030 for(i=0;i<nodemap->num;i++){
1031 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1032 continue;
1034 (void) control_status_1_human(ctdb, mypnn, &nodemap->nodes[i]);
1037 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
1038 if (ret != 0) {
1039 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
1040 talloc_free(tmp_ctx);
1041 return -1;
1043 if (vnnmap->generation == INVALID_GENERATION) {
1044 printf("Generation:INVALID\n");
1045 } else {
1046 printf("Generation:%d\n",vnnmap->generation);
1048 printf("Size:%d\n",vnnmap->size);
1049 for(i=0;i<vnnmap->size;i++){
1050 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
1053 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmode);
1054 if (ret != 0) {
1055 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
1056 talloc_free(tmp_ctx);
1057 return -1;
1059 printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
1061 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmaster);
1062 if (ret != 0) {
1063 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1064 talloc_free(tmp_ctx);
1065 return -1;
1067 printf("Recovery master:%d\n",recmaster);
1069 talloc_free(tmp_ctx);
1070 return 0;
1073 static int control_nodestatus(struct ctdb_context *ctdb, int argc, const char **argv)
1075 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1076 int i, ret;
1077 struct ctdb_node_map *nodemap=NULL;
1078 uint32_t * nodes;
1079 uint32_t pnn_mode, mypnn;
1081 if (argc > 1) {
1082 usage();
1085 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1086 options.pnn, true, &nodes, &pnn_mode)) {
1087 return -1;
1090 if (options.machinereadable) {
1091 control_status_header_machine();
1092 } else if (pnn_mode == CTDB_BROADCAST_ALL) {
1093 printf("Number of nodes:%d\n", (int) talloc_array_length(nodes));
1096 mypnn = getpnn(ctdb);
1098 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1099 if (ret != 0) {
1100 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1101 talloc_free(tmp_ctx);
1102 return -1;
1105 ret = 0;
1107 for (i = 0; i < talloc_array_length(nodes); i++) {
1108 if (options.machinereadable) {
1109 ret |= control_status_1_machine(ctdb, mypnn,
1110 &nodemap->nodes[nodes[i]]);
1111 } else {
1112 ret |= control_status_1_human(ctdb, mypnn,
1113 &nodemap->nodes[nodes[i]]);
1117 talloc_free(tmp_ctx);
1118 return ret;
1121 struct natgw_node {
1122 struct natgw_node *next;
1123 const char *addr;
1126 /* talloc off the existing nodemap... */
1127 static struct ctdb_node_map *talloc_nodemap(struct ctdb_node_map *nodemap)
1129 return talloc_zero_size(nodemap,
1130 offsetof(struct ctdb_node_map, nodes) +
1131 nodemap->num * sizeof(struct ctdb_node_and_flags));
1134 static struct ctdb_node_map *
1135 filter_nodemap_by_capabilities(struct ctdb_context *ctdb,
1136 struct ctdb_node_map *nodemap,
1137 uint32_t required_capabilities)
1139 int i;
1140 uint32_t capabilities;
1141 struct ctdb_node_map *ret;
1143 ret = talloc_nodemap(nodemap);
1144 CTDB_NO_MEMORY_NULL(ctdb, ret);
1146 ret->num = 0;
1148 for (i = 0; i < nodemap->num; i++) {
1149 int res = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(),
1150 nodemap->nodes[i].pnn,
1151 &capabilities);
1152 if (res != 0) {
1153 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n",
1154 nodemap->nodes[i].pnn));
1155 talloc_free(ret);
1156 return NULL;
1158 if (!(capabilities & required_capabilities)) {
1159 continue;
1162 ret->nodes[ret->num] = nodemap->nodes[i];
1163 ret->num++;
1166 return ret;
1169 static struct ctdb_node_map *
1170 filter_nodemap_by_flags(struct ctdb_context *ctdb,
1171 struct ctdb_node_map *nodemap,
1172 uint32_t flags_mask)
1174 int i;
1175 struct ctdb_node_map *ret;
1177 ret = talloc_nodemap(nodemap);
1178 CTDB_NO_MEMORY_NULL(ctdb, ret);
1180 ret->num = 0;
1182 for (i = 0; i < nodemap->num; i++) {
1183 if (nodemap->nodes[i].flags & flags_mask) {
1184 continue;
1187 ret->nodes[ret->num] = nodemap->nodes[i];
1188 ret->num++;
1191 return ret;
1195 display the list of nodes belonging to this natgw configuration
1197 static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **argv)
1199 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1200 int i, ret;
1201 const char *natgw_list;
1202 int nlines;
1203 char **lines;
1204 struct natgw_node *natgw_nodes = NULL;
1205 struct natgw_node *natgw_node;
1206 struct ctdb_node_map *nodemap=NULL;
1207 struct ctdb_node_map *cnodemap;
1208 uint32_t mypnn, pnn;
1209 const char *ip;
1211 /* When we have some nodes that could be the NATGW, make a
1212 * series of attempts to find the first node that doesn't have
1213 * certain status flags set.
1215 uint32_t exclude_flags[] = {
1216 /* Look for a nice healthy node */
1217 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_UNHEALTHY,
1218 /* If not found, an UNHEALTHY/BANNED node will do */
1219 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED,
1220 /* If not found, a STOPPED node will do */
1221 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED,
1225 /* read the natgw nodes file into a linked list */
1226 natgw_list = getenv("CTDB_NATGW_NODES");
1227 if (natgw_list == NULL) {
1228 natgw_list = talloc_asprintf(tmp_ctx, "%s/natgw_nodes",
1229 getenv("CTDB_BASE"));
1230 if (natgw_list == NULL) {
1231 DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
1232 exit(1);
1235 lines = file_lines_load(natgw_list, &nlines, ctdb);
1236 if (lines == NULL) {
1237 ctdb_set_error(ctdb, "Failed to load natgw node list '%s'\n", natgw_list);
1238 talloc_free(tmp_ctx);
1239 return -1;
1241 for (i=0;i<nlines;i++) {
1242 char *node;
1244 node = lines[i];
1245 /* strip leading spaces */
1246 while((*node == ' ') || (*node == '\t')) {
1247 node++;
1249 if (*node == '#') {
1250 continue;
1252 if (strcmp(node, "") == 0) {
1253 continue;
1255 natgw_node = talloc(ctdb, struct natgw_node);
1256 natgw_node->addr = talloc_strdup(natgw_node, node);
1257 CTDB_NO_MEMORY(ctdb, natgw_node->addr);
1258 natgw_node->next = natgw_nodes;
1259 natgw_nodes = natgw_node;
1262 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1263 if (ret != 0) {
1264 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node.\n"));
1265 talloc_free(tmp_ctx);
1266 return -1;
1269 /* Trim the nodemap so it only includes connected nodes in the
1270 * current natgw group.
1272 i=0;
1273 while(i<nodemap->num) {
1274 for(natgw_node=natgw_nodes;natgw_node;natgw_node=natgw_node->next) {
1275 if (!strcmp(natgw_node->addr, ctdb_addr_to_str(&nodemap->nodes[i].addr))) {
1276 break;
1280 /* this node was not in the natgw so we just remove it from
1281 * the list
1283 if ((natgw_node == NULL)
1284 || (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) ) {
1285 int j;
1287 for (j=i+1; j<nodemap->num; j++) {
1288 nodemap->nodes[j-1] = nodemap->nodes[j];
1290 nodemap->num--;
1291 continue;
1294 i++;
1297 /* Get a nodemap that includes only the nodes with the NATGW
1298 * capability */
1299 cnodemap = filter_nodemap_by_capabilities(ctdb, nodemap,
1300 CTDB_CAP_NATGW);
1301 if (cnodemap == NULL) {
1302 ret = -1;
1303 goto done;
1306 ret = 2; /* matches ENOENT */
1307 pnn = -1;
1308 ip = "0.0.0.0";
1309 for (i = 0; exclude_flags[i] != 0; i++) {
1310 struct ctdb_node_map *t =
1311 filter_nodemap_by_flags(ctdb, cnodemap,
1312 exclude_flags[i]);
1313 if (t == NULL) {
1314 /* No memory */
1315 ret = -1;
1316 goto done;
1318 if (t->num > 0) {
1319 ret = 0;
1320 pnn = t->nodes[0].pnn;
1321 ip = ctdb_addr_to_str(&t->nodes[0].addr);
1322 break;
1324 talloc_free(t);
1327 if (options.machinereadable) {
1328 printf(":Node:IP:\n");
1329 printf(":%d:%s:\n", pnn, ip);
1330 } else {
1331 printf("%d %s\n", pnn, ip);
1334 /* print the pruned list of nodes belonging to this natgw list */
1335 mypnn = getpnn(ctdb);
1336 if (options.machinereadable) {
1337 control_status_header_machine();
1338 } else {
1339 printf("Number of nodes:%d\n", nodemap->num);
1341 for(i=0;i<nodemap->num;i++){
1342 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1343 continue;
1345 if (options.machinereadable) {
1346 control_status_1_machine(ctdb, mypnn, &(nodemap->nodes[i]));
1347 } else {
1348 control_status_1_human(ctdb, mypnn, &(nodemap->nodes[i]));
1352 done:
1353 talloc_free(tmp_ctx);
1354 return ret;
1358 display the status of the scripts for monitoring (or other events)
1360 static int control_one_scriptstatus(struct ctdb_context *ctdb,
1361 enum ctdb_eventscript_call type)
1363 struct ctdb_scripts_wire *script_status;
1364 int ret, i;
1366 ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
1367 if (ret != 0) {
1368 DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
1369 return ret;
1372 if (script_status == NULL) {
1373 if (!options.machinereadable) {
1374 printf("%s cycle never run\n",
1375 ctdb_eventscript_call_names[type]);
1377 return 0;
1380 if (!options.machinereadable) {
1381 printf("%d scripts were executed last %s cycle\n",
1382 script_status->num_scripts,
1383 ctdb_eventscript_call_names[type]);
1385 for (i=0; i<script_status->num_scripts; i++) {
1386 const char *status = NULL;
1388 switch (script_status->scripts[i].status) {
1389 case -ETIME:
1390 status = "TIMEDOUT";
1391 break;
1392 case -ENOEXEC:
1393 status = "DISABLED";
1394 break;
1395 case 0:
1396 status = "OK";
1397 break;
1398 default:
1399 if (script_status->scripts[i].status > 0)
1400 status = "ERROR";
1401 break;
1403 if (options.machinereadable) {
1404 printf(":%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
1405 ctdb_eventscript_call_names[type],
1406 script_status->scripts[i].name,
1407 script_status->scripts[i].status,
1408 status,
1409 (long)script_status->scripts[i].start.tv_sec,
1410 (long)script_status->scripts[i].start.tv_usec,
1411 (long)script_status->scripts[i].finished.tv_sec,
1412 (long)script_status->scripts[i].finished.tv_usec,
1413 script_status->scripts[i].output);
1414 continue;
1416 if (status)
1417 printf("%-20s Status:%s ",
1418 script_status->scripts[i].name, status);
1419 else
1420 /* Some other error, eg from stat. */
1421 printf("%-20s Status:CANNOT RUN (%s)",
1422 script_status->scripts[i].name,
1423 strerror(-script_status->scripts[i].status));
1425 if (script_status->scripts[i].status >= 0) {
1426 printf("Duration:%.3lf ",
1427 timeval_delta(&script_status->scripts[i].finished,
1428 &script_status->scripts[i].start));
1430 if (script_status->scripts[i].status != -ENOEXEC) {
1431 printf("%s",
1432 ctime(&script_status->scripts[i].start.tv_sec));
1433 if (script_status->scripts[i].status != 0) {
1434 printf(" OUTPUT:%s\n",
1435 script_status->scripts[i].output);
1437 } else {
1438 printf("\n");
1441 return 0;
1445 static int control_scriptstatus(struct ctdb_context *ctdb,
1446 int argc, const char **argv)
1448 int ret;
1449 enum ctdb_eventscript_call type, min, max;
1450 const char *arg;
1452 if (argc > 1) {
1453 DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
1454 return -1;
1457 if (argc == 0)
1458 arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
1459 else
1460 arg = argv[0];
1462 for (type = 0; type < CTDB_EVENT_MAX; type++) {
1463 if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
1464 min = type;
1465 max = type+1;
1466 break;
1469 if (type == CTDB_EVENT_MAX) {
1470 if (strcmp(arg, "all") == 0) {
1471 min = 0;
1472 max = CTDB_EVENT_MAX;
1473 } else {
1474 DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
1475 return -1;
1479 if (options.machinereadable) {
1480 printf(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1483 for (type = min; type < max; type++) {
1484 ret = control_one_scriptstatus(ctdb, type);
1485 if (ret != 0) {
1486 return ret;
1490 return 0;
1494 enable an eventscript
1496 static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1498 int ret;
1500 if (argc < 1) {
1501 usage();
1504 ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1505 if (ret != 0) {
1506 DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
1507 return ret;
1510 return 0;
1514 disable an eventscript
1516 static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1518 int ret;
1520 if (argc < 1) {
1521 usage();
1524 ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1525 if (ret != 0) {
1526 DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
1527 return ret;
1530 return 0;
1534 display the pnn of the recovery master
1536 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1538 uint32_t recmaster;
1539 int ret;
1541 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
1542 if (ret != 0) {
1543 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1544 return -1;
1546 printf("%d\n",recmaster);
1548 return 0;
1552 add a tickle to a public address
1554 static int control_add_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1556 struct ctdb_tcp_connection t;
1557 TDB_DATA data;
1558 int ret;
1560 assert_single_node_only();
1562 if (argc < 2) {
1563 usage();
1566 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1567 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1568 return -1;
1570 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1571 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1572 return -1;
1575 data.dptr = (uint8_t *)&t;
1576 data.dsize = sizeof(t);
1578 /* tell all nodes about this tcp connection */
1579 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE,
1580 0, data, ctdb, NULL, NULL, NULL, NULL);
1581 if (ret != 0) {
1582 DEBUG(DEBUG_ERR,("Failed to add tickle\n"));
1583 return -1;
1586 return 0;
1591 delete a tickle from a node
1593 static int control_del_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1595 struct ctdb_tcp_connection t;
1596 TDB_DATA data;
1597 int ret;
1599 assert_single_node_only();
1601 if (argc < 2) {
1602 usage();
1605 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1606 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1607 return -1;
1609 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1610 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1611 return -1;
1614 data.dptr = (uint8_t *)&t;
1615 data.dsize = sizeof(t);
1617 /* tell all nodes about this tcp connection */
1618 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_REMOVE,
1619 0, data, ctdb, NULL, NULL, NULL, NULL);
1620 if (ret != 0) {
1621 DEBUG(DEBUG_ERR,("Failed to remove tickle\n"));
1622 return -1;
1625 return 0;
1630 get a list of all tickles for this pnn
1632 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
1634 struct ctdb_control_tcp_tickle_list *list;
1635 ctdb_sock_addr addr;
1636 int i, ret;
1637 unsigned port = 0;
1639 assert_single_node_only();
1641 if (argc < 1) {
1642 usage();
1645 if (argc == 2) {
1646 port = atoi(argv[1]);
1649 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1650 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1651 return -1;
1654 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
1655 if (ret == -1) {
1656 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
1657 return -1;
1660 if (options.machinereadable){
1661 printf(":source ip:port:destination ip:port:\n");
1662 for (i=0;i<list->tickles.num;i++) {
1663 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1664 continue;
1666 printf(":%s:%u", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1667 printf(":%s:%u:\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
1669 } else {
1670 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
1671 printf("Num tickles:%u\n", list->tickles.num);
1672 for (i=0;i<list->tickles.num;i++) {
1673 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1674 continue;
1676 printf("SRC: %s:%u ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1677 printf("DST: %s:%u\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
1681 talloc_free(list);
1683 return 0;
1687 static int move_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1689 struct ctdb_all_public_ips *ips;
1690 struct ctdb_public_ip ip;
1691 int i, ret;
1692 uint32_t *nodes;
1693 uint32_t disable_time;
1694 TDB_DATA data;
1695 struct ctdb_node_map *nodemap=NULL;
1696 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1698 disable_time = 30;
1699 data.dptr = (uint8_t*)&disable_time;
1700 data.dsize = sizeof(disable_time);
1701 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1702 if (ret != 0) {
1703 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1704 return -1;
1709 /* read the public ip list from the node */
1710 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
1711 if (ret != 0) {
1712 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
1713 talloc_free(tmp_ctx);
1714 return -1;
1717 for (i=0;i<ips->num;i++) {
1718 if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
1719 break;
1722 if (i==ips->num) {
1723 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
1724 pnn, ctdb_addr_to_str(addr)));
1725 talloc_free(tmp_ctx);
1726 return -1;
1729 ip.pnn = pnn;
1730 ip.addr = *addr;
1732 data.dptr = (uint8_t *)&ip;
1733 data.dsize = sizeof(ip);
1735 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1736 if (ret != 0) {
1737 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1738 talloc_free(tmp_ctx);
1739 return ret;
1742 nodes = list_of_nodes(ctdb, nodemap, tmp_ctx, NODE_FLAGS_INACTIVE, pnn);
1743 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1744 nodes, 0,
1745 LONGTIMELIMIT(),
1746 false, data,
1747 NULL, NULL,
1748 NULL);
1749 if (ret != 0) {
1750 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1751 talloc_free(tmp_ctx);
1752 return -1;
1755 ret = ctdb_ctrl_takeover_ip(ctdb, LONGTIMELIMIT(), pnn, &ip);
1756 if (ret != 0) {
1757 DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
1758 talloc_free(tmp_ctx);
1759 return -1;
1762 /* update the recovery daemon so it now knows to expect the new
1763 node assignment for this ip.
1765 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_RECD_UPDATE_IP, data);
1766 if (ret != 0) {
1767 DEBUG(DEBUG_ERR,("Failed to send message to update the ip on the recovery master.\n"));
1768 return -1;
1771 talloc_free(tmp_ctx);
1772 return 0;
1777 * scans all other nodes and returns a pnn for another node that can host this
1778 * ip address or -1
1780 static int
1781 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1783 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1784 struct ctdb_all_public_ips *ips;
1785 struct ctdb_node_map *nodemap=NULL;
1786 int i, j, ret;
1788 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1789 if (ret != 0) {
1790 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1791 talloc_free(tmp_ctx);
1792 return ret;
1795 for(i=0;i<nodemap->num;i++){
1796 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1797 continue;
1799 if (nodemap->nodes[i].pnn == options.pnn) {
1800 continue;
1803 /* read the public ip list from this node */
1804 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
1805 if (ret != 0) {
1806 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1807 return -1;
1810 for (j=0;j<ips->num;j++) {
1811 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1812 talloc_free(tmp_ctx);
1813 return nodemap->nodes[i].pnn;
1816 talloc_free(ips);
1819 talloc_free(tmp_ctx);
1820 return -1;
1823 /* If pnn is -1 then try to find a node to move IP to... */
1824 static bool try_moveip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1826 bool pnn_specified = (pnn == -1 ? false : true);
1827 int retries = 0;
1829 while (retries < 5) {
1830 if (!pnn_specified) {
1831 pnn = find_other_host_for_public_ip(ctdb, addr);
1832 if (pnn == -1) {
1833 return false;
1835 DEBUG(DEBUG_NOTICE,
1836 ("Trying to move public IP to node %u\n", pnn));
1839 if (move_ip(ctdb, addr, pnn) == 0) {
1840 return true;
1843 sleep(3);
1844 retries++;
1847 return false;
1852 move/failover an ip address to a specific node
1854 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
1856 uint32_t pnn;
1857 ctdb_sock_addr addr;
1859 assert_single_node_only();
1861 if (argc < 2) {
1862 usage();
1863 return -1;
1866 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1867 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1868 return -1;
1872 if (sscanf(argv[1], "%u", &pnn) != 1) {
1873 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
1874 return -1;
1877 if (!try_moveip(ctdb, &addr, pnn)) {
1878 DEBUG(DEBUG_ERR,("Failed to move IP to node %d.\n", pnn));
1879 return -1;
1882 return 0;
1885 static int rebalance_node(struct ctdb_context *ctdb, uint32_t pnn)
1887 TDB_DATA data;
1889 data.dptr = (uint8_t *)&pnn;
1890 data.dsize = sizeof(uint32_t);
1891 if (ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_REBALANCE_NODE, data) != 0) {
1892 DEBUG(DEBUG_ERR,
1893 ("Failed to send message to force node %u to be a rebalancing target\n",
1894 pnn));
1895 return -1;
1898 return 0;
1903 rebalance a node by setting it to allow failback and triggering a
1904 takeover run
1906 static int control_rebalancenode(struct ctdb_context *ctdb, int argc, const char **argv)
1908 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1909 uint32_t *nodes;
1910 uint32_t pnn_mode;
1911 int i, ret;
1913 assert_single_node_only();
1915 if (argc > 1) {
1916 usage();
1919 /* Determine the nodes where IPs need to be reloaded */
1920 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1921 options.pnn, true, &nodes, &pnn_mode)) {
1922 ret = -1;
1923 goto done;
1926 for (i = 0; i < talloc_array_length(nodes); i++) {
1927 if (!rebalance_node(ctdb, nodes[i])) {
1928 ret = -1;
1932 done:
1933 talloc_free(tmp_ctx);
1934 return ret;
1937 static int rebalance_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1939 struct ctdb_public_ip ip;
1940 int ret;
1941 uint32_t *nodes;
1942 uint32_t disable_time;
1943 TDB_DATA data;
1944 struct ctdb_node_map *nodemap=NULL;
1945 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1947 disable_time = 30;
1948 data.dptr = (uint8_t*)&disable_time;
1949 data.dsize = sizeof(disable_time);
1950 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1951 if (ret != 0) {
1952 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1953 return -1;
1956 ip.pnn = -1;
1957 ip.addr = *addr;
1959 data.dptr = (uint8_t *)&ip;
1960 data.dsize = sizeof(ip);
1962 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1963 if (ret != 0) {
1964 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1965 talloc_free(tmp_ctx);
1966 return ret;
1969 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
1970 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1971 nodes, 0,
1972 LONGTIMELIMIT(),
1973 false, data,
1974 NULL, NULL,
1975 NULL);
1976 if (ret != 0) {
1977 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1978 talloc_free(tmp_ctx);
1979 return -1;
1982 talloc_free(tmp_ctx);
1983 return 0;
1987 release an ip form all nodes and have it re-assigned by recd
1989 static int control_rebalanceip(struct ctdb_context *ctdb, int argc, const char **argv)
1991 ctdb_sock_addr addr;
1993 assert_single_node_only();
1995 if (argc < 1) {
1996 usage();
1997 return -1;
2000 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2001 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2002 return -1;
2005 if (rebalance_ip(ctdb, &addr) != 0) {
2006 DEBUG(DEBUG_ERR,("Error when trying to reassign ip\n"));
2007 return -1;
2010 return 0;
2013 static int getips_store_callback(void *param, void *data)
2015 struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
2016 struct ctdb_all_public_ips *ips = param;
2017 int i;
2019 i = ips->num++;
2020 ips->ips[i].pnn = node_ip->pnn;
2021 ips->ips[i].addr = node_ip->addr;
2022 return 0;
2025 static int getips_count_callback(void *param, void *data)
2027 uint32_t *count = param;
2029 (*count)++;
2030 return 0;
2033 #define IP_KEYLEN 4
2034 static uint32_t *ip_key(ctdb_sock_addr *ip)
2036 static uint32_t key[IP_KEYLEN];
2038 bzero(key, sizeof(key));
2040 switch (ip->sa.sa_family) {
2041 case AF_INET:
2042 key[0] = ip->ip.sin_addr.s_addr;
2043 break;
2044 case AF_INET6: {
2045 uint32_t *s6_a32 = (uint32_t *)&(ip->ip6.sin6_addr.s6_addr);
2046 key[0] = s6_a32[3];
2047 key[1] = s6_a32[2];
2048 key[2] = s6_a32[1];
2049 key[3] = s6_a32[0];
2050 break;
2052 default:
2053 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
2054 return key;
2057 return key;
2060 static void *add_ip_callback(void *parm, void *data)
2062 return parm;
2065 static int
2066 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
2068 struct ctdb_all_public_ips *tmp_ips;
2069 struct ctdb_node_map *nodemap=NULL;
2070 trbt_tree_t *ip_tree;
2071 int i, j, len, ret;
2072 uint32_t count;
2074 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2075 if (ret != 0) {
2076 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2077 return ret;
2080 ip_tree = trbt_create(tmp_ctx, 0);
2082 for(i=0;i<nodemap->num;i++){
2083 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
2084 continue;
2086 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
2087 continue;
2090 /* read the public ip list from this node */
2091 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
2092 if (ret != 0) {
2093 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
2094 return -1;
2097 for (j=0; j<tmp_ips->num;j++) {
2098 struct ctdb_public_ip *node_ip;
2100 node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
2101 node_ip->pnn = tmp_ips->ips[j].pnn;
2102 node_ip->addr = tmp_ips->ips[j].addr;
2104 trbt_insertarray32_callback(ip_tree,
2105 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
2106 add_ip_callback,
2107 node_ip);
2109 talloc_free(tmp_ips);
2112 /* traverse */
2113 count = 0;
2114 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
2116 len = offsetof(struct ctdb_all_public_ips, ips) +
2117 count*sizeof(struct ctdb_public_ip);
2118 tmp_ips = talloc_zero_size(tmp_ctx, len);
2119 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
2121 *ips = tmp_ips;
2123 return 0;
2127 static void ctdb_every_second(struct event_context *ev, struct timed_event *te, struct timeval t, void *p)
2129 struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
2131 event_add_timed(ctdb->ev, ctdb,
2132 timeval_current_ofs(1, 0),
2133 ctdb_every_second, ctdb);
2136 struct srvid_reply_handler_data {
2137 bool done;
2138 bool wait_for_all;
2139 uint32_t *nodes;
2140 const char *srvid_str;
2143 static void srvid_broadcast_reply_handler(struct ctdb_context *ctdb,
2144 uint64_t srvid,
2145 TDB_DATA data,
2146 void *private_data)
2148 struct srvid_reply_handler_data *d =
2149 (struct srvid_reply_handler_data *)private_data;
2150 int i;
2151 int32_t ret;
2153 if (data.dsize != sizeof(ret)) {
2154 DEBUG(DEBUG_ERR, (__location__ " Wrong reply size\n"));
2155 return;
2158 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
2159 ret = *(int32_t *)data.dptr;
2160 if (ret < 0) {
2161 DEBUG(DEBUG_ERR,
2162 ("%s failed with result %d\n", d->srvid_str, ret));
2163 return;
2166 if (!d->wait_for_all) {
2167 d->done = true;
2168 return;
2171 /* Wait for all replies */
2172 d->done = true;
2173 for (i = 0; i < talloc_array_length(d->nodes); i++) {
2174 if (d->nodes[i] == ret) {
2175 DEBUG(DEBUG_INFO,
2176 ("%s reply received from node %u\n",
2177 d->srvid_str, ret));
2178 d->nodes[i] = -1;
2180 if (d->nodes[i] != -1) {
2181 /* Found a node that hasn't yet replied */
2182 d->done = false;
2187 /* Broadcast the given SRVID to all connected nodes. Wait for 1 reply
2188 * or replies from all connected nodes. arg is the data argument to
2189 * pass in the srvid_request structure - pass 0 if this isn't needed.
2191 static int srvid_broadcast(struct ctdb_context *ctdb,
2192 uint64_t srvid, uint32_t *arg,
2193 const char *srvid_str, bool wait_for_all)
2195 int ret;
2196 TDB_DATA data;
2197 uint32_t pnn;
2198 uint64_t reply_srvid;
2199 struct srvid_request request;
2200 struct srvid_request_data request_data;
2201 struct srvid_reply_handler_data reply_data;
2202 struct timeval tv;
2204 ZERO_STRUCT(request);
2206 /* Time ticks to enable timeouts to be processed */
2207 event_add_timed(ctdb->ev, ctdb,
2208 timeval_current_ofs(1, 0),
2209 ctdb_every_second, ctdb);
2211 pnn = ctdb_get_pnn(ctdb);
2212 reply_srvid = getpid();
2214 if (arg == NULL) {
2215 request.pnn = pnn;
2216 request.srvid = reply_srvid;
2218 data.dptr = (uint8_t *)&request;
2219 data.dsize = sizeof(request);
2220 } else {
2221 request_data.pnn = pnn;
2222 request_data.srvid = reply_srvid;
2223 request_data.data = *arg;
2225 data.dptr = (uint8_t *)&request_data;
2226 data.dsize = sizeof(request_data);
2229 /* Register message port for reply from recovery master */
2230 ctdb_client_set_message_handler(ctdb, reply_srvid,
2231 srvid_broadcast_reply_handler,
2232 &reply_data);
2234 reply_data.wait_for_all = wait_for_all;
2235 reply_data.nodes = NULL;
2236 reply_data.srvid_str = srvid_str;
2238 again:
2239 reply_data.done = false;
2241 if (wait_for_all) {
2242 struct ctdb_node_map *nodemap;
2244 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
2245 CTDB_CURRENT_NODE, ctdb, &nodemap);
2246 if (ret != 0) {
2247 DEBUG(DEBUG_ERR,
2248 ("Unable to get nodemap from current node, try again\n"));
2249 sleep(1);
2250 goto again;
2253 if (reply_data.nodes != NULL) {
2254 talloc_free(reply_data.nodes);
2256 reply_data.nodes = list_of_connected_nodes(ctdb, nodemap,
2257 NULL, true);
2259 talloc_free(nodemap);
2262 /* Send to all connected nodes. Only recmaster replies */
2263 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED,
2264 srvid, data);
2265 if (ret != 0) {
2266 /* This can only happen if the socket is closed and
2267 * there's no way to recover from that, so don't try
2268 * again.
2270 DEBUG(DEBUG_ERR,
2271 ("Failed to send %s request to connected nodes\n",
2272 srvid_str));
2273 return -1;
2276 tv = timeval_current();
2277 /* This loop terminates the reply is received */
2278 while (timeval_elapsed(&tv) < 5.0 && !reply_data.done) {
2279 event_loop_once(ctdb->ev);
2282 if (!reply_data.done) {
2283 DEBUG(DEBUG_NOTICE,
2284 ("Still waiting for confirmation of %s\n", srvid_str));
2285 sleep(1);
2286 goto again;
2289 ctdb_client_remove_message_handler(ctdb, reply_srvid, &reply_data);
2291 talloc_free(reply_data.nodes);
2293 return 0;
2296 static int ipreallocate(struct ctdb_context *ctdb)
2298 return srvid_broadcast(ctdb, CTDB_SRVID_TAKEOVER_RUN, NULL,
2299 "IP reallocation", false);
2303 static int control_ipreallocate(struct ctdb_context *ctdb, int argc, const char **argv)
2305 return ipreallocate(ctdb);
2309 add a public ip address to a node
2311 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
2313 int i, ret;
2314 int len, retries = 0;
2315 unsigned mask;
2316 ctdb_sock_addr addr;
2317 struct ctdb_control_ip_iface *pub;
2318 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2319 struct ctdb_all_public_ips *ips;
2322 if (argc != 2) {
2323 talloc_free(tmp_ctx);
2324 usage();
2327 if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
2328 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
2329 talloc_free(tmp_ctx);
2330 return -1;
2333 /* read the public ip list from the node */
2334 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2335 if (ret != 0) {
2336 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", options.pnn));
2337 talloc_free(tmp_ctx);
2338 return -1;
2340 for (i=0;i<ips->num;i++) {
2341 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2342 DEBUG(DEBUG_ERR,("Can not add ip to node. Node already hosts this ip\n"));
2343 return 0;
2349 /* Dont timeout. This command waits for an ip reallocation
2350 which sometimes can take wuite a while if there has
2351 been a recent recovery
2353 alarm(0);
2355 len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
2356 pub = talloc_size(tmp_ctx, len);
2357 CTDB_NO_MEMORY(ctdb, pub);
2359 pub->addr = addr;
2360 pub->mask = mask;
2361 pub->len = strlen(argv[1])+1;
2362 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
2364 do {
2365 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
2366 if (ret != 0) {
2367 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options.pnn));
2368 sleep(3);
2369 retries++;
2371 } while (retries < 5 && ret != 0);
2372 if (ret != 0) {
2373 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Giving up.\n", options.pnn));
2374 talloc_free(tmp_ctx);
2375 return ret;
2378 if (rebalance_node(ctdb, options.pnn) != 0) {
2379 DEBUG(DEBUG_ERR,("Error when trying to rebalance node\n"));
2380 return ret;
2383 talloc_free(tmp_ctx);
2384 return 0;
2388 add a public ip address to a node
2390 static int control_ipiface(struct ctdb_context *ctdb, int argc, const char **argv)
2392 ctdb_sock_addr addr;
2394 if (argc != 1) {
2395 usage();
2398 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2399 printf("Badly formed ip : %s\n", argv[0]);
2400 return -1;
2403 printf("IP on interface %s\n", ctdb_sys_find_ifname(&addr));
2405 return 0;
2408 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
2410 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
2412 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2413 struct ctdb_node_map *nodemap=NULL;
2414 struct ctdb_all_public_ips *ips;
2415 int ret, i, j;
2417 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2418 if (ret != 0) {
2419 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
2420 return ret;
2423 /* remove it from the nodes that are not hosting the ip currently */
2424 for(i=0;i<nodemap->num;i++){
2425 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2426 continue;
2428 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2429 if (ret != 0) {
2430 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2431 continue;
2434 for (j=0;j<ips->num;j++) {
2435 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2436 break;
2439 if (j==ips->num) {
2440 continue;
2443 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
2444 continue;
2447 options.pnn = nodemap->nodes[i].pnn;
2448 control_delip(ctdb, argc, argv);
2452 /* remove it from every node (also the one hosting it) */
2453 for(i=0;i<nodemap->num;i++){
2454 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2455 continue;
2457 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2458 if (ret != 0) {
2459 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2460 continue;
2463 for (j=0;j<ips->num;j++) {
2464 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2465 break;
2468 if (j==ips->num) {
2469 continue;
2472 options.pnn = nodemap->nodes[i].pnn;
2473 control_delip(ctdb, argc, argv);
2476 talloc_free(tmp_ctx);
2477 return 0;
2481 delete a public ip address from a node
2483 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
2485 int i, ret;
2486 ctdb_sock_addr addr;
2487 struct ctdb_control_ip_iface pub;
2488 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2489 struct ctdb_all_public_ips *ips;
2491 if (argc != 1) {
2492 talloc_free(tmp_ctx);
2493 usage();
2496 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2497 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2498 return -1;
2501 if (options.pnn == CTDB_BROADCAST_ALL) {
2502 return control_delip_all(ctdb, argc, argv, &addr);
2505 pub.addr = addr;
2506 pub.mask = 0;
2507 pub.len = 0;
2509 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2510 if (ret != 0) {
2511 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
2512 talloc_free(tmp_ctx);
2513 return ret;
2516 for (i=0;i<ips->num;i++) {
2517 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2518 break;
2522 if (i==ips->num) {
2523 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
2524 ctdb_addr_to_str(&addr)));
2525 talloc_free(tmp_ctx);
2526 return -1;
2529 /* This is an optimisation. If this node is hosting the IP
2530 * then try to move it somewhere else without invoking a full
2531 * takeover run. We don't care if this doesn't work!
2533 if (ips->ips[i].pnn == options.pnn) {
2534 (void) try_moveip(ctdb, &addr, -1);
2537 ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
2538 if (ret != 0) {
2539 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
2540 talloc_free(tmp_ctx);
2541 return ret;
2544 talloc_free(tmp_ctx);
2545 return 0;
2548 static int kill_tcp_from_file(struct ctdb_context *ctdb,
2549 int argc, const char **argv)
2551 struct ctdb_control_killtcp *killtcp;
2552 int max_entries, current, i;
2553 struct timeval timeout;
2554 char line[128], src[128], dst[128];
2555 int linenum;
2556 TDB_DATA data;
2557 struct client_async_data *async_data;
2558 struct ctdb_client_control_state *state;
2560 if (argc != 0) {
2561 usage();
2564 linenum = 1;
2565 killtcp = NULL;
2566 max_entries = 0;
2567 current = 0;
2568 while (!feof(stdin)) {
2569 if (fgets(line, sizeof(line), stdin) == NULL) {
2570 continue;
2573 /* Silently skip empty lines */
2574 if (line[0] == '\n') {
2575 continue;
2578 if (sscanf(line, "%s %s\n", src, dst) != 2) {
2579 DEBUG(DEBUG_ERR, ("Bad line [%d]: '%s'\n",
2580 linenum, line));
2581 talloc_free(killtcp);
2582 return -1;
2585 if (current >= max_entries) {
2586 max_entries += 1024;
2587 killtcp = talloc_realloc(ctdb, killtcp,
2588 struct ctdb_control_killtcp,
2589 max_entries);
2590 CTDB_NO_MEMORY(ctdb, killtcp);
2593 if (!parse_ip_port(src, &killtcp[current].src_addr)) {
2594 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2595 linenum, src));
2596 talloc_free(killtcp);
2597 return -1;
2600 if (!parse_ip_port(dst, &killtcp[current].dst_addr)) {
2601 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2602 linenum, dst));
2603 talloc_free(killtcp);
2604 return -1;
2607 current++;
2610 async_data = talloc_zero(ctdb, struct client_async_data);
2611 if (async_data == NULL) {
2612 talloc_free(killtcp);
2613 return -1;
2616 for (i = 0; i < current; i++) {
2618 data.dsize = sizeof(struct ctdb_control_killtcp);
2619 data.dptr = (unsigned char *)&killtcp[i];
2621 timeout = TIMELIMIT();
2622 state = ctdb_control_send(ctdb, options.pnn, 0,
2623 CTDB_CONTROL_KILL_TCP, 0, data,
2624 async_data, &timeout, NULL);
2626 if (state == NULL) {
2627 DEBUG(DEBUG_ERR,
2628 ("Failed to call async killtcp control to node %u\n",
2629 options.pnn));
2630 talloc_free(killtcp);
2631 return -1;
2634 ctdb_client_async_add(async_data, state);
2637 if (ctdb_client_async_wait(ctdb, async_data) != 0) {
2638 DEBUG(DEBUG_ERR,("killtcp failed\n"));
2639 talloc_free(killtcp);
2640 return -1;
2643 talloc_free(killtcp);
2644 return 0;
2649 kill a tcp connection
2651 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2653 int ret;
2654 struct ctdb_control_killtcp killtcp;
2656 assert_single_node_only();
2658 if (argc == 0) {
2659 return kill_tcp_from_file(ctdb, argc, argv);
2662 if (argc < 2) {
2663 usage();
2666 if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
2667 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2668 return -1;
2671 if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
2672 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2673 return -1;
2676 ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
2677 if (ret != 0) {
2678 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
2679 return ret;
2682 return 0;
2687 send a gratious arp
2689 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
2691 int ret;
2692 ctdb_sock_addr addr;
2694 assert_single_node_only();
2696 if (argc < 2) {
2697 usage();
2700 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2701 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
2702 return -1;
2705 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
2706 if (ret != 0) {
2707 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
2708 return ret;
2711 return 0;
2715 register a server id
2717 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2719 int ret;
2720 struct ctdb_server_id server_id;
2722 if (argc < 3) {
2723 usage();
2726 server_id.pnn = strtoul(argv[0], NULL, 0);
2727 server_id.type = strtoul(argv[1], NULL, 0);
2728 server_id.server_id = strtoul(argv[2], NULL, 0);
2730 ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
2731 if (ret != 0) {
2732 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
2733 return ret;
2735 DEBUG(DEBUG_ERR,("Srvid registered. Sleeping for 999 seconds\n"));
2736 sleep(999);
2737 return -1;
2741 unregister a server id
2743 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2745 int ret;
2746 struct ctdb_server_id server_id;
2748 if (argc < 3) {
2749 usage();
2752 server_id.pnn = strtoul(argv[0], NULL, 0);
2753 server_id.type = strtoul(argv[1], NULL, 0);
2754 server_id.server_id = strtoul(argv[2], NULL, 0);
2756 ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
2757 if (ret != 0) {
2758 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
2759 return ret;
2761 return -1;
2765 check if a server id exists
2767 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2769 uint32_t status;
2770 int ret;
2771 struct ctdb_server_id server_id;
2773 if (argc < 3) {
2774 usage();
2777 server_id.pnn = strtoul(argv[0], NULL, 0);
2778 server_id.type = strtoul(argv[1], NULL, 0);
2779 server_id.server_id = strtoul(argv[2], NULL, 0);
2781 ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
2782 if (ret != 0) {
2783 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
2784 return ret;
2787 if (status) {
2788 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
2789 } else {
2790 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
2792 return 0;
2796 get a list of all server ids that are registered on a node
2798 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
2800 int i, ret;
2801 struct ctdb_server_id_list *server_ids;
2803 ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
2804 if (ret != 0) {
2805 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
2806 return ret;
2809 for (i=0; i<server_ids->num; i++) {
2810 printf("Server id %d:%d:%d\n",
2811 server_ids->server_ids[i].pnn,
2812 server_ids->server_ids[i].type,
2813 server_ids->server_ids[i].server_id);
2816 return -1;
2820 check if a server id exists
2822 static int check_srvids(struct ctdb_context *ctdb, int argc, const char **argv)
2824 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
2825 uint64_t *ids;
2826 uint8_t *result;
2827 int i;
2829 if (argc < 1) {
2830 talloc_free(tmp_ctx);
2831 usage();
2834 ids = talloc_array(tmp_ctx, uint64_t, argc);
2835 result = talloc_array(tmp_ctx, uint8_t, argc);
2837 for (i = 0; i < argc; i++) {
2838 ids[i] = strtoull(argv[i], NULL, 0);
2841 if (!ctdb_client_check_message_handlers(ctdb, ids, argc, result)) {
2842 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n",
2843 options.pnn));
2844 talloc_free(tmp_ctx);
2845 return -1;
2848 for (i=0; i < argc; i++) {
2849 printf("Server id %d:%llu %s\n", options.pnn, (long long)ids[i],
2850 result[i] ? "exists" : "does not exist");
2853 talloc_free(tmp_ctx);
2854 return 0;
2858 send a tcp tickle ack
2860 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2862 int ret;
2863 ctdb_sock_addr src, dst;
2865 if (argc < 2) {
2866 usage();
2869 if (!parse_ip_port(argv[0], &src)) {
2870 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2871 return -1;
2874 if (!parse_ip_port(argv[1], &dst)) {
2875 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2876 return -1;
2879 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
2880 if (ret==0) {
2881 return 0;
2883 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
2885 return -1;
2890 display public ip status
2892 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
2894 int i, ret;
2895 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2896 struct ctdb_all_public_ips *ips;
2898 if (options.pnn == CTDB_BROADCAST_ALL) {
2899 /* read the list of public ips from all nodes */
2900 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
2901 } else {
2902 /* read the public ip list from this node */
2903 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2905 if (ret != 0) {
2906 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
2907 talloc_free(tmp_ctx);
2908 return ret;
2911 if (options.machinereadable){
2912 printf(":Public IP:Node:");
2913 if (options.verbose){
2914 printf("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2916 printf("\n");
2917 } else {
2918 if (options.pnn == CTDB_BROADCAST_ALL) {
2919 printf("Public IPs on ALL nodes\n");
2920 } else {
2921 printf("Public IPs on node %u\n", options.pnn);
2925 for (i=1;i<=ips->num;i++) {
2926 struct ctdb_control_public_ip_info *info = NULL;
2927 int32_t pnn;
2928 char *aciface = NULL;
2929 char *avifaces = NULL;
2930 char *cifaces = NULL;
2932 if (options.pnn == CTDB_BROADCAST_ALL) {
2933 pnn = ips->ips[ips->num-i].pnn;
2934 } else {
2935 pnn = options.pnn;
2938 if (pnn != -1) {
2939 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
2940 &ips->ips[ips->num-i].addr, &info);
2941 } else {
2942 ret = -1;
2945 if (ret == 0) {
2946 int j;
2947 for (j=0; j < info->num; j++) {
2948 if (cifaces == NULL) {
2949 cifaces = talloc_strdup(info,
2950 info->ifaces[j].name);
2951 } else {
2952 cifaces = talloc_asprintf_append(cifaces,
2953 ",%s",
2954 info->ifaces[j].name);
2957 if (info->active_idx == j) {
2958 aciface = info->ifaces[j].name;
2961 if (info->ifaces[j].link_state == 0) {
2962 continue;
2965 if (avifaces == NULL) {
2966 avifaces = talloc_strdup(info, info->ifaces[j].name);
2967 } else {
2968 avifaces = talloc_asprintf_append(avifaces,
2969 ",%s",
2970 info->ifaces[j].name);
2975 if (options.machinereadable){
2976 printf(":%s:%d:",
2977 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2978 ips->ips[ips->num-i].pnn);
2979 if (options.verbose){
2980 printf("%s:%s:%s:",
2981 aciface?aciface:"",
2982 avifaces?avifaces:"",
2983 cifaces?cifaces:"");
2985 printf("\n");
2986 } else {
2987 if (options.verbose) {
2988 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
2989 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2990 ips->ips[ips->num-i].pnn,
2991 aciface?aciface:"",
2992 avifaces?avifaces:"",
2993 cifaces?cifaces:"");
2994 } else {
2995 printf("%s %d\n",
2996 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2997 ips->ips[ips->num-i].pnn);
3000 talloc_free(info);
3003 talloc_free(tmp_ctx);
3004 return 0;
3008 public ip info
3010 static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
3012 int i, ret;
3013 ctdb_sock_addr addr;
3014 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3015 struct ctdb_control_public_ip_info *info;
3017 if (argc != 1) {
3018 talloc_free(tmp_ctx);
3019 usage();
3022 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
3023 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
3024 return -1;
3027 /* read the public ip info from this node */
3028 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), options.pnn,
3029 tmp_ctx, &addr, &info);
3030 if (ret != 0) {
3031 DEBUG(DEBUG_ERR, ("Unable to get public ip[%s]info from node %u\n",
3032 argv[0], options.pnn));
3033 talloc_free(tmp_ctx);
3034 return ret;
3037 printf("Public IP[%s] info on node %u\n",
3038 ctdb_addr_to_str(&info->ip.addr),
3039 options.pnn);
3041 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
3042 ctdb_addr_to_str(&info->ip.addr),
3043 info->ip.pnn, info->num);
3045 for (i=0; i<info->num; i++) {
3046 info->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
3048 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
3049 i+1, info->ifaces[i].name,
3050 info->ifaces[i].link_state?"up":"down",
3051 (unsigned int)info->ifaces[i].references,
3052 (i==info->active_idx)?" (active)":"");
3055 talloc_free(tmp_ctx);
3056 return 0;
3060 display interfaces status
3062 static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
3064 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3065 int i;
3066 struct ctdb_control_get_ifaces *ifaces;
3067 int ret;
3069 /* read the public ip list from this node */
3070 ret = ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ifaces);
3071 if (ret != 0) {
3072 DEBUG(DEBUG_ERR, ("Unable to get interfaces from node %u\n",
3073 options.pnn));
3074 talloc_free(tmp_ctx);
3075 return -1;
3078 if (options.machinereadable){
3079 printf(":Name:LinkStatus:References:\n");
3080 } else {
3081 printf("Interfaces on node %u\n", options.pnn);
3084 for (i=0; i<ifaces->num; i++) {
3085 if (options.machinereadable){
3086 printf(":%s:%s:%u\n",
3087 ifaces->ifaces[i].name,
3088 ifaces->ifaces[i].link_state?"1":"0",
3089 (unsigned int)ifaces->ifaces[i].references);
3090 } else {
3091 printf("name:%s link:%s references:%u\n",
3092 ifaces->ifaces[i].name,
3093 ifaces->ifaces[i].link_state?"up":"down",
3094 (unsigned int)ifaces->ifaces[i].references);
3098 talloc_free(tmp_ctx);
3099 return 0;
3104 set link status of an interface
3106 static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
3108 int ret;
3109 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3110 struct ctdb_control_iface_info info;
3112 ZERO_STRUCT(info);
3114 if (argc != 2) {
3115 usage();
3118 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
3119 DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
3120 argv[0]));
3121 talloc_free(tmp_ctx);
3122 return -1;
3124 strcpy(info.name, argv[0]);
3126 if (strcmp(argv[1], "up") == 0) {
3127 info.link_state = 1;
3128 } else if (strcmp(argv[1], "down") == 0) {
3129 info.link_state = 0;
3130 } else {
3131 DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
3132 argv[1]));
3133 talloc_free(tmp_ctx);
3134 return -1;
3137 /* read the public ip list from this node */
3138 ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
3139 tmp_ctx, &info);
3140 if (ret != 0) {
3141 DEBUG(DEBUG_ERR, ("Unable to set link state for interfaces %s node %u\n",
3142 argv[0], options.pnn));
3143 talloc_free(tmp_ctx);
3144 return ret;
3147 talloc_free(tmp_ctx);
3148 return 0;
3152 display pid of a ctdb daemon
3154 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
3156 uint32_t pid;
3157 int ret;
3159 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
3160 if (ret != 0) {
3161 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
3162 return ret;
3164 printf("Pid:%d\n", pid);
3166 return 0;
3169 typedef bool update_flags_handler_t(struct ctdb_context *ctdb, void *data);
3171 static int update_flags_and_ipreallocate(struct ctdb_context *ctdb,
3172 void *data,
3173 update_flags_handler_t handler,
3174 uint32_t flag,
3175 const char *desc,
3176 bool set_flag)
3178 struct ctdb_node_map *nodemap = NULL;
3179 bool flag_is_set;
3180 int ret;
3182 /* Check if the node is already in the desired state */
3183 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3184 if (ret != 0) {
3185 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3186 exit(10);
3188 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3189 if (set_flag == flag_is_set) {
3190 DEBUG(DEBUG_NOTICE, ("Node %d is %s %s\n", options.pnn,
3191 (set_flag ? "already" : "not"), desc));
3192 return 0;
3195 do {
3196 if (!handler(ctdb, data)) {
3197 DEBUG(DEBUG_WARNING,
3198 ("Failed to send control to set state %s on node %u, try again\n",
3199 desc, options.pnn));
3202 sleep(1);
3204 /* Read the nodemap and verify the change took effect.
3205 * Even if the above control/hanlder timed out then it
3206 * could still have worked!
3208 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
3209 ctdb, &nodemap);
3210 if (ret != 0) {
3211 DEBUG(DEBUG_WARNING,
3212 ("Unable to get nodemap from local node, try again\n"));
3214 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3215 } while (nodemap == NULL || (set_flag != flag_is_set));
3217 return ipreallocate(ctdb);
3220 /* Administratively disable a node */
3221 static bool update_flags_disabled(struct ctdb_context *ctdb, void *data)
3223 int ret;
3225 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3226 NODE_FLAGS_PERMANENTLY_DISABLED, 0);
3227 return ret == 0;
3230 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
3232 return update_flags_and_ipreallocate(ctdb, NULL,
3233 update_flags_disabled,
3234 NODE_FLAGS_PERMANENTLY_DISABLED,
3235 "disabled",
3236 true /* set_flag*/);
3239 /* Administratively re-enable a node */
3240 static bool update_flags_not_disabled(struct ctdb_context *ctdb, void *data)
3242 int ret;
3244 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3245 0, NODE_FLAGS_PERMANENTLY_DISABLED);
3246 return ret == 0;
3249 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
3251 return update_flags_and_ipreallocate(ctdb, NULL,
3252 update_flags_not_disabled,
3253 NODE_FLAGS_PERMANENTLY_DISABLED,
3254 "disabled",
3255 false /* set_flag*/);
3258 /* Stop a node */
3259 static bool update_flags_stopped(struct ctdb_context *ctdb, void *data)
3261 int ret;
3263 ret = ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn);
3265 return ret == 0;
3268 static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
3270 return update_flags_and_ipreallocate(ctdb, NULL,
3271 update_flags_stopped,
3272 NODE_FLAGS_STOPPED,
3273 "stopped",
3274 true /* set_flag*/);
3277 /* Continue a stopped node */
3278 static bool update_flags_not_stopped(struct ctdb_context *ctdb, void *data)
3280 int ret;
3282 ret = ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn);
3284 return ret == 0;
3287 static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
3289 return update_flags_and_ipreallocate(ctdb, NULL,
3290 update_flags_not_stopped,
3291 NODE_FLAGS_STOPPED,
3292 "stopped",
3293 false /* set_flag */);
3296 static uint32_t get_generation(struct ctdb_context *ctdb)
3298 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3299 struct ctdb_vnn_map *vnnmap=NULL;
3300 int ret;
3301 uint32_t generation;
3303 /* wait until the recmaster is not in recovery mode */
3304 while (1) {
3305 uint32_t recmode, recmaster;
3307 if (vnnmap != NULL) {
3308 talloc_free(vnnmap);
3309 vnnmap = NULL;
3312 /* get the recmaster */
3313 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), CTDB_CURRENT_NODE, &recmaster);
3314 if (ret != 0) {
3315 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
3316 talloc_free(tmp_ctx);
3317 exit(10);
3320 /* get recovery mode */
3321 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), recmaster, &recmode);
3322 if (ret != 0) {
3323 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
3324 talloc_free(tmp_ctx);
3325 exit(10);
3328 /* get the current generation number */
3329 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, tmp_ctx, &vnnmap);
3330 if (ret != 0) {
3331 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
3332 talloc_free(tmp_ctx);
3333 exit(10);
3336 if ((recmode == CTDB_RECOVERY_NORMAL) && (vnnmap->generation != 1)) {
3337 generation = vnnmap->generation;
3338 talloc_free(tmp_ctx);
3339 return generation;
3341 sleep(1);
3345 /* Ban a node */
3346 static bool update_state_banned(struct ctdb_context *ctdb, void *data)
3348 struct ctdb_ban_time *bantime = (struct ctdb_ban_time *)data;
3349 int ret;
3351 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, bantime);
3353 return ret == 0;
3356 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
3358 struct ctdb_ban_time bantime;
3360 if (argc < 1) {
3361 usage();
3364 bantime.pnn = options.pnn;
3365 bantime.time = strtoul(argv[0], NULL, 0);
3367 if (bantime.time == 0) {
3368 DEBUG(DEBUG_ERR, ("Invalid ban time specified - must be >0\n"));
3369 return -1;
3372 return update_flags_and_ipreallocate(ctdb, &bantime,
3373 update_state_banned,
3374 NODE_FLAGS_BANNED,
3375 "banned",
3376 true /* set_flag*/);
3380 /* Unban a node */
3381 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
3383 struct ctdb_ban_time bantime;
3385 bantime.pnn = options.pnn;
3386 bantime.time = 0;
3388 return update_flags_and_ipreallocate(ctdb, &bantime,
3389 update_state_banned,
3390 NODE_FLAGS_BANNED,
3391 "banned",
3392 false /* set_flag*/);
3396 show ban information for a node
3398 static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
3400 int ret;
3401 struct ctdb_node_map *nodemap=NULL;
3402 struct ctdb_ban_time *bantime;
3404 /* verify the node exists */
3405 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3406 if (ret != 0) {
3407 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3408 return ret;
3411 ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
3412 if (ret != 0) {
3413 DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
3414 return -1;
3417 if (bantime->time == 0) {
3418 printf("Node %u is not banned\n", bantime->pnn);
3419 } else {
3420 printf("Node %u is banned, %d seconds remaining\n",
3421 bantime->pnn, bantime->time);
3424 return 0;
3428 shutdown a daemon
3430 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
3432 int ret;
3434 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
3435 if (ret != 0) {
3436 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
3437 return ret;
3440 return 0;
3444 trigger a recovery
3446 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
3448 int ret;
3449 uint32_t generation, next_generation;
3450 bool force;
3452 /* "force" option ignores freeze failure and forces recovery */
3453 force = (argc == 1) && (strcasecmp(argv[0], "force") == 0);
3455 /* record the current generation number */
3456 generation = get_generation(ctdb);
3458 ret = ctdb_ctrl_freeze_priority(ctdb, TIMELIMIT(), options.pnn, 1);
3459 if (ret != 0) {
3460 if (!force) {
3461 DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
3462 return ret;
3464 DEBUG(DEBUG_WARNING, ("Unable to freeze node but proceeding because \"force\" option given\n"));
3467 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
3468 if (ret != 0) {
3469 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
3470 return ret;
3473 /* wait until we are in a new generation */
3474 while (1) {
3475 next_generation = get_generation(ctdb);
3476 if (next_generation != generation) {
3477 return 0;
3479 sleep(1);
3482 return 0;
3487 display monitoring mode of a remote node
3489 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
3491 uint32_t monmode;
3492 int ret;
3494 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
3495 if (ret != 0) {
3496 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
3497 return ret;
3499 if (!options.machinereadable){
3500 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
3501 } else {
3502 printf(":mode:\n");
3503 printf(":%d:\n",monmode);
3505 return 0;
3510 display capabilities of a remote node
3512 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
3514 uint32_t capabilities;
3515 int ret;
3517 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
3518 if (ret != 0) {
3519 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
3520 return -1;
3523 if (!options.machinereadable){
3524 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
3525 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
3526 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
3527 printf("NATGW: %s\n", (capabilities&CTDB_CAP_NATGW)?"YES":"NO");
3528 } else {
3529 printf(":RECMASTER:LMASTER:LVS:NATGW:\n");
3530 printf(":%d:%d:%d:%d:\n",
3531 !!(capabilities&CTDB_CAP_RECMASTER),
3532 !!(capabilities&CTDB_CAP_LMASTER),
3533 !!(capabilities&CTDB_CAP_LVS),
3534 !!(capabilities&CTDB_CAP_NATGW));
3536 return 0;
3540 display lvs configuration
3542 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
3544 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3545 struct ctdb_node_map *orig_nodemap=NULL;
3546 struct ctdb_node_map *nodemap;
3547 int i, ret;
3548 int healthy_count = 0;
3550 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn,
3551 tmp_ctx, &orig_nodemap);
3552 if (ret != 0) {
3553 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3554 talloc_free(tmp_ctx);
3555 return -1;
3558 nodemap = filter_nodemap_by_capabilities(ctdb, orig_nodemap,
3559 CTDB_CAP_LVS);
3560 if (nodemap == NULL) {
3561 /* No memory */
3562 ret = -1;
3563 goto done;
3566 ret = 0;
3568 /* collect capabilities for all connected nodes */
3569 for (i=0; i<nodemap->num; i++) {
3570 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3571 continue;
3573 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3574 continue;
3577 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
3578 healthy_count++;
3582 /* Print all LVS nodes */
3583 for (i=0; i<nodemap->num; i++) {
3584 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3585 continue;
3587 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3588 continue;
3591 if (healthy_count != 0) {
3592 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
3593 continue;
3597 printf("%d:%s\n", nodemap->nodes[i].pnn,
3598 ctdb_addr_to_str(&nodemap->nodes[i].addr));
3601 done:
3602 talloc_free(tmp_ctx);
3603 return ret;
3607 display who is the lvs master
3609 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
3611 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3612 struct ctdb_node_map *orig_nodemap=NULL;
3613 struct ctdb_node_map *nodemap;
3614 int i, ret;
3615 int healthy_count = 0;
3617 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn,
3618 tmp_ctx, &orig_nodemap);
3619 if (ret != 0) {
3620 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3621 talloc_free(tmp_ctx);
3622 return -1;
3625 nodemap = filter_nodemap_by_capabilities(ctdb, orig_nodemap,
3626 CTDB_CAP_LVS);
3627 if (nodemap == NULL) {
3628 /* No memory */
3629 ret = -1;
3630 goto done;
3633 /* collect capabilities for all connected nodes */
3634 for (i=0; i<nodemap->num; i++) {
3635 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3636 continue;
3638 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3639 continue;
3642 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
3643 healthy_count++;
3647 ret = -1;
3649 /* find and show the lvsmaster */
3650 for (i=0; i<nodemap->num; i++) {
3651 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3652 continue;
3654 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3655 continue;
3657 if (healthy_count != 0) {
3658 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
3659 continue;
3663 if (options.machinereadable){
3664 printf("%d\n", nodemap->nodes[i].pnn);
3665 } else {
3666 printf("Node %d is LVS master\n",
3667 nodemap->nodes[i].pnn);
3669 ret = 0;
3670 goto done;
3673 printf("There is no LVS master\n");
3674 done:
3675 talloc_free(tmp_ctx);
3676 return ret;
3680 disable monitoring on a node
3682 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3685 int ret;
3687 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
3688 if (ret != 0) {
3689 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
3690 return ret;
3692 printf("Monitoring mode:%s\n","DISABLED");
3694 return 0;
3698 enable monitoring on a node
3700 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3703 int ret;
3705 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
3706 if (ret != 0) {
3707 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
3708 return ret;
3710 printf("Monitoring mode:%s\n","ACTIVE");
3712 return 0;
3716 display remote list of keys/data for a db
3718 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
3720 const char *db_name;
3721 struct ctdb_db_context *ctdb_db;
3722 int ret;
3723 struct ctdb_dump_db_context c;
3724 uint8_t flags;
3726 if (argc < 1) {
3727 usage();
3730 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3731 return -1;
3734 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3735 if (ctdb_db == NULL) {
3736 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3737 return -1;
3740 if (options.printlmaster) {
3741 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn,
3742 ctdb, &ctdb->vnn_map);
3743 if (ret != 0) {
3744 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
3745 options.pnn));
3746 return ret;
3750 ZERO_STRUCT(c);
3751 c.f = stdout;
3752 c.printemptyrecords = (bool)options.printemptyrecords;
3753 c.printdatasize = (bool)options.printdatasize;
3754 c.printlmaster = (bool)options.printlmaster;
3755 c.printhash = (bool)options.printhash;
3756 c.printrecordflags = (bool)options.printrecordflags;
3758 /* traverse and dump the cluster tdb */
3759 ret = ctdb_dump_db(ctdb_db, &c);
3760 if (ret == -1) {
3761 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
3762 DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
3763 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3764 db_name));
3765 return -1;
3767 talloc_free(ctdb_db);
3769 printf("Dumped %d records\n", ret);
3770 return 0;
3773 struct cattdb_data {
3774 struct ctdb_context *ctdb;
3775 uint32_t count;
3778 static int cattdb_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
3780 struct cattdb_data *d = private_data;
3781 struct ctdb_dump_db_context c;
3783 d->count++;
3785 ZERO_STRUCT(c);
3786 c.f = stdout;
3787 c.printemptyrecords = (bool)options.printemptyrecords;
3788 c.printdatasize = (bool)options.printdatasize;
3789 c.printlmaster = false;
3790 c.printhash = (bool)options.printhash;
3791 c.printrecordflags = true;
3793 return ctdb_dumpdb_record(d->ctdb, key, data, &c);
3797 cat the local tdb database using same format as catdb
3799 static int control_cattdb(struct ctdb_context *ctdb, int argc, const char **argv)
3801 const char *db_name;
3802 struct ctdb_db_context *ctdb_db;
3803 struct cattdb_data d;
3804 uint8_t flags;
3806 if (argc < 1) {
3807 usage();
3810 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3811 return -1;
3814 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3815 if (ctdb_db == NULL) {
3816 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3817 return -1;
3820 /* traverse the local tdb */
3821 d.count = 0;
3822 d.ctdb = ctdb;
3823 if (tdb_traverse_read(ctdb_db->ltdb->tdb, cattdb_traverse, &d) == -1) {
3824 printf("Failed to cattdb data\n");
3825 exit(10);
3827 talloc_free(ctdb_db);
3829 printf("Dumped %d records\n", d.count);
3830 return 0;
3834 display the content of a database key
3836 static int control_readkey(struct ctdb_context *ctdb, int argc, const char **argv)
3838 const char *db_name;
3839 struct ctdb_db_context *ctdb_db;
3840 struct ctdb_record_handle *h;
3841 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3842 TDB_DATA key, data;
3843 uint8_t flags;
3845 if (argc < 2) {
3846 usage();
3849 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3850 return -1;
3853 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3854 if (ctdb_db == NULL) {
3855 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3856 return -1;
3859 key.dptr = discard_const(argv[1]);
3860 key.dsize = strlen((char *)key.dptr);
3862 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3863 if (h == NULL) {
3864 printf("Failed to fetch record '%s' on node %d\n",
3865 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3866 talloc_free(tmp_ctx);
3867 exit(10);
3870 printf("Data: size:%d ptr:[%.*s]\n", (int)data.dsize, (int)data.dsize, data.dptr);
3872 talloc_free(ctdb_db);
3873 talloc_free(tmp_ctx);
3874 return 0;
3878 display the content of a database key
3880 static int control_writekey(struct ctdb_context *ctdb, int argc, const char **argv)
3882 const char *db_name;
3883 struct ctdb_db_context *ctdb_db;
3884 struct ctdb_record_handle *h;
3885 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3886 TDB_DATA key, data;
3887 uint8_t flags;
3889 if (argc < 3) {
3890 usage();
3893 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3894 return -1;
3897 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3898 if (ctdb_db == NULL) {
3899 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3900 return -1;
3903 key.dptr = discard_const(argv[1]);
3904 key.dsize = strlen((char *)key.dptr);
3906 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3907 if (h == NULL) {
3908 printf("Failed to fetch record '%s' on node %d\n",
3909 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3910 talloc_free(tmp_ctx);
3911 exit(10);
3914 data.dptr = discard_const(argv[2]);
3915 data.dsize = strlen((char *)data.dptr);
3917 if (ctdb_record_store(h, data) != 0) {
3918 printf("Failed to store record\n");
3921 talloc_free(h);
3922 talloc_free(ctdb_db);
3923 talloc_free(tmp_ctx);
3924 return 0;
3928 fetch a record from a persistent database
3930 static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3932 const char *db_name;
3933 struct ctdb_db_context *ctdb_db;
3934 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3935 struct ctdb_transaction_handle *h;
3936 TDB_DATA key, data;
3937 int fd, ret;
3938 bool persistent;
3939 uint8_t flags;
3941 if (argc < 2) {
3942 talloc_free(tmp_ctx);
3943 usage();
3946 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3947 talloc_free(tmp_ctx);
3948 return -1;
3951 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
3952 if (!persistent) {
3953 DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", db_name));
3954 talloc_free(tmp_ctx);
3955 return -1;
3958 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3959 if (ctdb_db == NULL) {
3960 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3961 talloc_free(tmp_ctx);
3962 return -1;
3965 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3966 if (h == NULL) {
3967 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3968 talloc_free(tmp_ctx);
3969 return -1;
3972 key.dptr = discard_const(argv[1]);
3973 key.dsize = strlen(argv[1]);
3974 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
3975 if (ret != 0) {
3976 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
3977 talloc_free(tmp_ctx);
3978 return -1;
3981 if (data.dsize == 0 || data.dptr == NULL) {
3982 DEBUG(DEBUG_ERR,("Record is empty\n"));
3983 talloc_free(tmp_ctx);
3984 return -1;
3987 if (argc == 3) {
3988 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3989 if (fd == -1) {
3990 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3991 talloc_free(tmp_ctx);
3992 return -1;
3994 write(fd, data.dptr, data.dsize);
3995 close(fd);
3996 } else {
3997 write(1, data.dptr, data.dsize);
4000 /* abort the transaction */
4001 talloc_free(h);
4004 talloc_free(tmp_ctx);
4005 return 0;
4009 fetch a record from a tdb-file
4011 static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv)
4013 const char *tdb_file;
4014 TDB_CONTEXT *tdb;
4015 TDB_DATA key, data;
4016 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
4017 int fd;
4019 if (argc < 2) {
4020 usage();
4023 tdb_file = argv[0];
4025 tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
4026 if (tdb == NULL) {
4027 printf("Failed to open TDB file %s\n", tdb_file);
4028 return -1;
4031 if (!strncmp(argv[1], "0x", 2)) {
4032 key = hextodata(tmp_ctx, argv[1] + 2);
4033 if (key.dsize == 0) {
4034 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4035 return -1;
4037 } else {
4038 key.dptr = discard_const(argv[1]);
4039 key.dsize = strlen(argv[1]);
4042 data = tdb_fetch(tdb, key);
4043 if (data.dptr == NULL || data.dsize < sizeof(struct ctdb_ltdb_header)) {
4044 printf("Failed to read record %s from tdb %s\n", argv[1], tdb_file);
4045 tdb_close(tdb);
4046 return -1;
4049 tdb_close(tdb);
4051 if (argc == 3) {
4052 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
4053 if (fd == -1) {
4054 printf("Failed to open output file %s\n", argv[2]);
4055 return -1;
4057 if (options.verbose){
4058 write(fd, data.dptr, data.dsize);
4059 } else {
4060 write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
4062 close(fd);
4063 } else {
4064 if (options.verbose){
4065 write(1, data.dptr, data.dsize);
4066 } else {
4067 write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
4071 talloc_free(tmp_ctx);
4072 return 0;
4076 store a record and header to a tdb-file
4078 static int control_tstore(struct ctdb_context *ctdb, int argc, const char **argv)
4080 const char *tdb_file;
4081 TDB_CONTEXT *tdb;
4082 TDB_DATA key, value, data;
4083 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
4084 struct ctdb_ltdb_header header;
4086 if (argc < 3) {
4087 usage();
4090 tdb_file = argv[0];
4092 tdb = tdb_open(tdb_file, 0, 0, O_RDWR, 0);
4093 if (tdb == NULL) {
4094 printf("Failed to open TDB file %s\n", tdb_file);
4095 return -1;
4098 if (!strncmp(argv[1], "0x", 2)) {
4099 key = hextodata(tmp_ctx, argv[1] + 2);
4100 if (key.dsize == 0) {
4101 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4102 return -1;
4104 } else {
4105 key.dptr = discard_const(argv[1]);
4106 key.dsize = strlen(argv[1]);
4109 if (!strncmp(argv[2], "0x", 2)) {
4110 value = hextodata(tmp_ctx, argv[2] + 2);
4111 if (value.dsize == 0) {
4112 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[2]);
4113 return -1;
4115 } else {
4116 value.dptr = discard_const(argv[2]);
4117 value.dsize = strlen(argv[2]);
4120 ZERO_STRUCT(header);
4121 if (argc > 3) {
4122 header.rsn = atoll(argv[3]);
4124 if (argc > 4) {
4125 header.dmaster = atoi(argv[4]);
4127 if (argc > 5) {
4128 header.flags = atoi(argv[5]);
4131 data.dsize = sizeof(struct ctdb_ltdb_header) + value.dsize;
4132 data.dptr = talloc_size(tmp_ctx, data.dsize);
4133 if (data.dptr == NULL) {
4134 printf("Failed to allocate header+value\n");
4135 return -1;
4138 *(struct ctdb_ltdb_header *)data.dptr = header;
4139 memcpy(data.dptr + sizeof(struct ctdb_ltdb_header), value.dptr, value.dsize);
4141 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0) {
4142 printf("Failed to write record %s to tdb %s\n", argv[1], tdb_file);
4143 tdb_close(tdb);
4144 return -1;
4147 tdb_close(tdb);
4149 talloc_free(tmp_ctx);
4150 return 0;
4154 write a record to a persistent database
4156 static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv)
4158 const char *db_name;
4159 struct ctdb_db_context *ctdb_db;
4160 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4161 struct ctdb_transaction_handle *h;
4162 struct stat st;
4163 TDB_DATA key, data;
4164 int fd, ret;
4166 if (argc < 3) {
4167 talloc_free(tmp_ctx);
4168 usage();
4171 fd = open(argv[2], O_RDONLY);
4172 if (fd == -1) {
4173 DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s %s\n", argv[2], strerror(errno)));
4174 talloc_free(tmp_ctx);
4175 return -1;
4178 ret = fstat(fd, &st);
4179 if (ret == -1) {
4180 DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
4181 close(fd);
4182 talloc_free(tmp_ctx);
4183 return -1;
4186 if (!S_ISREG(st.st_mode)) {
4187 DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
4188 close(fd);
4189 talloc_free(tmp_ctx);
4190 return -1;
4193 data.dsize = st.st_size;
4194 if (data.dsize == 0) {
4195 data.dptr = NULL;
4196 } else {
4197 data.dptr = talloc_size(tmp_ctx, data.dsize);
4198 if (data.dptr == NULL) {
4199 DEBUG(DEBUG_ERR,("Failed to talloc %d of memory to store record data\n", (int)data.dsize));
4200 close(fd);
4201 talloc_free(tmp_ctx);
4202 return -1;
4204 ret = read(fd, data.dptr, data.dsize);
4205 if (ret != data.dsize) {
4206 DEBUG(DEBUG_ERR,("Failed to read %d bytes of record data\n", (int)data.dsize));
4207 close(fd);
4208 talloc_free(tmp_ctx);
4209 return -1;
4212 close(fd);
4215 db_name = argv[0];
4217 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
4218 if (ctdb_db == NULL) {
4219 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4220 talloc_free(tmp_ctx);
4221 return -1;
4224 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4225 if (h == NULL) {
4226 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4227 talloc_free(tmp_ctx);
4228 return -1;
4231 key.dptr = discard_const(argv[1]);
4232 key.dsize = strlen(argv[1]);
4233 ret = ctdb_transaction_store(h, key, data);
4234 if (ret != 0) {
4235 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4236 talloc_free(tmp_ctx);
4237 return -1;
4240 ret = ctdb_transaction_commit(h);
4241 if (ret != 0) {
4242 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4243 talloc_free(tmp_ctx);
4244 return -1;
4248 talloc_free(tmp_ctx);
4249 return 0;
4253 * delete a record from a persistent database
4255 static int control_pdelete(struct ctdb_context *ctdb, int argc, const char **argv)
4257 const char *db_name;
4258 struct ctdb_db_context *ctdb_db;
4259 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4260 struct ctdb_transaction_handle *h;
4261 TDB_DATA key;
4262 int ret;
4263 bool persistent;
4264 uint8_t flags;
4266 if (argc < 2) {
4267 talloc_free(tmp_ctx);
4268 usage();
4271 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
4272 talloc_free(tmp_ctx);
4273 return -1;
4276 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
4277 if (!persistent) {
4278 DEBUG(DEBUG_ERR, ("Database '%s' is not persistent\n", db_name));
4279 talloc_free(tmp_ctx);
4280 return -1;
4283 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4284 if (ctdb_db == NULL) {
4285 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n", db_name));
4286 talloc_free(tmp_ctx);
4287 return -1;
4290 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4291 if (h == NULL) {
4292 DEBUG(DEBUG_ERR, ("Failed to start transaction on database %s\n", db_name));
4293 talloc_free(tmp_ctx);
4294 return -1;
4297 key.dptr = discard_const(argv[1]);
4298 key.dsize = strlen(argv[1]);
4299 ret = ctdb_transaction_store(h, key, tdb_null);
4300 if (ret != 0) {
4301 DEBUG(DEBUG_ERR, ("Failed to delete record\n"));
4302 talloc_free(tmp_ctx);
4303 return -1;
4306 ret = ctdb_transaction_commit(h);
4307 if (ret != 0) {
4308 DEBUG(DEBUG_ERR, ("Failed to commit transaction\n"));
4309 talloc_free(tmp_ctx);
4310 return -1;
4313 talloc_free(tmp_ctx);
4314 return 0;
4317 static const char *ptrans_parse_string(TALLOC_CTX *mem_ctx, const char *s,
4318 TDB_DATA *data)
4320 const char *t;
4321 size_t n;
4322 const char *ret; /* Next byte after successfully parsed value */
4324 /* Error, unless someone says otherwise */
4325 ret = NULL;
4326 /* Indicates no value to parse */
4327 *data = tdb_null;
4329 /* Skip whitespace */
4330 n = strspn(s, " \t");
4331 t = s + n;
4333 if (t[0] == '"') {
4334 /* Quoted ASCII string - no wide characters! */
4335 t++;
4336 n = strcspn(t, "\"");
4337 if (t[n] == '"') {
4338 if (n > 0) {
4339 data->dsize = n;
4340 data->dptr = talloc_memdup(mem_ctx, t, n);
4341 CTDB_NOMEM_ABORT(data->dptr);
4343 ret = t + n + 1;
4344 } else {
4345 DEBUG(DEBUG_WARNING,("Unmatched \" in input %s\n", s));
4347 } else {
4348 DEBUG(DEBUG_WARNING,("Unsupported input format in %s\n", s));
4351 return ret;
4354 static bool ptrans_get_key_value(TALLOC_CTX *mem_ctx, FILE *file,
4355 TDB_DATA *key, TDB_DATA *value)
4357 char line [1024]; /* FIXME: make this more flexible? */
4358 const char *t;
4359 char *ptr;
4361 ptr = fgets(line, sizeof(line), file);
4363 if (ptr == NULL) {
4364 return false;
4367 /* Get key */
4368 t = ptrans_parse_string(mem_ctx, line, key);
4369 if (t == NULL || key->dptr == NULL) {
4370 /* Line Ignored but not EOF */
4371 return true;
4374 /* Get value */
4375 t = ptrans_parse_string(mem_ctx, t, value);
4376 if (t == NULL) {
4377 /* Line Ignored but not EOF */
4378 talloc_free(key->dptr);
4379 *key = tdb_null;
4380 return true;
4383 return true;
4387 * Update a persistent database as per file/stdin
4389 static int control_ptrans(struct ctdb_context *ctdb,
4390 int argc, const char **argv)
4392 const char *db_name;
4393 struct ctdb_db_context *ctdb_db;
4394 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4395 struct ctdb_transaction_handle *h;
4396 TDB_DATA key, value;
4397 FILE *file;
4398 int ret;
4400 if (argc < 1) {
4401 talloc_free(tmp_ctx);
4402 usage();
4405 file = stdin;
4406 if (argc == 2) {
4407 file = fopen(argv[1], "r");
4408 if (file == NULL) {
4409 DEBUG(DEBUG_ERR,("Unable to open file for reading '%s'\n", argv[1]));
4410 talloc_free(tmp_ctx);
4411 return -1;
4415 db_name = argv[0];
4417 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
4418 if (ctdb_db == NULL) {
4419 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4420 goto error;
4423 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4424 if (h == NULL) {
4425 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4426 goto error;
4429 while (ptrans_get_key_value(tmp_ctx, file, &key, &value)) {
4430 if (key.dsize != 0) {
4431 ret = ctdb_transaction_store(h, key, value);
4432 /* Minimise memory use */
4433 talloc_free(key.dptr);
4434 if (value.dptr != NULL) {
4435 talloc_free(value.dptr);
4437 if (ret != 0) {
4438 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4439 ctdb_transaction_cancel(h);
4440 goto error;
4445 ret = ctdb_transaction_commit(h);
4446 if (ret != 0) {
4447 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4448 goto error;
4451 if (file != stdin) {
4452 fclose(file);
4454 talloc_free(tmp_ctx);
4455 return 0;
4457 error:
4458 if (file != stdin) {
4459 fclose(file);
4462 talloc_free(tmp_ctx);
4463 return -1;
4467 check if a service is bound to a port or not
4469 static int control_chktcpport(struct ctdb_context *ctdb, int argc, const char **argv)
4471 int s, ret;
4472 int v;
4473 int port;
4474 struct sockaddr_in sin;
4476 if (argc != 1) {
4477 printf("Use: ctdb chktcport <port>\n");
4478 return EINVAL;
4481 port = atoi(argv[0]);
4483 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
4484 if (s == -1) {
4485 printf("Failed to open local socket\n");
4486 return errno;
4489 v = fcntl(s, F_GETFL, 0);
4490 if (v == -1 || fcntl(s, F_SETFL, v | O_NONBLOCK) != 0) {
4491 printf("Unable to set socket non-blocking: %s\n", strerror(errno));
4494 bzero(&sin, sizeof(sin));
4495 sin.sin_family = PF_INET;
4496 sin.sin_port = htons(port);
4497 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
4498 close(s);
4499 if (ret == -1) {
4500 printf("Failed to bind to local socket: %d %s\n", errno, strerror(errno));
4501 return errno;
4504 return 0;
4509 static void log_handler(struct ctdb_context *ctdb, uint64_t srvid,
4510 TDB_DATA data, void *private_data)
4512 DEBUG(DEBUG_ERR,("Log data received\n"));
4513 if (data.dsize > 0) {
4514 printf("%s", data.dptr);
4517 exit(0);
4521 display a list of log messages from the in memory ringbuffer
4523 static int control_getlog(struct ctdb_context *ctdb, int argc, const char **argv)
4525 int ret, i;
4526 bool main_daemon;
4527 struct ctdb_get_log_addr log_addr;
4528 TDB_DATA data;
4529 struct timeval tv;
4531 /* Process options */
4532 main_daemon = true;
4533 log_addr.pnn = ctdb_get_pnn(ctdb);
4534 log_addr.level = DEBUG_NOTICE;
4535 for (i = 0; i < argc; i++) {
4536 if (strcmp(argv[i], "recoverd") == 0) {
4537 main_daemon = false;
4538 } else {
4539 if (isalpha(argv[i][0]) || argv[i][0] == '-') {
4540 log_addr.level = get_debug_by_desc(argv[i]);
4541 } else {
4542 log_addr.level = strtol(argv[i], NULL, 0);
4547 /* Our message port is our PID */
4548 log_addr.srvid = getpid();
4550 data.dptr = (unsigned char *)&log_addr;
4551 data.dsize = sizeof(log_addr);
4553 DEBUG(DEBUG_ERR, ("Pulling logs from node %u\n", options.pnn));
4555 ctdb_client_set_message_handler(ctdb, log_addr.srvid, log_handler, NULL);
4556 sleep(1);
4558 DEBUG(DEBUG_ERR,("Listen for response on %d\n", (int)log_addr.srvid));
4560 if (main_daemon) {
4561 int32_t res;
4562 char *errmsg;
4563 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4565 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_GET_LOG,
4566 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
4567 if (ret != 0 || res != 0) {
4568 DEBUG(DEBUG_ERR,("Failed to get logs - %s\n", errmsg));
4569 talloc_free(tmp_ctx);
4570 return -1;
4572 talloc_free(tmp_ctx);
4573 } else {
4574 ret = ctdb_client_send_message(ctdb, options.pnn,
4575 CTDB_SRVID_GETLOG, data);
4576 if (ret != 0) {
4577 DEBUG(DEBUG_ERR,("Failed to send getlog request message to %u\n", options.pnn));
4578 return -1;
4582 tv = timeval_current();
4583 /* this loop will terminate when we have received the reply */
4584 while (timeval_elapsed(&tv) < (double)options.timelimit) {
4585 event_loop_once(ctdb->ev);
4588 DEBUG(DEBUG_INFO,("Timed out waiting for log data.\n"));
4590 return 0;
4594 clear the in memory log area
4596 static int control_clearlog(struct ctdb_context *ctdb, int argc, const char **argv)
4598 int ret;
4600 if (argc == 0 || (argc >= 1 && strcmp(argv[0], "recoverd") != 0)) {
4601 /* "recoverd" not given - get logs from main daemon */
4602 int32_t res;
4603 char *errmsg;
4604 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4606 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_CLEAR_LOG,
4607 0, tdb_null, tmp_ctx, NULL, &res, NULL, &errmsg);
4608 if (ret != 0 || res != 0) {
4609 DEBUG(DEBUG_ERR,("Failed to clear logs\n"));
4610 talloc_free(tmp_ctx);
4611 return -1;
4614 talloc_free(tmp_ctx);
4615 } else {
4616 TDB_DATA data; /* unused in recoverd... */
4617 data.dsize = 0;
4619 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_CLEARLOG, data);
4620 if (ret != 0) {
4621 DEBUG(DEBUG_ERR,("Failed to send clearlog request message to %u\n", options.pnn));
4622 return -1;
4626 return 0;
4629 /* Reload public IPs on a specified nodes */
4630 static int control_reloadips(struct ctdb_context *ctdb, int argc, const char **argv)
4632 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4633 uint32_t *nodes;
4634 uint32_t pnn_mode;
4635 uint32_t timeout;
4636 int ret;
4638 assert_single_node_only();
4640 if (argc > 1) {
4641 usage();
4644 /* Determine the nodes where IPs need to be reloaded */
4645 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
4646 options.pnn, true, &nodes, &pnn_mode)) {
4647 ret = -1;
4648 goto done;
4651 again:
4652 /* Disable takeover runs on all connected nodes. A reply
4653 * indicating success is needed from each node so all nodes
4654 * will need to be active. This will retry until maxruntime
4655 * is exceeded, hence no error handling.
4657 * A check could be added to not allow reloading of IPs when
4658 * there are disconnected nodes. However, this should
4659 * probably be left up to the administrator.
4661 timeout = LONGTIMEOUT;
4662 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, &timeout,
4663 "Disable takeover runs", true);
4665 /* Now tell all the desired nodes to reload their public IPs.
4666 * Keep trying this until it succeeds. This assumes all
4667 * failures are transient, which might not be true...
4669 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_RELOAD_PUBLIC_IPS,
4670 nodes, 0, LONGTIMELIMIT(),
4671 false, tdb_null,
4672 NULL, NULL, NULL) != 0) {
4673 DEBUG(DEBUG_ERR,
4674 ("Unable to reload IPs on some nodes, try again.\n"));
4675 goto again;
4678 /* It isn't strictly necessary to wait until takeover runs are
4679 * re-enabled but doing so can't hurt.
4681 timeout = 0;
4682 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, &timeout,
4683 "Enable takeover runs", true);
4685 ipreallocate(ctdb);
4687 ret = 0;
4688 done:
4689 talloc_free(tmp_ctx);
4690 return ret;
4694 display a list of the databases on a remote ctdb
4696 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
4698 int i, ret;
4699 struct ctdb_dbid_map *dbmap=NULL;
4701 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
4702 if (ret != 0) {
4703 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4704 return ret;
4707 if(options.machinereadable){
4708 printf(":ID:Name:Path:Persistent:Sticky:Unhealthy:ReadOnly:\n");
4709 for(i=0;i<dbmap->num;i++){
4710 const char *path;
4711 const char *name;
4712 const char *health;
4713 bool persistent;
4714 bool readonly;
4715 bool sticky;
4717 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
4718 dbmap->dbs[i].dbid, ctdb, &path);
4719 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
4720 dbmap->dbs[i].dbid, ctdb, &name);
4721 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
4722 dbmap->dbs[i].dbid, ctdb, &health);
4723 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4724 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4725 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4726 printf(":0x%08X:%s:%s:%d:%d:%d:%d:\n",
4727 dbmap->dbs[i].dbid, name, path,
4728 !!(persistent), !!(sticky),
4729 !!(health), !!(readonly));
4731 return 0;
4734 printf("Number of databases:%d\n", dbmap->num);
4735 for(i=0;i<dbmap->num;i++){
4736 const char *path;
4737 const char *name;
4738 const char *health;
4739 bool persistent;
4740 bool readonly;
4741 bool sticky;
4743 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
4744 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
4745 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
4746 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4747 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4748 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4749 printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
4750 dbmap->dbs[i].dbid, name, path,
4751 persistent?" PERSISTENT":"",
4752 sticky?" STICKY":"",
4753 readonly?" READONLY":"",
4754 health?" UNHEALTHY":"");
4757 return 0;
4761 display the status of a database on a remote ctdb
4763 static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
4765 const char *db_name;
4766 uint32_t db_id;
4767 uint8_t flags;
4768 const char *path;
4769 const char *health;
4771 if (argc < 1) {
4772 usage();
4775 if (!db_exists(ctdb, argv[0], &db_id, &db_name, &flags)) {
4776 return -1;
4779 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &path);
4780 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &health);
4781 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
4782 db_id, db_name, path,
4783 (flags & CTDB_DB_FLAGS_PERSISTENT ? "yes" : "no"),
4784 (flags & CTDB_DB_FLAGS_STICKY ? "yes" : "no"),
4785 (flags & CTDB_DB_FLAGS_READONLY ? "yes" : "no"),
4786 (health ? health : "OK"));
4788 return 0;
4792 check if the local node is recmaster or not
4793 it will return 1 if this node is the recmaster and 0 if it is not
4794 or if the local ctdb daemon could not be contacted
4796 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
4798 uint32_t mypnn, recmaster;
4799 int ret;
4801 assert_single_node_only();
4803 mypnn = getpnn(ctdb);
4805 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
4806 if (ret != 0) {
4807 printf("Failed to get the recmaster\n");
4808 return 1;
4811 if (recmaster != mypnn) {
4812 printf("this node is not the recmaster\n");
4813 return 1;
4816 printf("this node is the recmaster\n");
4817 return 0;
4821 ping a node
4823 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
4825 int ret;
4826 struct timeval tv = timeval_current();
4827 ret = ctdb_ctrl_ping(ctdb, options.pnn);
4828 if (ret == -1) {
4829 printf("Unable to get ping response from node %u\n", options.pnn);
4830 return -1;
4831 } else {
4832 printf("response from %u time=%.6f sec (%d clients)\n",
4833 options.pnn, timeval_elapsed(&tv), ret);
4835 return 0;
4840 get a node's runstate
4842 static int control_runstate(struct ctdb_context *ctdb, int argc, const char **argv)
4844 int ret;
4845 enum ctdb_runstate runstate;
4847 ret = ctdb_ctrl_get_runstate(ctdb, TIMELIMIT(), options.pnn, &runstate);
4848 if (ret == -1) {
4849 printf("Unable to get runstate response from node %u\n",
4850 options.pnn);
4851 return -1;
4852 } else {
4853 bool found = true;
4854 enum ctdb_runstate t;
4855 int i;
4856 for (i=0; i<argc; i++) {
4857 found = false;
4858 t = runstate_from_string(argv[i]);
4859 if (t == CTDB_RUNSTATE_UNKNOWN) {
4860 printf("Invalid run state (%s)\n", argv[i]);
4861 return -1;
4864 if (t == runstate) {
4865 found = true;
4866 break;
4870 if (!found) {
4871 printf("CTDB not in required run state (got %s)\n",
4872 runstate_to_string((enum ctdb_runstate)runstate));
4873 return -1;
4877 printf("%s\n", runstate_to_string(runstate));
4878 return 0;
4883 get a tunable
4885 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
4887 const char *name;
4888 uint32_t value;
4889 int ret;
4891 if (argc < 1) {
4892 usage();
4895 name = argv[0];
4896 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
4897 if (ret != 0) {
4898 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
4899 return -1;
4902 printf("%-23s = %u\n", name, value);
4903 return 0;
4907 set a tunable
4909 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
4911 const char *name;
4912 uint32_t value;
4913 int ret;
4915 if (argc < 2) {
4916 usage();
4919 name = argv[0];
4920 value = strtoul(argv[1], NULL, 0);
4922 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
4923 if (ret == -1) {
4924 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
4925 return -1;
4927 return 0;
4931 list all tunables
4933 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
4935 uint32_t count;
4936 const char **list;
4937 int ret, i;
4939 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
4940 if (ret == -1) {
4941 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
4942 return -1;
4945 for (i=0;i<count;i++) {
4946 control_getvar(ctdb, 1, &list[i]);
4949 talloc_free(list);
4951 return 0;
4955 display debug level on a node
4957 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4959 int ret;
4960 int32_t level;
4962 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
4963 if (ret != 0) {
4964 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
4965 return ret;
4966 } else {
4967 if (options.machinereadable){
4968 printf(":Name:Level:\n");
4969 printf(":%s:%d:\n",get_debug_by_level(level),level);
4970 } else {
4971 printf("Node %u is at debug level %s (%d)\n", options.pnn, get_debug_by_level(level), level);
4974 return 0;
4978 display reclock file of a node
4980 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4982 int ret;
4983 const char *reclock;
4985 ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
4986 if (ret != 0) {
4987 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4988 return ret;
4989 } else {
4990 if (options.machinereadable){
4991 if (reclock != NULL) {
4992 printf("%s", reclock);
4994 } else {
4995 if (reclock == NULL) {
4996 printf("No reclock file used.\n");
4997 } else {
4998 printf("Reclock file:%s\n", reclock);
5002 return 0;
5006 set the reclock file of a node
5008 static int control_setreclock(struct ctdb_context *ctdb, int argc, const char **argv)
5010 int ret;
5011 const char *reclock;
5013 if (argc == 0) {
5014 reclock = NULL;
5015 } else if (argc == 1) {
5016 reclock = argv[0];
5017 } else {
5018 usage();
5021 ret = ctdb_ctrl_setreclock(ctdb, TIMELIMIT(), options.pnn, reclock);
5022 if (ret != 0) {
5023 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
5024 return ret;
5026 return 0;
5030 set the natgw state on/off
5032 static int control_setnatgwstate(struct ctdb_context *ctdb, int argc, const char **argv)
5034 int ret;
5035 uint32_t natgwstate;
5037 if (argc == 0) {
5038 usage();
5041 if (!strcmp(argv[0], "on")) {
5042 natgwstate = 1;
5043 } else if (!strcmp(argv[0], "off")) {
5044 natgwstate = 0;
5045 } else {
5046 usage();
5049 ret = ctdb_ctrl_setnatgwstate(ctdb, TIMELIMIT(), options.pnn, natgwstate);
5050 if (ret != 0) {
5051 DEBUG(DEBUG_ERR, ("Unable to set the natgw state for node %u\n", options.pnn));
5052 return ret;
5055 return 0;
5059 set the lmaster role on/off
5061 static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
5063 int ret;
5064 uint32_t lmasterrole;
5066 if (argc == 0) {
5067 usage();
5070 if (!strcmp(argv[0], "on")) {
5071 lmasterrole = 1;
5072 } else if (!strcmp(argv[0], "off")) {
5073 lmasterrole = 0;
5074 } else {
5075 usage();
5078 ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
5079 if (ret != 0) {
5080 DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
5081 return ret;
5084 return 0;
5088 set the recmaster role on/off
5090 static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
5092 int ret;
5093 uint32_t recmasterrole;
5095 if (argc == 0) {
5096 usage();
5099 if (!strcmp(argv[0], "on")) {
5100 recmasterrole = 1;
5101 } else if (!strcmp(argv[0], "off")) {
5102 recmasterrole = 0;
5103 } else {
5104 usage();
5107 ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
5108 if (ret != 0) {
5109 DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
5110 return ret;
5113 return 0;
5117 set debug level on a node or all nodes
5119 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
5121 int i, ret;
5122 int32_t level;
5124 if (argc == 0) {
5125 printf("You must specify the debug level. Valid levels are:\n");
5126 for (i=0; debug_levels[i].description != NULL; i++) {
5127 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
5130 return 0;
5133 if (isalpha(argv[0][0]) || argv[0][0] == '-') {
5134 level = get_debug_by_desc(argv[0]);
5135 } else {
5136 level = strtol(argv[0], NULL, 0);
5139 for (i=0; debug_levels[i].description != NULL; i++) {
5140 if (level == debug_levels[i].level) {
5141 break;
5144 if (debug_levels[i].description == NULL) {
5145 printf("Invalid debug level, must be one of\n");
5146 for (i=0; debug_levels[i].description != NULL; i++) {
5147 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
5149 return -1;
5152 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
5153 if (ret != 0) {
5154 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
5156 return 0;
5161 thaw a node
5163 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
5165 int ret;
5166 uint32_t priority;
5168 if (argc == 1) {
5169 priority = strtol(argv[0], NULL, 0);
5170 } else {
5171 priority = 0;
5173 DEBUG(DEBUG_ERR,("Thaw by priority %u\n", priority));
5175 ret = ctdb_ctrl_thaw_priority(ctdb, TIMELIMIT(), options.pnn, priority);
5176 if (ret != 0) {
5177 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
5179 return 0;
5184 attach to a database
5186 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
5188 const char *db_name;
5189 struct ctdb_db_context *ctdb_db;
5190 bool persistent = false;
5192 if (argc < 1) {
5193 usage();
5195 db_name = argv[0];
5196 if (argc > 2) {
5197 usage();
5199 if (argc == 2) {
5200 if (strcmp(argv[1], "persistent") != 0) {
5201 usage();
5203 persistent = true;
5206 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
5207 if (ctdb_db == NULL) {
5208 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
5209 return -1;
5212 return 0;
5216 set db priority
5218 static int control_setdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
5220 struct ctdb_db_priority db_prio;
5221 int ret;
5223 if (argc < 2) {
5224 usage();
5227 db_prio.db_id = strtoul(argv[0], NULL, 0);
5228 db_prio.priority = strtoul(argv[1], NULL, 0);
5230 ret = ctdb_ctrl_set_db_priority(ctdb, TIMELIMIT(), options.pnn, &db_prio);
5231 if (ret != 0) {
5232 DEBUG(DEBUG_ERR,("Unable to set db prio\n"));
5233 return -1;
5236 return 0;
5240 get db priority
5242 static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
5244 uint32_t db_id, priority;
5245 int ret;
5247 if (argc < 1) {
5248 usage();
5251 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5252 return -1;
5255 ret = ctdb_ctrl_get_db_priority(ctdb, TIMELIMIT(), options.pnn, db_id, &priority);
5256 if (ret != 0) {
5257 DEBUG(DEBUG_ERR,("Unable to get db prio\n"));
5258 return -1;
5261 DEBUG(DEBUG_ERR,("Priority:%u\n", priority));
5263 return 0;
5267 set the sticky records capability for a database
5269 static int control_setdbsticky(struct ctdb_context *ctdb, int argc, const char **argv)
5271 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5272 uint32_t db_id;
5273 int ret;
5275 if (argc < 1) {
5276 usage();
5279 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5280 return -1;
5283 ret = ctdb_ctrl_set_db_sticky(ctdb, options.pnn, db_id);
5284 if (ret != 0) {
5285 DEBUG(DEBUG_ERR,("Unable to set db to support sticky records\n"));
5286 talloc_free(tmp_ctx);
5287 return -1;
5290 talloc_free(tmp_ctx);
5291 return 0;
5295 set the readonly capability for a database
5297 static int control_setdbreadonly(struct ctdb_context *ctdb, int argc, const char **argv)
5299 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5300 uint32_t db_id;
5301 int ret;
5303 if (argc < 1) {
5304 usage();
5307 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5308 return -1;
5311 ret = ctdb_ctrl_set_db_readonly(ctdb, options.pnn, db_id);
5312 if (ret != 0) {
5313 DEBUG(DEBUG_ERR,("Unable to set db to support readonly\n"));
5314 talloc_free(tmp_ctx);
5315 return -1;
5318 talloc_free(tmp_ctx);
5319 return 0;
5323 get db seqnum
5325 static int control_getdbseqnum(struct ctdb_context *ctdb, int argc, const char **argv)
5327 uint32_t db_id;
5328 uint64_t seqnum;
5329 int ret;
5331 if (argc < 1) {
5332 usage();
5335 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5336 return -1;
5339 ret = ctdb_ctrl_getdbseqnum(ctdb, TIMELIMIT(), options.pnn, db_id, &seqnum);
5340 if (ret != 0) {
5341 DEBUG(DEBUG_ERR, ("Unable to get seqnum from node."));
5342 return -1;
5345 printf("Sequence number:%lld\n", (long long)seqnum);
5347 return 0;
5351 run an eventscript on a node
5353 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
5355 TDB_DATA data;
5356 int ret;
5357 int32_t res;
5358 char *errmsg;
5359 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5361 if (argc != 1) {
5362 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5363 return -1;
5366 data.dptr = (unsigned char *)discard_const(argv[0]);
5367 data.dsize = strlen((char *)data.dptr) + 1;
5369 DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
5371 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
5372 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
5373 if (ret != 0 || res != 0) {
5374 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
5375 talloc_free(tmp_ctx);
5376 return -1;
5378 talloc_free(tmp_ctx);
5379 return 0;
5382 #define DB_VERSION 1
5383 #define MAX_DB_NAME 64
5384 struct db_file_header {
5385 unsigned long version;
5386 time_t timestamp;
5387 unsigned long persistent;
5388 unsigned long size;
5389 const char name[MAX_DB_NAME];
5392 struct backup_data {
5393 struct ctdb_marshall_buffer *records;
5394 uint32_t len;
5395 uint32_t total;
5396 bool traverse_error;
5399 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
5401 struct backup_data *bd = talloc_get_type(private, struct backup_data);
5402 struct ctdb_rec_data *rec;
5404 /* add the record */
5405 rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
5406 if (rec == NULL) {
5407 bd->traverse_error = true;
5408 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
5409 return -1;
5411 bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
5412 if (bd->records == NULL) {
5413 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
5414 bd->traverse_error = true;
5415 return -1;
5417 bd->records->count++;
5418 memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
5419 bd->len += rec->length;
5420 talloc_free(rec);
5422 bd->total++;
5423 return 0;
5427 * backup a database to a file
5429 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
5431 const char *db_name;
5432 int ret;
5433 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5434 struct db_file_header dbhdr;
5435 struct ctdb_db_context *ctdb_db;
5436 struct backup_data *bd;
5437 int fh = -1;
5438 int status = -1;
5439 const char *reason = NULL;
5440 uint32_t db_id;
5441 uint8_t flags;
5443 assert_single_node_only();
5445 if (argc != 2) {
5446 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5447 return -1;
5450 if (!db_exists(ctdb, argv[0], &db_id, &db_name, &flags)) {
5451 return -1;
5454 ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
5455 db_id, tmp_ctx, &reason);
5456 if (ret != 0) {
5457 DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
5458 argv[0]));
5459 talloc_free(tmp_ctx);
5460 return -1;
5462 if (reason) {
5463 uint32_t allow_unhealthy = 0;
5465 ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
5466 "AllowUnhealthyDBRead",
5467 &allow_unhealthy);
5469 if (allow_unhealthy != 1) {
5470 DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
5471 argv[0], reason));
5473 DEBUG(DEBUG_ERR,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
5474 allow_unhealthy));
5475 talloc_free(tmp_ctx);
5476 return -1;
5479 DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
5480 argv[0], argv[0]));
5481 DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
5482 "tunnable AllowUnhealthyDBRead = %u\n",
5483 allow_unhealthy));
5486 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5487 if (ctdb_db == NULL) {
5488 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
5489 talloc_free(tmp_ctx);
5490 return -1;
5494 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
5495 if (ret == -1) {
5496 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
5497 talloc_free(tmp_ctx);
5498 return -1;
5502 bd = talloc_zero(tmp_ctx, struct backup_data);
5503 if (bd == NULL) {
5504 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
5505 talloc_free(tmp_ctx);
5506 return -1;
5509 bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
5510 if (bd->records == NULL) {
5511 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
5512 talloc_free(tmp_ctx);
5513 return -1;
5516 bd->len = offsetof(struct ctdb_marshall_buffer, data);
5517 bd->records->db_id = ctdb_db->db_id;
5518 /* traverse the database collecting all records */
5519 if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
5520 bd->traverse_error) {
5521 DEBUG(DEBUG_ERR,("Traverse error\n"));
5522 talloc_free(tmp_ctx);
5523 return -1;
5526 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
5529 fh = open(argv[1], O_RDWR|O_CREAT, 0600);
5530 if (fh == -1) {
5531 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
5532 talloc_free(tmp_ctx);
5533 return -1;
5536 ZERO_STRUCT(dbhdr);
5537 dbhdr.version = DB_VERSION;
5538 dbhdr.timestamp = time(NULL);
5539 dbhdr.persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
5540 dbhdr.size = bd->len;
5541 if (strlen(argv[0]) >= MAX_DB_NAME) {
5542 DEBUG(DEBUG_ERR,("Too long dbname\n"));
5543 goto done;
5545 strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME-1);
5546 ret = write(fh, &dbhdr, sizeof(dbhdr));
5547 if (ret == -1) {
5548 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5549 goto done;
5551 ret = write(fh, bd->records, bd->len);
5552 if (ret == -1) {
5553 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5554 goto done;
5557 status = 0;
5558 done:
5559 if (fh != -1) {
5560 ret = close(fh);
5561 if (ret == -1) {
5562 DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
5566 DEBUG(DEBUG_ERR,("Database backed up to %s\n", argv[1]));
5568 talloc_free(tmp_ctx);
5569 return status;
5573 * restore a database from a file
5575 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
5577 int ret;
5578 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5579 TDB_DATA outdata;
5580 TDB_DATA data;
5581 struct db_file_header dbhdr;
5582 struct ctdb_db_context *ctdb_db;
5583 struct ctdb_node_map *nodemap=NULL;
5584 struct ctdb_vnn_map *vnnmap=NULL;
5585 int i, fh;
5586 struct ctdb_control_wipe_database w;
5587 uint32_t *nodes;
5588 uint32_t generation;
5589 struct tm *tm;
5590 char tbuf[100];
5591 char *dbname;
5593 assert_single_node_only();
5595 if (argc < 1 || argc > 2) {
5596 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5597 return -1;
5600 fh = open(argv[0], O_RDONLY);
5601 if (fh == -1) {
5602 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5603 talloc_free(tmp_ctx);
5604 return -1;
5607 read(fh, &dbhdr, sizeof(dbhdr));
5608 if (dbhdr.version != DB_VERSION) {
5609 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5610 close(fh);
5611 talloc_free(tmp_ctx);
5612 return -1;
5615 dbname = discard_const(dbhdr.name);
5616 if (argc == 2) {
5617 dbname = discard_const(argv[1]);
5620 outdata.dsize = dbhdr.size;
5621 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5622 if (outdata.dptr == NULL) {
5623 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5624 close(fh);
5625 talloc_free(tmp_ctx);
5626 return -1;
5628 read(fh, outdata.dptr, outdata.dsize);
5629 close(fh);
5631 tm = localtime(&dbhdr.timestamp);
5632 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5633 printf("Restoring database '%s' from backup @ %s\n",
5634 dbname, tbuf);
5637 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), dbname, dbhdr.persistent, 0);
5638 if (ctdb_db == NULL) {
5639 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbname));
5640 talloc_free(tmp_ctx);
5641 return -1;
5644 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
5645 if (ret != 0) {
5646 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
5647 talloc_free(tmp_ctx);
5648 return ret;
5652 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
5653 if (ret != 0) {
5654 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
5655 talloc_free(tmp_ctx);
5656 return ret;
5659 /* freeze all nodes */
5660 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5661 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5662 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5663 nodes, i,
5664 TIMELIMIT(),
5665 false, tdb_null,
5666 NULL, NULL,
5667 NULL) != 0) {
5668 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5669 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5670 talloc_free(tmp_ctx);
5671 return -1;
5675 generation = vnnmap->generation;
5676 data.dptr = (void *)&generation;
5677 data.dsize = sizeof(generation);
5679 /* start a cluster wide transaction */
5680 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5681 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5682 nodes, 0,
5683 TIMELIMIT(), false, data,
5684 NULL, NULL,
5685 NULL) != 0) {
5686 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
5687 return -1;
5691 w.db_id = ctdb_db->db_id;
5692 w.transaction_id = generation;
5694 data.dptr = (void *)&w;
5695 data.dsize = sizeof(w);
5697 /* wipe all the remote databases. */
5698 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5699 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5700 nodes, 0,
5701 TIMELIMIT(), false, data,
5702 NULL, NULL,
5703 NULL) != 0) {
5704 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5705 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5706 talloc_free(tmp_ctx);
5707 return -1;
5710 /* push the database */
5711 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5712 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
5713 nodes, 0,
5714 TIMELIMIT(), false, outdata,
5715 NULL, NULL,
5716 NULL) != 0) {
5717 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
5718 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5719 talloc_free(tmp_ctx);
5720 return -1;
5723 data.dptr = (void *)&ctdb_db->db_id;
5724 data.dsize = sizeof(ctdb_db->db_id);
5726 /* mark the database as healthy */
5727 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5728 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5729 nodes, 0,
5730 TIMELIMIT(), false, data,
5731 NULL, NULL,
5732 NULL) != 0) {
5733 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5734 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5735 talloc_free(tmp_ctx);
5736 return -1;
5739 data.dptr = (void *)&generation;
5740 data.dsize = sizeof(generation);
5742 /* commit all the changes */
5743 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5744 nodes, 0,
5745 TIMELIMIT(), false, data,
5746 NULL, NULL,
5747 NULL) != 0) {
5748 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5749 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5750 talloc_free(tmp_ctx);
5751 return -1;
5755 /* thaw all nodes */
5756 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5757 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5758 nodes, 0,
5759 TIMELIMIT(),
5760 false, tdb_null,
5761 NULL, NULL,
5762 NULL) != 0) {
5763 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5764 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5765 talloc_free(tmp_ctx);
5766 return -1;
5770 talloc_free(tmp_ctx);
5771 return 0;
5775 * dump a database backup from a file
5777 static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
5779 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5780 TDB_DATA outdata;
5781 struct db_file_header dbhdr;
5782 int i, fh;
5783 struct tm *tm;
5784 char tbuf[100];
5785 struct ctdb_rec_data *rec = NULL;
5786 struct ctdb_marshall_buffer *m;
5787 struct ctdb_dump_db_context c;
5789 assert_single_node_only();
5791 if (argc != 1) {
5792 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5793 return -1;
5796 fh = open(argv[0], O_RDONLY);
5797 if (fh == -1) {
5798 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5799 talloc_free(tmp_ctx);
5800 return -1;
5803 read(fh, &dbhdr, sizeof(dbhdr));
5804 if (dbhdr.version != DB_VERSION) {
5805 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5806 close(fh);
5807 talloc_free(tmp_ctx);
5808 return -1;
5811 outdata.dsize = dbhdr.size;
5812 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5813 if (outdata.dptr == NULL) {
5814 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5815 close(fh);
5816 talloc_free(tmp_ctx);
5817 return -1;
5819 read(fh, outdata.dptr, outdata.dsize);
5820 close(fh);
5821 m = (struct ctdb_marshall_buffer *)outdata.dptr;
5823 tm = localtime(&dbhdr.timestamp);
5824 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5825 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
5826 dbhdr.name, m->db_id, tbuf);
5828 ZERO_STRUCT(c);
5829 c.f = stdout;
5830 c.printemptyrecords = (bool)options.printemptyrecords;
5831 c.printdatasize = (bool)options.printdatasize;
5832 c.printlmaster = false;
5833 c.printhash = (bool)options.printhash;
5834 c.printrecordflags = (bool)options.printrecordflags;
5836 for (i=0; i < m->count; i++) {
5837 uint32_t reqid = 0;
5838 TDB_DATA key, data;
5840 /* we do not want the header splitted, so we pass NULL*/
5841 rec = ctdb_marshall_loop_next(m, rec, &reqid,
5842 NULL, &key, &data);
5844 ctdb_dumpdb_record(ctdb, key, data, &c);
5847 printf("Dumped %d records\n", i);
5848 talloc_free(tmp_ctx);
5849 return 0;
5853 * wipe a database from a file
5855 static int control_wipedb(struct ctdb_context *ctdb, int argc,
5856 const char **argv)
5858 const char *db_name;
5859 int ret;
5860 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5861 TDB_DATA data;
5862 struct ctdb_db_context *ctdb_db;
5863 struct ctdb_node_map *nodemap = NULL;
5864 struct ctdb_vnn_map *vnnmap = NULL;
5865 int i;
5866 struct ctdb_control_wipe_database w;
5867 uint32_t *nodes;
5868 uint32_t generation;
5869 uint8_t flags;
5871 assert_single_node_only();
5873 if (argc != 1) {
5874 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5875 return -1;
5878 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
5879 return -1;
5882 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5883 if (ctdb_db == NULL) {
5884 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
5885 argv[0]));
5886 talloc_free(tmp_ctx);
5887 return -1;
5890 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
5891 &nodemap);
5892 if (ret != 0) {
5893 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5894 options.pnn));
5895 talloc_free(tmp_ctx);
5896 return ret;
5899 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5900 &vnnmap);
5901 if (ret != 0) {
5902 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
5903 options.pnn));
5904 talloc_free(tmp_ctx);
5905 return ret;
5908 /* freeze all nodes */
5909 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5910 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5911 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5912 nodes, i,
5913 TIMELIMIT(),
5914 false, tdb_null,
5915 NULL, NULL,
5916 NULL);
5917 if (ret != 0) {
5918 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5919 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
5920 CTDB_RECOVERY_ACTIVE);
5921 talloc_free(tmp_ctx);
5922 return -1;
5926 generation = vnnmap->generation;
5927 data.dptr = (void *)&generation;
5928 data.dsize = sizeof(generation);
5930 /* start a cluster wide transaction */
5931 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5932 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5933 nodes, 0,
5934 TIMELIMIT(), false, data,
5935 NULL, NULL,
5936 NULL);
5937 if (ret!= 0) {
5938 DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
5939 "transactions.\n"));
5940 return -1;
5943 w.db_id = ctdb_db->db_id;
5944 w.transaction_id = generation;
5946 data.dptr = (void *)&w;
5947 data.dsize = sizeof(w);
5949 /* wipe all the remote databases. */
5950 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5951 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5952 nodes, 0,
5953 TIMELIMIT(), false, data,
5954 NULL, NULL,
5955 NULL) != 0) {
5956 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5957 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5958 talloc_free(tmp_ctx);
5959 return -1;
5962 data.dptr = (void *)&ctdb_db->db_id;
5963 data.dsize = sizeof(ctdb_db->db_id);
5965 /* mark the database as healthy */
5966 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5967 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5968 nodes, 0,
5969 TIMELIMIT(), false, data,
5970 NULL, NULL,
5971 NULL) != 0) {
5972 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5973 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5974 talloc_free(tmp_ctx);
5975 return -1;
5978 data.dptr = (void *)&generation;
5979 data.dsize = sizeof(generation);
5981 /* commit all the changes */
5982 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5983 nodes, 0,
5984 TIMELIMIT(), false, data,
5985 NULL, NULL,
5986 NULL) != 0) {
5987 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5988 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5989 talloc_free(tmp_ctx);
5990 return -1;
5993 /* thaw all nodes */
5994 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5995 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5996 nodes, 0,
5997 TIMELIMIT(),
5998 false, tdb_null,
5999 NULL, NULL,
6000 NULL) != 0) {
6001 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
6002 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
6003 talloc_free(tmp_ctx);
6004 return -1;
6007 DEBUG(DEBUG_ERR, ("Database wiped.\n"));
6009 talloc_free(tmp_ctx);
6010 return 0;
6014 dump memory usage
6016 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
6018 TDB_DATA data;
6019 int ret;
6020 int32_t res;
6021 char *errmsg;
6022 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
6023 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
6024 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
6025 if (ret != 0 || res != 0) {
6026 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
6027 talloc_free(tmp_ctx);
6028 return -1;
6030 write(1, data.dptr, data.dsize);
6031 talloc_free(tmp_ctx);
6032 return 0;
6036 handler for memory dumps
6038 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid,
6039 TDB_DATA data, void *private_data)
6041 write(1, data.dptr, data.dsize);
6042 exit(0);
6046 dump memory usage on the recovery daemon
6048 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
6050 int ret;
6051 TDB_DATA data;
6052 struct srvid_request rd;
6054 rd.pnn = ctdb_get_pnn(ctdb);
6055 rd.srvid = getpid();
6057 /* register a message port for receiveing the reply so that we
6058 can receive the reply
6060 ctdb_client_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
6063 data.dptr = (uint8_t *)&rd;
6064 data.dsize = sizeof(rd);
6066 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
6067 if (ret != 0) {
6068 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
6069 return -1;
6072 /* this loop will terminate when we have received the reply */
6073 while (1) {
6074 event_loop_once(ctdb->ev);
6077 return 0;
6081 send a message to a srvid
6083 static int control_msgsend(struct ctdb_context *ctdb, int argc, const char **argv)
6085 unsigned long srvid;
6086 int ret;
6087 TDB_DATA data;
6089 if (argc < 2) {
6090 usage();
6093 srvid = strtoul(argv[0], NULL, 0);
6095 data.dptr = (uint8_t *)discard_const(argv[1]);
6096 data.dsize= strlen(argv[1]);
6098 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, srvid, data);
6099 if (ret != 0) {
6100 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
6101 return -1;
6104 return 0;
6108 handler for msglisten
6110 static void msglisten_handler(struct ctdb_context *ctdb, uint64_t srvid,
6111 TDB_DATA data, void *private_data)
6113 int i;
6115 printf("Message received: ");
6116 for (i=0;i<data.dsize;i++) {
6117 printf("%c", data.dptr[i]);
6119 printf("\n");
6123 listen for messages on a messageport
6125 static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
6127 uint64_t srvid;
6129 srvid = getpid();
6131 /* register a message port and listen for messages
6133 ctdb_client_set_message_handler(ctdb, srvid, msglisten_handler, NULL);
6134 printf("Listening for messages on srvid:%d\n", (int)srvid);
6136 while (1) {
6137 event_loop_once(ctdb->ev);
6140 return 0;
6144 list all nodes in the cluster
6145 we parse the nodes file directly
6147 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
6149 TALLOC_CTX *mem_ctx = talloc_new(NULL);
6150 struct pnn_node *pnn_nodes;
6151 struct pnn_node *pnn_node;
6153 assert_single_node_only();
6155 pnn_nodes = read_nodes_file(mem_ctx);
6156 if (pnn_nodes == NULL) {
6157 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
6158 talloc_free(mem_ctx);
6159 return -1;
6162 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
6163 ctdb_sock_addr addr;
6164 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
6165 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
6166 talloc_free(mem_ctx);
6167 return -1;
6169 if (options.machinereadable){
6170 printf(":%d:%s:\n", pnn_node->pnn, pnn_node->addr);
6171 } else {
6172 printf("%s\n", pnn_node->addr);
6175 talloc_free(mem_ctx);
6177 return 0;
6181 reload the nodes file on the local node
6183 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
6185 int i, ret;
6186 int mypnn;
6187 struct ctdb_node_map *nodemap=NULL;
6189 assert_single_node_only();
6191 mypnn = ctdb_get_pnn(ctdb);
6193 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
6194 if (ret != 0) {
6195 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
6196 return ret;
6199 /* reload the nodes file on all remote nodes */
6200 for (i=0;i<nodemap->num;i++) {
6201 if (nodemap->nodes[i].pnn == mypnn) {
6202 continue;
6204 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
6205 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
6206 nodemap->nodes[i].pnn);
6207 if (ret != 0) {
6208 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
6212 /* reload the nodes file on the local node */
6213 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
6214 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
6215 if (ret != 0) {
6216 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
6219 /* initiate a recovery */
6220 control_recover(ctdb, argc, argv);
6222 return 0;
6226 static const struct {
6227 const char *name;
6228 int (*fn)(struct ctdb_context *, int, const char **);
6229 bool auto_all;
6230 bool without_daemon; /* can be run without daemon running ? */
6231 const char *msg;
6232 const char *args;
6233 } ctdb_commands[] = {
6234 { "version", control_version, true, true, "show version of ctdb" },
6235 { "status", control_status, true, false, "show node status" },
6236 { "uptime", control_uptime, true, false, "show node uptime" },
6237 { "ping", control_ping, true, false, "ping all nodes" },
6238 { "runstate", control_runstate, true, false, "get/check runstate of a node", "[setup|first_recovery|startup|running]" },
6239 { "getvar", control_getvar, true, false, "get a tunable variable", "<name>"},
6240 { "setvar", control_setvar, true, false, "set a tunable variable", "<name> <value>"},
6241 { "listvars", control_listvars, true, false, "list tunable variables"},
6242 { "statistics", control_statistics, false, false, "show statistics" },
6243 { "statisticsreset", control_statistics_reset, true, false, "reset statistics"},
6244 { "stats", control_stats, false, false, "show rolling statistics", "[number of history records]" },
6245 { "ip", control_ip, false, false, "show which public ip's that ctdb manages" },
6246 { "ipinfo", control_ipinfo, true, false, "show details about a public ip that ctdb manages", "<ip>" },
6247 { "ifaces", control_ifaces, true, false, "show which interfaces that ctdb manages" },
6248 { "setifacelink", control_setifacelink, true, false, "set interface link status", "<iface> <status>" },
6249 { "process-exists", control_process_exists, true, false, "check if a process exists on a node", "<pid>"},
6250 { "getdbmap", control_getdbmap, true, false, "show the database map" },
6251 { "getdbstatus", control_getdbstatus, true, false, "show the status of a database", "<dbname|dbid>" },
6252 { "catdb", control_catdb, true, false, "dump a ctdb database" , "<dbname|dbid>"},
6253 { "cattdb", control_cattdb, true, false, "dump a local tdb database" , "<dbname|dbid>"},
6254 { "getmonmode", control_getmonmode, true, false, "show monitoring mode" },
6255 { "getcapabilities", control_getcapabilities, true, false, "show node capabilities" },
6256 { "pnn", control_pnn, true, false, "show the pnn of the currnet node" },
6257 { "lvs", control_lvs, true, false, "show lvs configuration" },
6258 { "lvsmaster", control_lvsmaster, true, false, "show which node is the lvs master" },
6259 { "disablemonitor", control_disable_monmode,true, false, "set monitoring mode to DISABLE" },
6260 { "enablemonitor", control_enable_monmode, true, false, "set monitoring mode to ACTIVE" },
6261 { "setdebug", control_setdebug, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
6262 { "getdebug", control_getdebug, true, false, "get debug level" },
6263 { "getlog", control_getlog, true, false, "get the log data from the in memory ringbuffer", "[<level>] [recoverd]" },
6264 { "clearlog", control_clearlog, true, false, "clear the log data from the in memory ringbuffer", "[recoverd]" },
6265 { "attach", control_attach, true, false, "attach to a database", "<dbname> [persistent]" },
6266 { "dumpmemory", control_dumpmemory, true, false, "dump memory map to stdout" },
6267 { "rddumpmemory", control_rddumpmemory, true, false, "dump memory map from the recovery daemon to stdout" },
6268 { "getpid", control_getpid, true, false, "get ctdbd process ID" },
6269 { "disable", control_disable, true, false, "disable a nodes public IP" },
6270 { "enable", control_enable, true, false, "enable a nodes public IP" },
6271 { "stop", control_stop, true, false, "stop a node" },
6272 { "continue", control_continue, true, false, "re-start a stopped node" },
6273 { "ban", control_ban, true, false, "ban a node from the cluster", "<bantime>"},
6274 { "unban", control_unban, true, false, "unban a node" },
6275 { "showban", control_showban, true, false, "show ban information"},
6276 { "shutdown", control_shutdown, true, false, "shutdown ctdbd" },
6277 { "recover", control_recover, true, false, "force recovery" },
6278 { "sync", control_ipreallocate, false, false, "wait until ctdbd has synced all state changes" },
6279 { "ipreallocate", control_ipreallocate, false, false, "force the recovery daemon to perform a ip reallocation procedure" },
6280 { "thaw", control_thaw, true, false, "thaw databases", "[priority:1-3]" },
6281 { "isnotrecmaster", control_isnotrecmaster, false, false, "check if the local node is recmaster or not" },
6282 { "killtcp", kill_tcp, false, false, "kill a tcp connection.", "[<srcip:port> <dstip:port>]" },
6283 { "gratiousarp", control_gratious_arp, false, false, "send a gratious arp", "<ip> <interface>" },
6284 { "tickle", tickle_tcp, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
6285 { "gettickles", control_get_tickles, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
6286 { "addtickle", control_add_tickle, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
6288 { "deltickle", control_del_tickle, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
6290 { "regsrvid", regsrvid, false, false, "register a server id", "<pnn> <type> <id>" },
6291 { "unregsrvid", unregsrvid, false, false, "unregister a server id", "<pnn> <type> <id>" },
6292 { "chksrvid", chksrvid, false, false, "check if a server id exists", "<pnn> <type> <id>" },
6293 { "getsrvids", getsrvids, false, false, "get a list of all server ids"},
6294 { "check_srvids", check_srvids, false, false, "check if a srvid exists", "<id>+" },
6295 { "repack", ctdb_repack, false, false, "repack all databases", "[max_freelist]"},
6296 { "listnodes", control_listnodes, false, true, "list all nodes in the cluster"},
6297 { "reloadnodes", control_reload_nodes_file, false, false, "reload the nodes file and restart the transport on all nodes"},
6298 { "moveip", control_moveip, false, false, "move/failover an ip address to another node", "<ip> <node>"},
6299 { "rebalanceip", control_rebalanceip, false, false, "release an ip from the node and let recd rebalance it", "<ip>"},
6300 { "addip", control_addip, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
6301 { "delip", control_delip, false, false, "delete an ip address from a node", "<ip>"},
6302 { "eventscript", control_eventscript, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
6303 { "backupdb", control_backupdb, false, false, "backup the database into a file.", "<dbname|dbid> <file>"},
6304 { "restoredb", control_restoredb, false, false, "restore the database from a file.", "<file> [dbname]"},
6305 { "dumpdbbackup", control_dumpdbbackup, false, true, "dump database backup from a file.", "<file>"},
6306 { "wipedb", control_wipedb, false, false, "wipe the contents of a database.", "<dbname|dbid>"},
6307 { "recmaster", control_recmaster, true, false, "show the pnn for the recovery master."},
6308 { "scriptstatus", control_scriptstatus, true, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
6309 { "enablescript", control_enablescript, true, false, "enable an eventscript", "<script>"},
6310 { "disablescript", control_disablescript, true, false, "disable an eventscript", "<script>"},
6311 { "natgwlist", control_natgwlist, true, false, "show the nodes belonging to this natgw configuration"},
6312 { "xpnn", control_xpnn, false, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
6313 { "getreclock", control_getreclock, true, false, "Show the reclock file of a node"},
6314 { "setreclock", control_setreclock, true, false, "Set/clear the reclock file of a node", "[filename]"},
6315 { "setnatgwstate", control_setnatgwstate, false, false, "Set NATGW state to on/off", "{on|off}"},
6316 { "setlmasterrole", control_setlmasterrole, false, false, "Set LMASTER role to on/off", "{on|off}"},
6317 { "setrecmasterrole", control_setrecmasterrole, false, false, "Set RECMASTER role to on/off", "{on|off}"},
6318 { "setdbprio", control_setdbprio, false, false, "Set DB priority", "<dbname|dbid> <prio:1-3>"},
6319 { "getdbprio", control_getdbprio, false, false, "Get DB priority", "<dbname|dbid>"},
6320 { "setdbreadonly", control_setdbreadonly, false, false, "Set DB readonly capable", "<dbname|dbid>"},
6321 { "setdbsticky", control_setdbsticky, false, false, "Set DB sticky-records capable", "<dbname|dbid>"},
6322 { "msglisten", control_msglisten, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
6323 { "msgsend", control_msgsend, false, false, "Send a message to srvid", "<srvid> <message>"},
6324 { "pfetch", control_pfetch, false, false, "fetch a record from a persistent database", "<dbname|dbid> <key> [<file>]" },
6325 { "pstore", control_pstore, false, false, "write a record to a persistent database", "<dbname|dbid> <key> <file containing record>" },
6326 { "pdelete", control_pdelete, false, false, "delete a record from a persistent database", "<dbname|dbid> <key>" },
6327 { "ptrans", control_ptrans, false, false, "update a persistent database (from stdin)", "<dbname|dbid>" },
6328 { "tfetch", control_tfetch, false, true, "fetch a record from a [c]tdb-file [-v]", "<tdb-file> <key> [<file>]" },
6329 { "tstore", control_tstore, false, true, "store a record (including ltdb header)", "<tdb-file> <key> <data> [<rsn> <dmaster> <flags>]" },
6330 { "readkey", control_readkey, true, false, "read the content off a database key", "<dbname|dbid> <key>" },
6331 { "writekey", control_writekey, true, false, "write to a database key", "<dbname|dbid> <key> <value>" },
6332 { "checktcpport", control_chktcpport, false, true, "check if a service is bound to a specific tcp port or not", "<port>" },
6333 { "rebalancenode", control_rebalancenode, false, false, "mark nodes as forced IP rebalancing targets", "[<pnn-list>]"},
6334 { "getdbseqnum", control_getdbseqnum, false, false, "get the sequence number off a database", "<dbname|dbid>" },
6335 { "nodestatus", control_nodestatus, true, false, "show and return node status", "[<pnn-list>]" },
6336 { "dbstatistics", control_dbstatistics, false, false, "show db statistics", "<dbname|dbid>" },
6337 { "reloadips", control_reloadips, false, false, "reload the public addresses file on specified nodes" , "[<pnn-list>]" },
6338 { "ipiface", control_ipiface, false, true, "Find which interface an ip address is hosted on", "<ip>" },
6342 show usage message
6344 static void usage(void)
6346 int i;
6347 printf(
6348 "Usage: ctdb [options] <control>\n" \
6349 "Options:\n" \
6350 " -n <node> choose node number, or 'all' (defaults to local node)\n"
6351 " -Y generate machinereadable output\n"
6352 " -v generate verbose output\n"
6353 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
6354 printf("Controls:\n");
6355 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6356 printf(" %-15s %-27s %s\n",
6357 ctdb_commands[i].name,
6358 ctdb_commands[i].args?ctdb_commands[i].args:"",
6359 ctdb_commands[i].msg);
6361 exit(1);
6365 static void ctdb_alarm(int sig)
6367 printf("Maximum runtime exceeded - exiting\n");
6368 _exit(ERR_TIMEOUT);
6372 main program
6374 int main(int argc, const char *argv[])
6376 struct ctdb_context *ctdb;
6377 char *nodestring = NULL;
6378 struct poptOption popt_options[] = {
6379 POPT_AUTOHELP
6380 POPT_CTDB_CMDLINE
6381 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
6382 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
6383 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
6384 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
6385 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
6386 { "print-emptyrecords", 0, POPT_ARG_NONE, &options.printemptyrecords, 0, "print the empty records when dumping databases (catdb, cattdb, dumpdbbackup)", NULL },
6387 { "print-datasize", 0, POPT_ARG_NONE, &options.printdatasize, 0, "do not print record data when dumping databases, only the data size", NULL },
6388 { "print-lmaster", 0, POPT_ARG_NONE, &options.printlmaster, 0, "print the record's lmaster in catdb", NULL },
6389 { "print-hash", 0, POPT_ARG_NONE, &options.printhash, 0, "print the record's hash when dumping databases", NULL },
6390 { "print-recordflags", 0, POPT_ARG_NONE, &options.printrecordflags, 0, "print the record flags in catdb and dumpdbbackup", NULL },
6391 POPT_TABLEEND
6393 int opt;
6394 const char **extra_argv;
6395 int extra_argc = 0;
6396 int ret=-1, i;
6397 poptContext pc;
6398 struct event_context *ev;
6399 const char *control;
6401 setlinebuf(stdout);
6403 /* set some defaults */
6404 options.maxruntime = 0;
6405 options.timelimit = 10;
6406 options.pnn = CTDB_CURRENT_NODE;
6408 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
6410 while ((opt = poptGetNextOpt(pc)) != -1) {
6411 switch (opt) {
6412 default:
6413 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
6414 poptBadOption(pc, 0), poptStrerror(opt)));
6415 exit(1);
6419 /* setup the remaining options for the main program to use */
6420 extra_argv = poptGetArgs(pc);
6421 if (extra_argv) {
6422 extra_argv++;
6423 while (extra_argv[extra_argc]) extra_argc++;
6426 if (extra_argc < 1) {
6427 usage();
6430 if (options.maxruntime == 0) {
6431 const char *ctdb_timeout;
6432 ctdb_timeout = getenv("CTDB_TIMEOUT");
6433 if (ctdb_timeout != NULL) {
6434 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
6435 } else {
6436 /* default timeout is 120 seconds */
6437 options.maxruntime = 120;
6441 signal(SIGALRM, ctdb_alarm);
6442 alarm(options.maxruntime);
6444 control = extra_argv[0];
6446 /* Default value for CTDB_BASE - don't override */
6447 setenv("CTDB_BASE", ETCDIR "/ctdb", 0);
6449 ev = event_context_init(NULL);
6450 if (!ev) {
6451 DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
6452 exit(1);
6455 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6456 if (strcmp(control, ctdb_commands[i].name) == 0) {
6457 break;
6461 if (i == ARRAY_SIZE(ctdb_commands)) {
6462 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
6463 exit(1);
6466 if (ctdb_commands[i].without_daemon == true) {
6467 if (nodestring != NULL) {
6468 DEBUG(DEBUG_ERR, ("Can't specify node(s) with \"ctdb %s\"\n", control));
6469 exit(1);
6471 return ctdb_commands[i].fn(NULL, extra_argc-1, extra_argv+1);
6474 /* initialise ctdb */
6475 ctdb = ctdb_cmdline_client(ev, TIMELIMIT());
6477 if (ctdb == NULL) {
6478 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
6479 exit(1);
6482 /* setup the node number(s) to contact */
6483 if (!parse_nodestring(ctdb, ctdb, nodestring, CTDB_CURRENT_NODE, false,
6484 &options.nodes, &options.pnn)) {
6485 usage();
6488 if (options.pnn == CTDB_CURRENT_NODE) {
6489 options.pnn = options.nodes[0];
6492 if (ctdb_commands[i].auto_all &&
6493 ((options.pnn == CTDB_BROADCAST_ALL) ||
6494 (options.pnn == CTDB_MULTICAST))) {
6495 int j;
6497 ret = 0;
6498 for (j = 0; j < talloc_array_length(options.nodes); j++) {
6499 options.pnn = options.nodes[j];
6500 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6502 } else {
6503 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6506 talloc_free(ctdb);
6507 talloc_free(ev);
6508 (void)poptFreeContext(pc);
6510 return ret;