ctdb: Use sys_read() and sys_write() to ensure correct signal interaction
[Samba.git] / ctdb / tools / ctdb.c
blobca79363d17381015572fe5e3cec58756de7dba4d
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"
33 #include "lib/util/dlinklist.h"
35 #define ERR_TIMEOUT 20 /* timed out trying to reach node */
36 #define ERR_NONODE 21 /* node does not exist */
37 #define ERR_DISNODE 22 /* node is disconnected */
39 static void usage(void);
41 static struct {
42 int timelimit;
43 uint32_t pnn;
44 uint32_t *nodes;
45 int machinereadable;
46 int verbose;
47 int maxruntime;
48 int printemptyrecords;
49 int printdatasize;
50 int printlmaster;
51 int printhash;
52 int printrecordflags;
53 } options;
55 #define LONGTIMEOUT options.timelimit*10
57 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
58 #define LONGTIMELIMIT() timeval_current_ofs(LONGTIMEOUT, 0)
60 static int control_version(struct ctdb_context *ctdb, int argc, const char **argv)
62 printf("CTDB version: %s\n", CTDB_VERSION_STRING);
63 return 0;
66 #define CTDB_NOMEM_ABORT(p) do { if (!(p)) { \
67 DEBUG(DEBUG_ALERT,("ctdb fatal error: %s\n", \
68 "Out of memory in " __location__ )); \
69 abort(); \
70 }} while (0)
72 static uint32_t getpnn(struct ctdb_context *ctdb)
74 if ((options.pnn == CTDB_BROADCAST_ALL) ||
75 (options.pnn == CTDB_MULTICAST)) {
76 DEBUG(DEBUG_ERR,
77 ("Cannot get PNN for node %u\n", options.pnn));
78 exit(1);
81 if (options.pnn == CTDB_CURRENT_NODE) {
82 return ctdb_get_pnn(ctdb);
83 } else {
84 return options.pnn;
88 static void assert_single_node_only(void)
90 if ((options.pnn == CTDB_BROADCAST_ALL) ||
91 (options.pnn == CTDB_MULTICAST)) {
92 DEBUG(DEBUG_ERR,
93 ("This control can not be applied to multiple PNNs\n"));
94 exit(1);
98 /* Pretty print the flags to a static buffer in human-readable format.
99 * This never returns NULL!
101 static const char *pretty_print_flags(uint32_t flags)
103 int j;
104 static const struct {
105 uint32_t flag;
106 const char *name;
107 } flag_names[] = {
108 { NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
109 { NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
110 { NODE_FLAGS_BANNED, "BANNED" },
111 { NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
112 { NODE_FLAGS_DELETED, "DELETED" },
113 { NODE_FLAGS_STOPPED, "STOPPED" },
114 { NODE_FLAGS_INACTIVE, "INACTIVE" },
116 static char flags_str[512]; /* Big enough to contain all flag names */
118 flags_str[0] = '\0';
119 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
120 if (flags & flag_names[j].flag) {
121 if (flags_str[0] == '\0') {
122 (void) strcpy(flags_str, flag_names[j].name);
123 } else {
124 (void) strncat(flags_str, "|", sizeof(flags_str)-1);
125 (void) strncat(flags_str, flag_names[j].name,
126 sizeof(flags_str)-1);
130 if (flags_str[0] == '\0') {
131 (void) strcpy(flags_str, "OK");
134 return flags_str;
137 static int h2i(char h)
139 if (h >= 'a' && h <= 'f') return h - 'a' + 10;
140 if (h >= 'A' && h <= 'F') return h - 'f' + 10;
141 return h - '0';
144 static TDB_DATA hextodata(TALLOC_CTX *mem_ctx, const char *str)
146 int i, len;
147 TDB_DATA key = {NULL, 0};
149 len = strlen(str);
150 if (len & 0x01) {
151 DEBUG(DEBUG_ERR,("Key specified with odd number of hexadecimal digits\n"));
152 return key;
155 key.dsize = len>>1;
156 key.dptr = talloc_size(mem_ctx, key.dsize);
158 for (i=0; i < len/2; i++) {
159 key.dptr[i] = h2i(str[i*2]) << 4 | h2i(str[i*2+1]);
161 return key;
164 /* Parse a nodestring. Parameter dd_ok controls what happens to nodes
165 * that are disconnected or deleted. If dd_ok is true those nodes are
166 * included in the output list of nodes. If dd_ok is false, those
167 * nodes are filtered from the "all" case and cause an error if
168 * explicitly specified.
170 static bool parse_nodestring(struct ctdb_context *ctdb,
171 TALLOC_CTX *mem_ctx,
172 const char * nodestring,
173 uint32_t current_pnn,
174 bool dd_ok,
175 uint32_t **nodes,
176 uint32_t *pnn_mode)
178 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
179 int n;
180 uint32_t i;
181 struct ctdb_node_map *nodemap;
182 int ret;
184 *nodes = NULL;
186 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
187 if (ret != 0) {
188 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
189 talloc_free(tmp_ctx);
190 exit(10);
193 if (nodestring != NULL) {
194 *nodes = talloc_array(mem_ctx, uint32_t, 0);
195 if (*nodes == NULL) {
196 goto failed;
199 n = 0;
201 if (strcmp(nodestring, "all") == 0) {
202 *pnn_mode = CTDB_BROADCAST_ALL;
204 /* all */
205 for (i = 0; i < nodemap->num; i++) {
206 if ((nodemap->nodes[i].flags &
207 (NODE_FLAGS_DISCONNECTED |
208 NODE_FLAGS_DELETED)) && !dd_ok) {
209 continue;
211 *nodes = talloc_realloc(mem_ctx, *nodes,
212 uint32_t, n+1);
213 if (*nodes == NULL) {
214 goto failed;
216 (*nodes)[n] = i;
217 n++;
219 } else {
220 /* x{,y...} */
221 char *ns, *tok;
223 ns = talloc_strdup(tmp_ctx, nodestring);
224 tok = strtok(ns, ",");
225 while (tok != NULL) {
226 uint32_t pnn;
227 char *endptr;
228 i = (uint32_t)strtoul(tok, &endptr, 0);
229 if (i == 0 && tok == endptr) {
230 DEBUG(DEBUG_ERR,
231 ("Invalid node %s\n", tok));
232 talloc_free(tmp_ctx);
233 exit(ERR_NONODE);
235 if (i >= nodemap->num) {
236 DEBUG(DEBUG_ERR, ("Node %u does not exist\n", i));
237 talloc_free(tmp_ctx);
238 exit(ERR_NONODE);
240 if ((nodemap->nodes[i].flags &
241 (NODE_FLAGS_DISCONNECTED |
242 NODE_FLAGS_DELETED)) && !dd_ok) {
243 DEBUG(DEBUG_ERR, ("Node %u has status %s\n", i, pretty_print_flags(nodemap->nodes[i].flags)));
244 talloc_free(tmp_ctx);
245 exit(ERR_DISNODE);
247 if ((pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), i)) < 0) {
248 DEBUG(DEBUG_ERR, ("Can not access node %u. Node is not operational.\n", i));
249 talloc_free(tmp_ctx);
250 exit(10);
253 *nodes = talloc_realloc(mem_ctx, *nodes,
254 uint32_t, n+1);
255 if (*nodes == NULL) {
256 goto failed;
259 (*nodes)[n] = i;
260 n++;
262 tok = strtok(NULL, ",");
264 talloc_free(ns);
266 if (n == 1) {
267 *pnn_mode = (*nodes)[0];
268 } else {
269 *pnn_mode = CTDB_MULTICAST;
272 } else {
273 /* default - no nodes specified */
274 *nodes = talloc_array(mem_ctx, uint32_t, 1);
275 if (*nodes == NULL) {
276 goto failed;
278 *pnn_mode = CTDB_CURRENT_NODE;
280 if (((*nodes)[0] = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), current_pnn)) < 0) {
281 goto failed;
285 talloc_free(tmp_ctx);
286 return true;
288 failed:
289 talloc_free(tmp_ctx);
290 return false;
294 check if a database exists
296 static bool db_exists(struct ctdb_context *ctdb, const char *dbarg,
297 uint32_t *dbid, const char **dbname, uint8_t *flags)
299 int i, ret;
300 struct ctdb_dbid_map *dbmap=NULL;
301 bool dbid_given = false, found = false;
302 uint32_t id;
303 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
304 const char *name;
306 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
307 if (ret != 0) {
308 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
309 goto fail;
312 if (strncmp(dbarg, "0x", 2) == 0) {
313 id = strtoul(dbarg, NULL, 0);
314 dbid_given = true;
317 for(i=0; i<dbmap->num; i++) {
318 if (dbid_given) {
319 if (id == dbmap->dbs[i].dbid) {
320 found = true;
321 break;
323 } else {
324 ret = ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
325 if (ret != 0) {
326 DEBUG(DEBUG_ERR, ("Unable to get dbname from dbid %u\n", dbmap->dbs[i].dbid));
327 goto fail;
330 if (strcmp(name, dbarg) == 0) {
331 id = dbmap->dbs[i].dbid;
332 found = true;
333 break;
338 if (found && dbid_given && dbname != NULL) {
339 ret = ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
340 if (ret != 0) {
341 DEBUG(DEBUG_ERR, ("Unable to get dbname from dbid %u\n", dbmap->dbs[i].dbid));
342 found = false;
343 goto fail;
347 if (found) {
348 if (dbid) *dbid = id;
349 if (dbname) *dbname = talloc_strdup(ctdb, name);
350 if (flags) *flags = dbmap->dbs[i].flags;
351 } else {
352 DEBUG(DEBUG_ERR,("No database matching '%s' found\n", dbarg));
355 fail:
356 talloc_free(tmp_ctx);
357 return found;
361 see if a process exists
363 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
365 uint32_t pnn, pid;
366 int ret;
367 if (argc < 1) {
368 usage();
371 if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
372 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
373 return -1;
376 ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
377 if (ret == 0) {
378 printf("%u:%u exists\n", pnn, pid);
379 } else {
380 printf("%u:%u does not exist\n", pnn, pid);
382 return ret;
386 display statistics structure
388 static void show_statistics(struct ctdb_statistics *s, int show_header)
390 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
391 int i;
392 const char *prefix=NULL;
393 int preflen=0;
394 int tmp, days, hours, minutes, seconds;
395 const struct {
396 const char *name;
397 uint32_t offset;
398 } fields[] = {
399 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
400 STATISTICS_FIELD(num_clients),
401 STATISTICS_FIELD(frozen),
402 STATISTICS_FIELD(recovering),
403 STATISTICS_FIELD(num_recoveries),
404 STATISTICS_FIELD(client_packets_sent),
405 STATISTICS_FIELD(client_packets_recv),
406 STATISTICS_FIELD(node_packets_sent),
407 STATISTICS_FIELD(node_packets_recv),
408 STATISTICS_FIELD(keepalive_packets_sent),
409 STATISTICS_FIELD(keepalive_packets_recv),
410 STATISTICS_FIELD(node.req_call),
411 STATISTICS_FIELD(node.reply_call),
412 STATISTICS_FIELD(node.req_dmaster),
413 STATISTICS_FIELD(node.reply_dmaster),
414 STATISTICS_FIELD(node.reply_error),
415 STATISTICS_FIELD(node.req_message),
416 STATISTICS_FIELD(node.req_control),
417 STATISTICS_FIELD(node.reply_control),
418 STATISTICS_FIELD(client.req_call),
419 STATISTICS_FIELD(client.req_message),
420 STATISTICS_FIELD(client.req_control),
421 STATISTICS_FIELD(timeouts.call),
422 STATISTICS_FIELD(timeouts.control),
423 STATISTICS_FIELD(timeouts.traverse),
424 STATISTICS_FIELD(locks.num_calls),
425 STATISTICS_FIELD(locks.num_current),
426 STATISTICS_FIELD(locks.num_pending),
427 STATISTICS_FIELD(locks.num_failed),
428 STATISTICS_FIELD(total_calls),
429 STATISTICS_FIELD(pending_calls),
430 STATISTICS_FIELD(childwrite_calls),
431 STATISTICS_FIELD(pending_childwrite_calls),
432 STATISTICS_FIELD(memory_used),
433 STATISTICS_FIELD(max_hop_count),
434 STATISTICS_FIELD(total_ro_delegations),
435 STATISTICS_FIELD(total_ro_revokes),
438 tmp = s->statistics_current_time.tv_sec - s->statistics_start_time.tv_sec;
439 seconds = tmp%60;
440 tmp /= 60;
441 minutes = tmp%60;
442 tmp /= 60;
443 hours = tmp%24;
444 tmp /= 24;
445 days = tmp;
447 if (options.machinereadable){
448 if (show_header) {
449 printf("CTDB version:");
450 printf("Current time of statistics:");
451 printf("Statistics collected since:");
452 for (i=0;i<ARRAY_SIZE(fields);i++) {
453 printf("%s:", fields[i].name);
455 printf("num_reclock_ctdbd_latency:");
456 printf("min_reclock_ctdbd_latency:");
457 printf("avg_reclock_ctdbd_latency:");
458 printf("max_reclock_ctdbd_latency:");
460 printf("num_reclock_recd_latency:");
461 printf("min_reclock_recd_latency:");
462 printf("avg_reclock_recd_latency:");
463 printf("max_reclock_recd_latency:");
465 printf("num_call_latency:");
466 printf("min_call_latency:");
467 printf("avg_call_latency:");
468 printf("max_call_latency:");
470 printf("num_lockwait_latency:");
471 printf("min_lockwait_latency:");
472 printf("avg_lockwait_latency:");
473 printf("max_lockwait_latency:");
475 printf("num_childwrite_latency:");
476 printf("min_childwrite_latency:");
477 printf("avg_childwrite_latency:");
478 printf("max_childwrite_latency:");
479 printf("\n");
481 printf("%d:", CTDB_VERSION);
482 printf("%d:", (int)s->statistics_current_time.tv_sec);
483 printf("%d:", (int)s->statistics_start_time.tv_sec);
484 for (i=0;i<ARRAY_SIZE(fields);i++) {
485 printf("%d:", *(uint32_t *)(fields[i].offset+(uint8_t *)s));
487 printf("%d:", s->reclock.ctdbd.num);
488 printf("%.6f:", s->reclock.ctdbd.min);
489 printf("%.6f:", s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0);
490 printf("%.6f:", s->reclock.ctdbd.max);
492 printf("%d:", s->reclock.recd.num);
493 printf("%.6f:", s->reclock.recd.min);
494 printf("%.6f:", s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0);
495 printf("%.6f:", s->reclock.recd.max);
497 printf("%d:", s->call_latency.num);
498 printf("%.6f:", s->call_latency.min);
499 printf("%.6f:", s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0);
500 printf("%.6f:", s->call_latency.max);
502 printf("%d:", s->childwrite_latency.num);
503 printf("%.6f:", s->childwrite_latency.min);
504 printf("%.6f:", s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0);
505 printf("%.6f:", s->childwrite_latency.max);
506 printf("\n");
507 } else {
508 printf("CTDB version %u\n", CTDB_VERSION);
509 printf("Current time of statistics : %s", ctime(&s->statistics_current_time.tv_sec));
510 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&s->statistics_start_time.tv_sec));
512 for (i=0;i<ARRAY_SIZE(fields);i++) {
513 if (strchr(fields[i].name, '.')) {
514 preflen = strcspn(fields[i].name, ".")+1;
515 if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
516 prefix = fields[i].name;
517 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
519 } else {
520 preflen = 0;
522 printf(" %*s%-22s%*s%10u\n",
523 preflen?4:0, "",
524 fields[i].name+preflen,
525 preflen?0:4, "",
526 *(uint32_t *)(fields[i].offset+(uint8_t *)s));
528 printf(" hop_count_buckets:");
529 for (i=0;i<MAX_COUNT_BUCKETS;i++) {
530 printf(" %d", s->hop_count_bucket[i]);
532 printf("\n");
533 printf(" lock_buckets:");
534 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
535 printf(" %d", s->locks.buckets[i]);
537 printf("\n");
538 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n", "locks_latency MIN/AVG/MAX", s->locks.latency.min, s->locks.latency.num?s->locks.latency.total/s->locks.latency.num:0.0, s->locks.latency.max, s->locks.latency.num);
540 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n", "reclock_ctdbd MIN/AVG/MAX", s->reclock.ctdbd.min, s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0, s->reclock.ctdbd.max, s->reclock.ctdbd.num);
542 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n", "reclock_recd MIN/AVG/MAX", s->reclock.recd.min, s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0, s->reclock.recd.max, s->reclock.recd.num);
544 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n", "call_latency MIN/AVG/MAX", s->call_latency.min, s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0, s->call_latency.max, s->call_latency.num);
545 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n", "childwrite_latency MIN/AVG/MAX", s->childwrite_latency.min, s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0, s->childwrite_latency.max, s->childwrite_latency.num);
548 talloc_free(tmp_ctx);
552 display remote ctdb statistics combined from all nodes
554 static int control_statistics_all(struct ctdb_context *ctdb)
556 int ret, i;
557 struct ctdb_statistics statistics;
558 uint32_t *nodes;
559 uint32_t num_nodes;
561 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
562 CTDB_NO_MEMORY(ctdb, nodes);
564 ZERO_STRUCT(statistics);
566 for (i=0;i<num_nodes;i++) {
567 struct ctdb_statistics s1;
568 int j;
569 uint32_t *v1 = (uint32_t *)&s1;
570 uint32_t *v2 = (uint32_t *)&statistics;
571 uint32_t num_ints =
572 offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
573 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
574 if (ret != 0) {
575 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
576 return ret;
578 for (j=0;j<num_ints;j++) {
579 v2[j] += v1[j];
581 statistics.max_hop_count =
582 MAX(statistics.max_hop_count, s1.max_hop_count);
583 statistics.call_latency.max =
584 MAX(statistics.call_latency.max, s1.call_latency.max);
586 talloc_free(nodes);
587 printf("Gathered statistics for %u nodes\n", num_nodes);
588 show_statistics(&statistics, 1);
589 return 0;
593 display remote ctdb statistics
595 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
597 int ret;
598 struct ctdb_statistics statistics;
600 if (options.pnn == CTDB_BROADCAST_ALL) {
601 return control_statistics_all(ctdb);
604 ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
605 if (ret != 0) {
606 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
607 return ret;
609 show_statistics(&statistics, 1);
610 return 0;
615 reset remote ctdb statistics
617 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
619 int ret;
621 ret = ctdb_statistics_reset(ctdb, options.pnn);
622 if (ret != 0) {
623 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
624 return ret;
626 return 0;
631 display remote ctdb rolling statistics
633 static int control_stats(struct ctdb_context *ctdb, int argc, const char **argv)
635 int ret;
636 struct ctdb_statistics_wire *stats;
637 int i, num_records = -1;
639 assert_single_node_only();
641 if (argc ==1) {
642 num_records = atoi(argv[0]) - 1;
645 ret = ctdb_ctrl_getstathistory(ctdb, TIMELIMIT(), options.pnn, ctdb, &stats);
646 if (ret != 0) {
647 DEBUG(DEBUG_ERR, ("Unable to get rolling statistics from node %u\n", options.pnn));
648 return ret;
650 for (i=0;i<stats->num;i++) {
651 if (stats->stats[i].statistics_start_time.tv_sec == 0) {
652 continue;
654 show_statistics(&stats->stats[i], i==0);
655 if (i == num_records) {
656 break;
659 return 0;
664 display remote ctdb db statistics
666 static int control_dbstatistics(struct ctdb_context *ctdb, int argc, const char **argv)
668 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
669 struct ctdb_db_statistics *dbstat;
670 int i;
671 uint32_t db_id;
672 int num_hot_keys;
673 int ret;
675 if (argc < 1) {
676 usage();
679 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
680 return -1;
683 ret = ctdb_ctrl_dbstatistics(ctdb, options.pnn, db_id, tmp_ctx, &dbstat);
684 if (ret != 0) {
685 DEBUG(DEBUG_ERR,("Failed to read db statistics from node\n"));
686 talloc_free(tmp_ctx);
687 return -1;
690 printf("DB Statistics: %s\n", argv[0]);
691 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_delegations", 4, "",
692 dbstat->db_ro_delegations);
693 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_revokes", 4, "",
694 dbstat->db_ro_delegations);
695 printf(" %s\n", "locks");
696 printf(" %*s%-22s%*s%10u\n", 4, "", "total", 0, "",
697 dbstat->locks.num_calls);
698 printf(" %*s%-22s%*s%10u\n", 4, "", "failed", 0, "",
699 dbstat->locks.num_failed);
700 printf(" %*s%-22s%*s%10u\n", 4, "", "current", 0, "",
701 dbstat->locks.num_current);
702 printf(" %*s%-22s%*s%10u\n", 4, "", "pending", 0, "",
703 dbstat->locks.num_pending);
704 printf(" %s", "hop_count_buckets:");
705 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
706 printf(" %d", dbstat->hop_count_bucket[i]);
708 printf("\n");
709 printf(" %s", "lock_buckets:");
710 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
711 printf(" %d", dbstat->locks.buckets[i]);
713 printf("\n");
714 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
715 "locks_latency MIN/AVG/MAX",
716 dbstat->locks.latency.min,
717 (dbstat->locks.latency.num ?
718 dbstat->locks.latency.total /dbstat->locks.latency.num :
719 0.0),
720 dbstat->locks.latency.max,
721 dbstat->locks.latency.num);
722 num_hot_keys = 0;
723 for (i=0; i<dbstat->num_hot_keys; i++) {
724 if (dbstat->hot_keys[i].count > 0) {
725 num_hot_keys++;
728 dbstat->num_hot_keys = num_hot_keys;
730 printf(" Num Hot Keys: %d\n", dbstat->num_hot_keys);
731 for (i = 0; i < dbstat->num_hot_keys; i++) {
732 int j;
733 printf(" Count:%d Key:", dbstat->hot_keys[i].count);
734 for (j = 0; j < dbstat->hot_keys[i].key.dsize; j++) {
735 printf("%02x", dbstat->hot_keys[i].key.dptr[j]&0xff);
737 printf("\n");
740 talloc_free(tmp_ctx);
741 return 0;
745 display uptime of remote node
747 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
749 int ret;
750 struct ctdb_uptime *uptime = NULL;
751 int tmp, days, hours, minutes, seconds;
753 ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
754 if (ret != 0) {
755 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
756 return ret;
759 if (options.machinereadable){
760 printf(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
761 printf(":%u:%u:%u:%lf\n",
762 (unsigned int)uptime->current_time.tv_sec,
763 (unsigned int)uptime->ctdbd_start_time.tv_sec,
764 (unsigned int)uptime->last_recovery_finished.tv_sec,
765 timeval_delta(&uptime->last_recovery_finished,
766 &uptime->last_recovery_started)
768 return 0;
771 printf("Current time of node : %s", ctime(&uptime->current_time.tv_sec));
773 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
774 seconds = tmp%60;
775 tmp /= 60;
776 minutes = tmp%60;
777 tmp /= 60;
778 hours = tmp%24;
779 tmp /= 24;
780 days = tmp;
781 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
783 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
784 seconds = tmp%60;
785 tmp /= 60;
786 minutes = tmp%60;
787 tmp /= 60;
788 hours = tmp%24;
789 tmp /= 24;
790 days = tmp;
791 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
793 printf("Duration of last recovery/failover: %lf seconds\n",
794 timeval_delta(&uptime->last_recovery_finished,
795 &uptime->last_recovery_started));
797 return 0;
801 show the PNN of the current node
803 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
805 uint32_t mypnn;
807 mypnn = getpnn(ctdb);
809 printf("PNN:%d\n", mypnn);
810 return 0;
814 struct pnn_node {
815 struct pnn_node *next, *prev;
816 ctdb_sock_addr addr;
817 int pnn;
820 static struct pnn_node *read_pnn_node_file(TALLOC_CTX *mem_ctx,
821 const char *file)
823 int nlines;
824 char **lines;
825 int i, pnn;
826 struct pnn_node *pnn_nodes = NULL;
827 struct pnn_node *pnn_node;
829 lines = file_lines_load(file, &nlines, mem_ctx);
830 if (lines == NULL) {
831 return NULL;
833 for (i=0, pnn=0; i<nlines; i++) {
834 char *node;
836 node = lines[i];
837 /* strip leading spaces */
838 while((*node == ' ') || (*node == '\t')) {
839 node++;
841 if (*node == '#') {
842 pnn++;
843 continue;
845 if (strcmp(node, "") == 0) {
846 continue;
848 pnn_node = talloc(mem_ctx, struct pnn_node);
849 pnn_node->pnn = pnn++;
851 if (!parse_ip(node, NULL, 0, &pnn_node->addr)) {
852 DEBUG(DEBUG_ERR,
853 ("Invalid IP address '%s' in file %s\n",
854 node, file));
855 /* Caller will free mem_ctx */
856 return NULL;
859 DLIST_ADD_END(pnn_nodes, pnn_node, NULL);
862 return pnn_nodes;
865 static struct pnn_node *read_nodes_file(TALLOC_CTX *mem_ctx)
867 const char *nodes_list;
869 /* read the nodes file */
870 nodes_list = getenv("CTDB_NODES");
871 if (nodes_list == NULL) {
872 nodes_list = talloc_asprintf(mem_ctx, "%s/nodes",
873 getenv("CTDB_BASE"));
874 if (nodes_list == NULL) {
875 DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
876 exit(1);
880 return read_pnn_node_file(mem_ctx, nodes_list);
884 show the PNN of the current node
885 discover the pnn by loading the nodes file and try to bind to all
886 addresses one at a time until the ip address is found.
888 static int find_node_xpnn(void)
890 TALLOC_CTX *mem_ctx = talloc_new(NULL);
891 struct pnn_node *pnn_nodes;
892 struct pnn_node *pnn_node;
894 pnn_nodes = read_nodes_file(mem_ctx);
895 if (pnn_nodes == NULL) {
896 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
897 talloc_free(mem_ctx);
898 return -1;
901 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
902 if (ctdb_sys_have_ip(&pnn_node->addr)) {
903 talloc_free(mem_ctx);
904 return pnn_node->pnn;
908 printf("Failed to detect which PNN this node is\n");
909 talloc_free(mem_ctx);
910 return -1;
913 static int control_xpnn(struct ctdb_context *ctdb, int argc, const char **argv)
915 uint32_t pnn;
917 assert_single_node_only();
919 pnn = find_node_xpnn();
920 if (pnn == -1) {
921 return -1;
924 printf("PNN:%d\n", pnn);
925 return 0;
928 /* Helpers for ctdb status
930 static bool is_partially_online(struct ctdb_context *ctdb, struct ctdb_node_and_flags *node)
932 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
933 int j;
934 bool ret = false;
936 if (node->flags == 0) {
937 struct ctdb_control_get_ifaces *ifaces;
939 if (ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), node->pnn,
940 tmp_ctx, &ifaces) == 0) {
941 for (j=0; j < ifaces->num; j++) {
942 if (ifaces->ifaces[j].link_state != 0) {
943 continue;
945 ret = true;
946 break;
950 talloc_free(tmp_ctx);
952 return ret;
955 static void control_status_header_machine(void)
957 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
958 ":Inactive:PartiallyOnline:ThisNode:\n");
961 static int control_status_1_machine(struct ctdb_context *ctdb, int mypnn,
962 struct ctdb_node_and_flags *node)
964 printf(":%d:%s:%d:%d:%d:%d:%d:%d:%d:%c:\n", node->pnn,
965 ctdb_addr_to_str(&node->addr),
966 !!(node->flags&NODE_FLAGS_DISCONNECTED),
967 !!(node->flags&NODE_FLAGS_BANNED),
968 !!(node->flags&NODE_FLAGS_PERMANENTLY_DISABLED),
969 !!(node->flags&NODE_FLAGS_UNHEALTHY),
970 !!(node->flags&NODE_FLAGS_STOPPED),
971 !!(node->flags&NODE_FLAGS_INACTIVE),
972 is_partially_online(ctdb, node) ? 1 : 0,
973 (node->pnn == mypnn)?'Y':'N');
975 return node->flags;
978 static int control_status_1_human(struct ctdb_context *ctdb, int mypnn,
979 struct ctdb_node_and_flags *node)
981 printf("pnn:%d %-16s %s%s\n", node->pnn,
982 ctdb_addr_to_str(&node->addr),
983 is_partially_online(ctdb, node) ? "PARTIALLYONLINE" : pretty_print_flags(node->flags),
984 node->pnn == mypnn?" (THIS NODE)":"");
986 return node->flags;
990 display remote ctdb status
992 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
994 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
995 int i;
996 struct ctdb_vnn_map *vnnmap=NULL;
997 struct ctdb_node_map *nodemap=NULL;
998 uint32_t recmode, recmaster, mypnn;
999 int num_deleted_nodes = 0;
1000 int ret;
1002 mypnn = getpnn(ctdb);
1004 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1005 if (ret != 0) {
1006 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1007 talloc_free(tmp_ctx);
1008 return -1;
1011 if (options.machinereadable) {
1012 control_status_header_machine();
1013 for (i=0;i<nodemap->num;i++) {
1014 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1015 continue;
1017 (void) control_status_1_machine(ctdb, mypnn,
1018 &nodemap->nodes[i]);
1020 talloc_free(tmp_ctx);
1021 return 0;
1024 for (i=0; i<nodemap->num; i++) {
1025 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1026 num_deleted_nodes++;
1029 if (num_deleted_nodes == 0) {
1030 printf("Number of nodes:%d\n", nodemap->num);
1031 } else {
1032 printf("Number of nodes:%d (including %d deleted nodes)\n",
1033 nodemap->num, num_deleted_nodes);
1035 for(i=0;i<nodemap->num;i++){
1036 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1037 continue;
1039 (void) control_status_1_human(ctdb, mypnn, &nodemap->nodes[i]);
1042 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
1043 if (ret != 0) {
1044 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
1045 talloc_free(tmp_ctx);
1046 return -1;
1048 if (vnnmap->generation == INVALID_GENERATION) {
1049 printf("Generation:INVALID\n");
1050 } else {
1051 printf("Generation:%d\n",vnnmap->generation);
1053 printf("Size:%d\n",vnnmap->size);
1054 for(i=0;i<vnnmap->size;i++){
1055 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
1058 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmode);
1059 if (ret != 0) {
1060 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
1061 talloc_free(tmp_ctx);
1062 return -1;
1064 printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
1066 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmaster);
1067 if (ret != 0) {
1068 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1069 talloc_free(tmp_ctx);
1070 return -1;
1072 printf("Recovery master:%d\n",recmaster);
1074 talloc_free(tmp_ctx);
1075 return 0;
1078 static int control_nodestatus(struct ctdb_context *ctdb, int argc, const char **argv)
1080 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1081 int i, ret;
1082 struct ctdb_node_map *nodemap=NULL;
1083 uint32_t * nodes;
1084 uint32_t pnn_mode, mypnn;
1086 if (argc > 1) {
1087 usage();
1090 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1091 options.pnn, true, &nodes, &pnn_mode)) {
1092 return -1;
1095 if (options.machinereadable) {
1096 control_status_header_machine();
1097 } else if (pnn_mode == CTDB_BROADCAST_ALL) {
1098 printf("Number of nodes:%d\n", (int) talloc_array_length(nodes));
1101 mypnn = getpnn(ctdb);
1103 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1104 if (ret != 0) {
1105 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1106 talloc_free(tmp_ctx);
1107 return -1;
1110 ret = 0;
1112 for (i = 0; i < talloc_array_length(nodes); i++) {
1113 if (options.machinereadable) {
1114 ret |= control_status_1_machine(ctdb, mypnn,
1115 &nodemap->nodes[nodes[i]]);
1116 } else {
1117 ret |= control_status_1_human(ctdb, mypnn,
1118 &nodemap->nodes[nodes[i]]);
1122 talloc_free(tmp_ctx);
1123 return ret;
1126 static struct pnn_node *read_natgw_nodes_file(struct ctdb_context *ctdb,
1127 TALLOC_CTX *mem_ctx)
1129 const char *natgw_list;
1130 struct pnn_node *natgw_nodes = NULL;
1132 natgw_list = getenv("CTDB_NATGW_NODES");
1133 if (natgw_list == NULL) {
1134 natgw_list = talloc_asprintf(mem_ctx, "%s/natgw_nodes",
1135 getenv("CTDB_BASE"));
1136 if (natgw_list == NULL) {
1137 DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
1138 exit(1);
1141 /* The PNNs will be junk but they're not used */
1142 natgw_nodes = read_pnn_node_file(mem_ctx, natgw_list);
1143 if (natgw_nodes == NULL) {
1144 DEBUG(DEBUG_ERR,
1145 ("Failed to load natgw node list '%s'\n", natgw_list));
1147 return natgw_nodes;
1151 /* talloc off the existing nodemap... */
1152 static struct ctdb_node_map *talloc_nodemap(struct ctdb_node_map *nodemap)
1154 return talloc_zero_size(nodemap,
1155 offsetof(struct ctdb_node_map, nodes) +
1156 nodemap->num * sizeof(struct ctdb_node_and_flags));
1159 static struct ctdb_node_map *
1160 filter_nodemap_by_addrs(struct ctdb_context *ctdb,
1161 struct ctdb_node_map *nodemap,
1162 struct pnn_node *nodes)
1164 int i;
1165 struct pnn_node *n;
1166 struct ctdb_node_map *ret;
1168 ret = talloc_nodemap(nodemap);
1169 CTDB_NO_MEMORY_NULL(ctdb, ret);
1171 ret->num = 0;
1173 for (i = 0; i < nodemap->num; i++) {
1174 for(n = nodes; n != NULL ; n = n->next) {
1175 if (ctdb_same_ip(&n->addr,
1176 &nodemap->nodes[i].addr)) {
1177 break;
1180 if (n == NULL) {
1181 continue;
1184 ret->nodes[ret->num] = nodemap->nodes[i];
1185 ret->num++;
1188 return ret;
1191 static struct ctdb_node_map *
1192 filter_nodemap_by_capabilities(struct ctdb_context *ctdb,
1193 struct ctdb_node_map *nodemap,
1194 uint32_t required_capabilities,
1195 bool first_only)
1197 int i;
1198 uint32_t capabilities;
1199 struct ctdb_node_map *ret;
1201 ret = talloc_nodemap(nodemap);
1202 CTDB_NO_MEMORY_NULL(ctdb, ret);
1204 ret->num = 0;
1206 for (i = 0; i < nodemap->num; i++) {
1207 int res;
1209 /* Disconnected nodes have no capabilities! */
1210 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
1211 continue;
1214 res = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(),
1215 nodemap->nodes[i].pnn,
1216 &capabilities);
1217 if (res != 0) {
1218 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n",
1219 nodemap->nodes[i].pnn));
1220 talloc_free(ret);
1221 return NULL;
1223 if (!(capabilities & required_capabilities)) {
1224 continue;
1227 ret->nodes[ret->num] = nodemap->nodes[i];
1228 ret->num++;
1229 if (first_only) {
1230 break;
1234 return ret;
1237 static struct ctdb_node_map *
1238 filter_nodemap_by_flags(struct ctdb_context *ctdb,
1239 struct ctdb_node_map *nodemap,
1240 uint32_t flags_mask)
1242 int i;
1243 struct ctdb_node_map *ret;
1245 ret = talloc_nodemap(nodemap);
1246 CTDB_NO_MEMORY_NULL(ctdb, ret);
1248 ret->num = 0;
1250 for (i = 0; i < nodemap->num; i++) {
1251 if (nodemap->nodes[i].flags & flags_mask) {
1252 continue;
1255 ret->nodes[ret->num] = nodemap->nodes[i];
1256 ret->num++;
1259 return ret;
1263 display the list of nodes belonging to this natgw configuration
1265 static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **argv)
1267 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1268 int i, ret;
1269 struct pnn_node *natgw_nodes = NULL;
1270 struct ctdb_node_map *orig_nodemap=NULL;
1271 struct ctdb_node_map *nodemap;
1272 uint32_t mypnn, pnn;
1273 const char *ip;
1275 /* When we have some nodes that could be the NATGW, make a
1276 * series of attempts to find the first node that doesn't have
1277 * certain status flags set.
1279 uint32_t exclude_flags[] = {
1280 /* Look for a nice healthy node */
1281 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_UNHEALTHY,
1282 /* If not found, an UNHEALTHY/BANNED node will do */
1283 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED,
1284 /* If not found, a STOPPED node will do */
1285 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED,
1289 /* read the natgw nodes file into a linked list */
1290 natgw_nodes = read_natgw_nodes_file(ctdb, tmp_ctx);
1291 if (natgw_nodes == NULL) {
1292 ret = -1;
1293 goto done;
1296 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
1297 tmp_ctx, &orig_nodemap);
1298 if (ret != 0) {
1299 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node.\n"));
1300 talloc_free(tmp_ctx);
1301 return -1;
1304 /* Get a nodemap that includes only the nodes in the NATGW
1305 * group */
1306 nodemap = filter_nodemap_by_addrs(ctdb, orig_nodemap, natgw_nodes);
1307 if (nodemap == NULL) {
1308 ret = -1;
1309 goto done;
1312 ret = 2; /* matches ENOENT */
1313 pnn = -1;
1314 ip = "0.0.0.0";
1315 /* For each flag mask... */
1316 for (i = 0; exclude_flags[i] != 0; i++) {
1317 /* ... get a nodemap that excludes nodes with with
1318 * masked flags... */
1319 struct ctdb_node_map *t =
1320 filter_nodemap_by_flags(ctdb, nodemap,
1321 exclude_flags[i]);
1322 if (t == NULL) {
1323 /* No memory */
1324 ret = -1;
1325 goto done;
1327 if (t->num > 0) {
1328 /* ... and find the first node with the NATGW
1329 * capability */
1330 struct ctdb_node_map *n;
1331 n = filter_nodemap_by_capabilities(ctdb, t,
1332 CTDB_CAP_NATGW,
1333 true);
1334 if (n == NULL) {
1335 /* No memory */
1336 ret = -1;
1337 goto done;
1339 if (n->num > 0) {
1340 ret = 0;
1341 pnn = n->nodes[0].pnn;
1342 ip = ctdb_addr_to_str(&n->nodes[0].addr);
1343 break;
1346 talloc_free(t);
1349 if (options.machinereadable) {
1350 printf(":Node:IP:\n");
1351 printf(":%d:%s:\n", pnn, ip);
1352 } else {
1353 printf("%d %s\n", pnn, ip);
1356 /* print the pruned list of nodes belonging to this natgw list */
1357 mypnn = getpnn(ctdb);
1358 if (options.machinereadable) {
1359 control_status_header_machine();
1360 } else {
1361 printf("Number of nodes:%d\n", nodemap->num);
1363 for(i=0;i<nodemap->num;i++){
1364 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1365 continue;
1367 if (options.machinereadable) {
1368 control_status_1_machine(ctdb, mypnn, &(nodemap->nodes[i]));
1369 } else {
1370 control_status_1_human(ctdb, mypnn, &(nodemap->nodes[i]));
1374 done:
1375 talloc_free(tmp_ctx);
1376 return ret;
1380 display the status of the scripts for monitoring (or other events)
1382 static int control_one_scriptstatus(struct ctdb_context *ctdb,
1383 enum ctdb_eventscript_call type)
1385 struct ctdb_scripts_wire *script_status;
1386 int ret, i;
1388 ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
1389 if (ret != 0) {
1390 DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
1391 return ret;
1394 if (script_status == NULL) {
1395 if (!options.machinereadable) {
1396 printf("%s cycle never run\n",
1397 ctdb_eventscript_call_names[type]);
1399 return 0;
1402 if (!options.machinereadable) {
1403 int num_run = 0;
1404 for (i=0; i<script_status->num_scripts; i++) {
1405 if (script_status->scripts[i].status != -ENOEXEC) {
1406 num_run++;
1409 printf("%d scripts were executed last %s cycle\n",
1410 num_run,
1411 ctdb_eventscript_call_names[type]);
1413 for (i=0; i<script_status->num_scripts; i++) {
1414 const char *status = NULL;
1416 switch (script_status->scripts[i].status) {
1417 case -ETIME:
1418 status = "TIMEDOUT";
1419 break;
1420 case -ENOEXEC:
1421 status = "DISABLED";
1422 break;
1423 case 0:
1424 status = "OK";
1425 break;
1426 default:
1427 if (script_status->scripts[i].status > 0)
1428 status = "ERROR";
1429 break;
1431 if (options.machinereadable) {
1432 printf(":%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
1433 ctdb_eventscript_call_names[type],
1434 script_status->scripts[i].name,
1435 script_status->scripts[i].status,
1436 status,
1437 (long)script_status->scripts[i].start.tv_sec,
1438 (long)script_status->scripts[i].start.tv_usec,
1439 (long)script_status->scripts[i].finished.tv_sec,
1440 (long)script_status->scripts[i].finished.tv_usec,
1441 script_status->scripts[i].output);
1442 continue;
1444 if (status)
1445 printf("%-20s Status:%s ",
1446 script_status->scripts[i].name, status);
1447 else
1448 /* Some other error, eg from stat. */
1449 printf("%-20s Status:CANNOT RUN (%s)",
1450 script_status->scripts[i].name,
1451 strerror(-script_status->scripts[i].status));
1453 if (script_status->scripts[i].status >= 0) {
1454 printf("Duration:%.3lf ",
1455 timeval_delta(&script_status->scripts[i].finished,
1456 &script_status->scripts[i].start));
1458 if (script_status->scripts[i].status != -ENOEXEC) {
1459 printf("%s",
1460 ctime(&script_status->scripts[i].start.tv_sec));
1461 if (script_status->scripts[i].status != 0) {
1462 printf(" OUTPUT:%s\n",
1463 script_status->scripts[i].output);
1465 } else {
1466 printf("\n");
1469 return 0;
1473 static int control_scriptstatus(struct ctdb_context *ctdb,
1474 int argc, const char **argv)
1476 int ret;
1477 enum ctdb_eventscript_call type, min, max;
1478 const char *arg;
1480 if (argc > 1) {
1481 DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
1482 return -1;
1485 if (argc == 0)
1486 arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
1487 else
1488 arg = argv[0];
1490 for (type = 0; type < CTDB_EVENT_MAX; type++) {
1491 if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
1492 min = type;
1493 max = type+1;
1494 break;
1497 if (type == CTDB_EVENT_MAX) {
1498 if (strcmp(arg, "all") == 0) {
1499 min = 0;
1500 max = CTDB_EVENT_MAX;
1501 } else {
1502 DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
1503 return -1;
1507 if (options.machinereadable) {
1508 printf(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1511 for (type = min; type < max; type++) {
1512 ret = control_one_scriptstatus(ctdb, type);
1513 if (ret != 0) {
1514 return ret;
1518 return 0;
1522 enable an eventscript
1524 static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1526 int ret;
1528 if (argc < 1) {
1529 usage();
1532 ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1533 if (ret != 0) {
1534 DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
1535 return ret;
1538 return 0;
1542 disable an eventscript
1544 static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1546 int ret;
1548 if (argc < 1) {
1549 usage();
1552 ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1553 if (ret != 0) {
1554 DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
1555 return ret;
1558 return 0;
1562 display the pnn of the recovery master
1564 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1566 uint32_t recmaster;
1567 int ret;
1569 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
1570 if (ret != 0) {
1571 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1572 return -1;
1574 printf("%d\n",recmaster);
1576 return 0;
1580 add a tickle to a public address
1582 static int control_add_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1584 struct ctdb_tcp_connection t;
1585 TDB_DATA data;
1586 int ret;
1588 assert_single_node_only();
1590 if (argc < 2) {
1591 usage();
1594 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1595 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1596 return -1;
1598 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1599 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1600 return -1;
1603 data.dptr = (uint8_t *)&t;
1604 data.dsize = sizeof(t);
1606 /* tell all nodes about this tcp connection */
1607 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE,
1608 0, data, ctdb, NULL, NULL, NULL, NULL);
1609 if (ret != 0) {
1610 DEBUG(DEBUG_ERR,("Failed to add tickle\n"));
1611 return -1;
1614 return 0;
1619 delete a tickle from a node
1621 static int control_del_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1623 struct ctdb_tcp_connection t;
1624 TDB_DATA data;
1625 int ret;
1627 assert_single_node_only();
1629 if (argc < 2) {
1630 usage();
1633 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1634 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1635 return -1;
1637 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1638 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1639 return -1;
1642 data.dptr = (uint8_t *)&t;
1643 data.dsize = sizeof(t);
1645 /* tell all nodes about this tcp connection */
1646 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_REMOVE,
1647 0, data, ctdb, NULL, NULL, NULL, NULL);
1648 if (ret != 0) {
1649 DEBUG(DEBUG_ERR,("Failed to remove tickle\n"));
1650 return -1;
1653 return 0;
1658 get a list of all tickles for this pnn
1660 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
1662 struct ctdb_control_tcp_tickle_list *list;
1663 ctdb_sock_addr addr;
1664 int i, ret;
1665 unsigned port = 0;
1667 assert_single_node_only();
1669 if (argc < 1) {
1670 usage();
1673 if (argc == 2) {
1674 port = atoi(argv[1]);
1677 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1678 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1679 return -1;
1682 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
1683 if (ret == -1) {
1684 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
1685 return -1;
1688 if (options.machinereadable){
1689 printf(":source ip:port:destination ip:port:\n");
1690 for (i=0;i<list->tickles.num;i++) {
1691 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1692 continue;
1694 printf(":%s:%u", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1695 printf(":%s:%u:\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
1697 } else {
1698 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
1699 printf("Num tickles:%u\n", list->tickles.num);
1700 for (i=0;i<list->tickles.num;i++) {
1701 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1702 continue;
1704 printf("SRC: %s:%u ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1705 printf("DST: %s:%u\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
1709 talloc_free(list);
1711 return 0;
1715 static int move_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1717 struct ctdb_all_public_ips *ips;
1718 struct ctdb_public_ip ip;
1719 int i, ret;
1720 uint32_t *nodes;
1721 uint32_t disable_time;
1722 TDB_DATA data;
1723 struct ctdb_node_map *nodemap=NULL;
1724 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1726 disable_time = 30;
1727 data.dptr = (uint8_t*)&disable_time;
1728 data.dsize = sizeof(disable_time);
1729 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1730 if (ret != 0) {
1731 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1732 return -1;
1737 /* read the public ip list from the node */
1738 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
1739 if (ret != 0) {
1740 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
1741 talloc_free(tmp_ctx);
1742 return -1;
1745 for (i=0;i<ips->num;i++) {
1746 if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
1747 break;
1750 if (i==ips->num) {
1751 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
1752 pnn, ctdb_addr_to_str(addr)));
1753 talloc_free(tmp_ctx);
1754 return -1;
1757 ip.pnn = pnn;
1758 ip.addr = *addr;
1760 data.dptr = (uint8_t *)&ip;
1761 data.dsize = sizeof(ip);
1763 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1764 if (ret != 0) {
1765 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1766 talloc_free(tmp_ctx);
1767 return ret;
1770 nodes = list_of_nodes(ctdb, nodemap, tmp_ctx, NODE_FLAGS_INACTIVE, pnn);
1771 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1772 nodes, 0,
1773 LONGTIMELIMIT(),
1774 false, data,
1775 NULL, NULL,
1776 NULL);
1777 if (ret != 0) {
1778 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1779 talloc_free(tmp_ctx);
1780 return -1;
1783 ret = ctdb_ctrl_takeover_ip(ctdb, LONGTIMELIMIT(), pnn, &ip);
1784 if (ret != 0) {
1785 DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
1786 talloc_free(tmp_ctx);
1787 return -1;
1790 /* update the recovery daemon so it now knows to expect the new
1791 node assignment for this ip.
1793 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_RECD_UPDATE_IP, data);
1794 if (ret != 0) {
1795 DEBUG(DEBUG_ERR,("Failed to send message to update the ip on the recovery master.\n"));
1796 return -1;
1799 talloc_free(tmp_ctx);
1800 return 0;
1805 * scans all other nodes and returns a pnn for another node that can host this
1806 * ip address or -1
1808 static int
1809 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1811 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1812 struct ctdb_all_public_ips *ips;
1813 struct ctdb_node_map *nodemap=NULL;
1814 int i, j, ret;
1816 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1817 if (ret != 0) {
1818 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1819 talloc_free(tmp_ctx);
1820 return ret;
1823 for(i=0;i<nodemap->num;i++){
1824 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1825 continue;
1827 if (nodemap->nodes[i].pnn == options.pnn) {
1828 continue;
1831 /* read the public ip list from this node */
1832 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
1833 if (ret != 0) {
1834 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1835 return -1;
1838 for (j=0;j<ips->num;j++) {
1839 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1840 talloc_free(tmp_ctx);
1841 return nodemap->nodes[i].pnn;
1844 talloc_free(ips);
1847 talloc_free(tmp_ctx);
1848 return -1;
1851 /* If pnn is -1 then try to find a node to move IP to... */
1852 static bool try_moveip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1854 bool pnn_specified = (pnn == -1 ? false : true);
1855 int retries = 0;
1857 while (retries < 5) {
1858 if (!pnn_specified) {
1859 pnn = find_other_host_for_public_ip(ctdb, addr);
1860 if (pnn == -1) {
1861 return false;
1863 DEBUG(DEBUG_NOTICE,
1864 ("Trying to move public IP to node %u\n", pnn));
1867 if (move_ip(ctdb, addr, pnn) == 0) {
1868 return true;
1871 sleep(3);
1872 retries++;
1875 return false;
1880 move/failover an ip address to a specific node
1882 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
1884 uint32_t pnn;
1885 ctdb_sock_addr addr;
1887 assert_single_node_only();
1889 if (argc < 2) {
1890 usage();
1891 return -1;
1894 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1895 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1896 return -1;
1900 if (sscanf(argv[1], "%u", &pnn) != 1) {
1901 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
1902 return -1;
1905 if (!try_moveip(ctdb, &addr, pnn)) {
1906 DEBUG(DEBUG_ERR,("Failed to move IP to node %d.\n", pnn));
1907 return -1;
1910 return 0;
1913 static int rebalance_node(struct ctdb_context *ctdb, uint32_t pnn)
1915 TDB_DATA data;
1917 data.dptr = (uint8_t *)&pnn;
1918 data.dsize = sizeof(uint32_t);
1919 if (ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_REBALANCE_NODE, data) != 0) {
1920 DEBUG(DEBUG_ERR,
1921 ("Failed to send message to force node %u to be a rebalancing target\n",
1922 pnn));
1923 return -1;
1926 return 0;
1931 rebalance a node by setting it to allow failback and triggering a
1932 takeover run
1934 static int control_rebalancenode(struct ctdb_context *ctdb, int argc, const char **argv)
1936 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1937 uint32_t *nodes;
1938 uint32_t pnn_mode;
1939 int i, ret;
1941 assert_single_node_only();
1943 if (argc > 1) {
1944 usage();
1947 /* Determine the nodes where IPs need to be reloaded */
1948 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1949 options.pnn, true, &nodes, &pnn_mode)) {
1950 ret = -1;
1951 goto done;
1954 for (i = 0; i < talloc_array_length(nodes); i++) {
1955 if (!rebalance_node(ctdb, nodes[i])) {
1956 ret = -1;
1960 done:
1961 talloc_free(tmp_ctx);
1962 return ret;
1965 static int rebalance_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1967 struct ctdb_public_ip ip;
1968 int ret;
1969 uint32_t *nodes;
1970 uint32_t disable_time;
1971 TDB_DATA data;
1972 struct ctdb_node_map *nodemap=NULL;
1973 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1975 disable_time = 30;
1976 data.dptr = (uint8_t*)&disable_time;
1977 data.dsize = sizeof(disable_time);
1978 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1979 if (ret != 0) {
1980 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1981 return -1;
1984 ip.pnn = -1;
1985 ip.addr = *addr;
1987 data.dptr = (uint8_t *)&ip;
1988 data.dsize = sizeof(ip);
1990 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1991 if (ret != 0) {
1992 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1993 talloc_free(tmp_ctx);
1994 return ret;
1997 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
1998 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1999 nodes, 0,
2000 LONGTIMELIMIT(),
2001 false, data,
2002 NULL, NULL,
2003 NULL);
2004 if (ret != 0) {
2005 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
2006 talloc_free(tmp_ctx);
2007 return -1;
2010 talloc_free(tmp_ctx);
2011 return 0;
2015 release an ip form all nodes and have it re-assigned by recd
2017 static int control_rebalanceip(struct ctdb_context *ctdb, int argc, const char **argv)
2019 ctdb_sock_addr addr;
2021 assert_single_node_only();
2023 if (argc < 1) {
2024 usage();
2025 return -1;
2028 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2029 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2030 return -1;
2033 if (rebalance_ip(ctdb, &addr) != 0) {
2034 DEBUG(DEBUG_ERR,("Error when trying to reassign ip\n"));
2035 return -1;
2038 return 0;
2041 static int getips_store_callback(void *param, void *data)
2043 struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
2044 struct ctdb_all_public_ips *ips = param;
2045 int i;
2047 i = ips->num++;
2048 ips->ips[i].pnn = node_ip->pnn;
2049 ips->ips[i].addr = node_ip->addr;
2050 return 0;
2053 static int getips_count_callback(void *param, void *data)
2055 uint32_t *count = param;
2057 (*count)++;
2058 return 0;
2061 #define IP_KEYLEN 4
2062 static uint32_t *ip_key(ctdb_sock_addr *ip)
2064 static uint32_t key[IP_KEYLEN];
2066 bzero(key, sizeof(key));
2068 switch (ip->sa.sa_family) {
2069 case AF_INET:
2070 key[0] = ip->ip.sin_addr.s_addr;
2071 break;
2072 case AF_INET6: {
2073 uint32_t *s6_a32 = (uint32_t *)&(ip->ip6.sin6_addr.s6_addr);
2074 key[0] = s6_a32[3];
2075 key[1] = s6_a32[2];
2076 key[2] = s6_a32[1];
2077 key[3] = s6_a32[0];
2078 break;
2080 default:
2081 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
2082 return key;
2085 return key;
2088 static void *add_ip_callback(void *parm, void *data)
2090 return parm;
2093 static int
2094 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
2096 struct ctdb_all_public_ips *tmp_ips;
2097 struct ctdb_node_map *nodemap=NULL;
2098 trbt_tree_t *ip_tree;
2099 int i, j, len, ret;
2100 uint32_t count;
2102 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2103 if (ret != 0) {
2104 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2105 return ret;
2108 ip_tree = trbt_create(tmp_ctx, 0);
2110 for(i=0;i<nodemap->num;i++){
2111 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
2112 continue;
2114 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
2115 continue;
2118 /* read the public ip list from this node */
2119 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
2120 if (ret != 0) {
2121 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
2122 return -1;
2125 for (j=0; j<tmp_ips->num;j++) {
2126 struct ctdb_public_ip *node_ip;
2128 node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
2129 node_ip->pnn = tmp_ips->ips[j].pnn;
2130 node_ip->addr = tmp_ips->ips[j].addr;
2132 trbt_insertarray32_callback(ip_tree,
2133 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
2134 add_ip_callback,
2135 node_ip);
2137 talloc_free(tmp_ips);
2140 /* traverse */
2141 count = 0;
2142 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
2144 len = offsetof(struct ctdb_all_public_ips, ips) +
2145 count*sizeof(struct ctdb_public_ip);
2146 tmp_ips = talloc_zero_size(tmp_ctx, len);
2147 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
2149 *ips = tmp_ips;
2151 return 0;
2155 static void ctdb_every_second(struct event_context *ev, struct timed_event *te, struct timeval t, void *p)
2157 struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
2159 event_add_timed(ctdb->ev, ctdb,
2160 timeval_current_ofs(1, 0),
2161 ctdb_every_second, ctdb);
2164 struct srvid_reply_handler_data {
2165 bool done;
2166 bool wait_for_all;
2167 uint32_t *nodes;
2168 const char *srvid_str;
2171 static void srvid_broadcast_reply_handler(struct ctdb_context *ctdb,
2172 uint64_t srvid,
2173 TDB_DATA data,
2174 void *private_data)
2176 struct srvid_reply_handler_data *d =
2177 (struct srvid_reply_handler_data *)private_data;
2178 int i;
2179 int32_t ret;
2181 if (data.dsize != sizeof(ret)) {
2182 DEBUG(DEBUG_ERR, (__location__ " Wrong reply size\n"));
2183 return;
2186 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
2187 ret = *(int32_t *)data.dptr;
2188 if (ret < 0) {
2189 DEBUG(DEBUG_ERR,
2190 ("%s failed with result %d\n", d->srvid_str, ret));
2191 return;
2194 if (!d->wait_for_all) {
2195 d->done = true;
2196 return;
2199 /* Wait for all replies */
2200 d->done = true;
2201 for (i = 0; i < talloc_array_length(d->nodes); i++) {
2202 if (d->nodes[i] == ret) {
2203 DEBUG(DEBUG_INFO,
2204 ("%s reply received from node %u\n",
2205 d->srvid_str, ret));
2206 d->nodes[i] = -1;
2208 if (d->nodes[i] != -1) {
2209 /* Found a node that hasn't yet replied */
2210 d->done = false;
2215 /* Broadcast the given SRVID to all connected nodes. Wait for 1 reply
2216 * or replies from all connected nodes. arg is the data argument to
2217 * pass in the srvid_request structure - pass 0 if this isn't needed.
2219 static int srvid_broadcast(struct ctdb_context *ctdb,
2220 uint64_t srvid, uint32_t *arg,
2221 const char *srvid_str, bool wait_for_all)
2223 int ret;
2224 TDB_DATA data;
2225 uint32_t pnn;
2226 uint64_t reply_srvid;
2227 struct srvid_request request;
2228 struct srvid_request_data request_data;
2229 struct srvid_reply_handler_data reply_data;
2230 struct timeval tv;
2232 ZERO_STRUCT(request);
2234 /* Time ticks to enable timeouts to be processed */
2235 event_add_timed(ctdb->ev, ctdb,
2236 timeval_current_ofs(1, 0),
2237 ctdb_every_second, ctdb);
2239 pnn = ctdb_get_pnn(ctdb);
2240 reply_srvid = getpid();
2242 if (arg == NULL) {
2243 request.pnn = pnn;
2244 request.srvid = reply_srvid;
2246 data.dptr = (uint8_t *)&request;
2247 data.dsize = sizeof(request);
2248 } else {
2249 request_data.pnn = pnn;
2250 request_data.srvid = reply_srvid;
2251 request_data.data = *arg;
2253 data.dptr = (uint8_t *)&request_data;
2254 data.dsize = sizeof(request_data);
2257 /* Register message port for reply from recovery master */
2258 ctdb_client_set_message_handler(ctdb, reply_srvid,
2259 srvid_broadcast_reply_handler,
2260 &reply_data);
2262 reply_data.wait_for_all = wait_for_all;
2263 reply_data.nodes = NULL;
2264 reply_data.srvid_str = srvid_str;
2266 again:
2267 reply_data.done = false;
2269 if (wait_for_all) {
2270 struct ctdb_node_map *nodemap;
2272 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
2273 CTDB_CURRENT_NODE, ctdb, &nodemap);
2274 if (ret != 0) {
2275 DEBUG(DEBUG_ERR,
2276 ("Unable to get nodemap from current node, try again\n"));
2277 sleep(1);
2278 goto again;
2281 if (reply_data.nodes != NULL) {
2282 talloc_free(reply_data.nodes);
2284 reply_data.nodes = list_of_connected_nodes(ctdb, nodemap,
2285 NULL, true);
2287 talloc_free(nodemap);
2290 /* Send to all connected nodes. Only recmaster replies */
2291 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED,
2292 srvid, data);
2293 if (ret != 0) {
2294 /* This can only happen if the socket is closed and
2295 * there's no way to recover from that, so don't try
2296 * again.
2298 DEBUG(DEBUG_ERR,
2299 ("Failed to send %s request to connected nodes\n",
2300 srvid_str));
2301 return -1;
2304 tv = timeval_current();
2305 /* This loop terminates the reply is received */
2306 while (timeval_elapsed(&tv) < 5.0 && !reply_data.done) {
2307 event_loop_once(ctdb->ev);
2310 if (!reply_data.done) {
2311 DEBUG(DEBUG_NOTICE,
2312 ("Still waiting for confirmation of %s\n", srvid_str));
2313 sleep(1);
2314 goto again;
2317 ctdb_client_remove_message_handler(ctdb, reply_srvid, &reply_data);
2319 talloc_free(reply_data.nodes);
2321 return 0;
2324 static int ipreallocate(struct ctdb_context *ctdb)
2326 return srvid_broadcast(ctdb, CTDB_SRVID_TAKEOVER_RUN, NULL,
2327 "IP reallocation", false);
2331 static int control_ipreallocate(struct ctdb_context *ctdb, int argc, const char **argv)
2333 return ipreallocate(ctdb);
2337 add a public ip address to a node
2339 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
2341 int i, ret;
2342 int len, retries = 0;
2343 unsigned mask;
2344 ctdb_sock_addr addr;
2345 struct ctdb_control_ip_iface *pub;
2346 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2347 struct ctdb_all_public_ips *ips;
2350 if (argc != 2) {
2351 talloc_free(tmp_ctx);
2352 usage();
2355 if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
2356 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
2357 talloc_free(tmp_ctx);
2358 return -1;
2361 /* read the public ip list from the node */
2362 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2363 if (ret != 0) {
2364 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", options.pnn));
2365 talloc_free(tmp_ctx);
2366 return -1;
2368 for (i=0;i<ips->num;i++) {
2369 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2370 DEBUG(DEBUG_ERR,("Can not add ip to node. Node already hosts this ip\n"));
2371 return 0;
2377 /* Dont timeout. This command waits for an ip reallocation
2378 which sometimes can take wuite a while if there has
2379 been a recent recovery
2381 alarm(0);
2383 len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
2384 pub = talloc_size(tmp_ctx, len);
2385 CTDB_NO_MEMORY(ctdb, pub);
2387 pub->addr = addr;
2388 pub->mask = mask;
2389 pub->len = strlen(argv[1])+1;
2390 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
2392 do {
2393 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
2394 if (ret != 0) {
2395 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options.pnn));
2396 sleep(3);
2397 retries++;
2399 } while (retries < 5 && ret != 0);
2400 if (ret != 0) {
2401 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Giving up.\n", options.pnn));
2402 talloc_free(tmp_ctx);
2403 return ret;
2406 if (rebalance_node(ctdb, options.pnn) != 0) {
2407 DEBUG(DEBUG_ERR,("Error when trying to rebalance node\n"));
2408 return ret;
2411 talloc_free(tmp_ctx);
2412 return 0;
2416 add a public ip address to a node
2418 static int control_ipiface(struct ctdb_context *ctdb, int argc, const char **argv)
2420 ctdb_sock_addr addr;
2422 if (argc != 1) {
2423 usage();
2426 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2427 printf("Badly formed ip : %s\n", argv[0]);
2428 return -1;
2431 printf("IP on interface %s\n", ctdb_sys_find_ifname(&addr));
2433 return 0;
2436 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
2438 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
2440 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2441 struct ctdb_node_map *nodemap=NULL;
2442 struct ctdb_all_public_ips *ips;
2443 int ret, i, j;
2445 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2446 if (ret != 0) {
2447 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
2448 return ret;
2451 /* remove it from the nodes that are not hosting the ip currently */
2452 for(i=0;i<nodemap->num;i++){
2453 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2454 continue;
2456 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2457 if (ret != 0) {
2458 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2459 continue;
2462 for (j=0;j<ips->num;j++) {
2463 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2464 break;
2467 if (j==ips->num) {
2468 continue;
2471 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
2472 continue;
2475 options.pnn = nodemap->nodes[i].pnn;
2476 control_delip(ctdb, argc, argv);
2480 /* remove it from every node (also the one hosting it) */
2481 for(i=0;i<nodemap->num;i++){
2482 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2483 continue;
2485 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2486 if (ret != 0) {
2487 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2488 continue;
2491 for (j=0;j<ips->num;j++) {
2492 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2493 break;
2496 if (j==ips->num) {
2497 continue;
2500 options.pnn = nodemap->nodes[i].pnn;
2501 control_delip(ctdb, argc, argv);
2504 talloc_free(tmp_ctx);
2505 return 0;
2509 delete a public ip address from a node
2511 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
2513 int i, ret;
2514 ctdb_sock_addr addr;
2515 struct ctdb_control_ip_iface pub;
2516 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2517 struct ctdb_all_public_ips *ips;
2519 if (argc != 1) {
2520 talloc_free(tmp_ctx);
2521 usage();
2524 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2525 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2526 return -1;
2529 if (options.pnn == CTDB_BROADCAST_ALL) {
2530 return control_delip_all(ctdb, argc, argv, &addr);
2533 pub.addr = addr;
2534 pub.mask = 0;
2535 pub.len = 0;
2537 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2538 if (ret != 0) {
2539 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
2540 talloc_free(tmp_ctx);
2541 return ret;
2544 for (i=0;i<ips->num;i++) {
2545 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2546 break;
2550 if (i==ips->num) {
2551 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
2552 ctdb_addr_to_str(&addr)));
2553 talloc_free(tmp_ctx);
2554 return -1;
2557 /* This is an optimisation. If this node is hosting the IP
2558 * then try to move it somewhere else without invoking a full
2559 * takeover run. We don't care if this doesn't work!
2561 if (ips->ips[i].pnn == options.pnn) {
2562 (void) try_moveip(ctdb, &addr, -1);
2565 ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
2566 if (ret != 0) {
2567 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
2568 talloc_free(tmp_ctx);
2569 return ret;
2572 talloc_free(tmp_ctx);
2573 return 0;
2576 static int kill_tcp_from_file(struct ctdb_context *ctdb,
2577 int argc, const char **argv)
2579 struct ctdb_control_killtcp *killtcp;
2580 int max_entries, current, i;
2581 struct timeval timeout;
2582 char line[128], src[128], dst[128];
2583 int linenum;
2584 TDB_DATA data;
2585 struct client_async_data *async_data;
2586 struct ctdb_client_control_state *state;
2588 if (argc != 0) {
2589 usage();
2592 linenum = 1;
2593 killtcp = NULL;
2594 max_entries = 0;
2595 current = 0;
2596 while (!feof(stdin)) {
2597 if (fgets(line, sizeof(line), stdin) == NULL) {
2598 continue;
2601 /* Silently skip empty lines */
2602 if (line[0] == '\n') {
2603 continue;
2606 if (sscanf(line, "%s %s\n", src, dst) != 2) {
2607 DEBUG(DEBUG_ERR, ("Bad line [%d]: '%s'\n",
2608 linenum, line));
2609 talloc_free(killtcp);
2610 return -1;
2613 if (current >= max_entries) {
2614 max_entries += 1024;
2615 killtcp = talloc_realloc(ctdb, killtcp,
2616 struct ctdb_control_killtcp,
2617 max_entries);
2618 CTDB_NO_MEMORY(ctdb, killtcp);
2621 if (!parse_ip_port(src, &killtcp[current].src_addr)) {
2622 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2623 linenum, src));
2624 talloc_free(killtcp);
2625 return -1;
2628 if (!parse_ip_port(dst, &killtcp[current].dst_addr)) {
2629 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2630 linenum, dst));
2631 talloc_free(killtcp);
2632 return -1;
2635 current++;
2638 async_data = talloc_zero(ctdb, struct client_async_data);
2639 if (async_data == NULL) {
2640 talloc_free(killtcp);
2641 return -1;
2644 for (i = 0; i < current; i++) {
2646 data.dsize = sizeof(struct ctdb_control_killtcp);
2647 data.dptr = (unsigned char *)&killtcp[i];
2649 timeout = TIMELIMIT();
2650 state = ctdb_control_send(ctdb, options.pnn, 0,
2651 CTDB_CONTROL_KILL_TCP, 0, data,
2652 async_data, &timeout, NULL);
2654 if (state == NULL) {
2655 DEBUG(DEBUG_ERR,
2656 ("Failed to call async killtcp control to node %u\n",
2657 options.pnn));
2658 talloc_free(killtcp);
2659 return -1;
2662 ctdb_client_async_add(async_data, state);
2665 if (ctdb_client_async_wait(ctdb, async_data) != 0) {
2666 DEBUG(DEBUG_ERR,("killtcp failed\n"));
2667 talloc_free(killtcp);
2668 return -1;
2671 talloc_free(killtcp);
2672 return 0;
2677 kill a tcp connection
2679 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2681 int ret;
2682 struct ctdb_control_killtcp killtcp;
2684 assert_single_node_only();
2686 if (argc == 0) {
2687 return kill_tcp_from_file(ctdb, argc, argv);
2690 if (argc < 2) {
2691 usage();
2694 if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
2695 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2696 return -1;
2699 if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
2700 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2701 return -1;
2704 ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
2705 if (ret != 0) {
2706 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
2707 return ret;
2710 return 0;
2715 send a gratious arp
2717 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
2719 int ret;
2720 ctdb_sock_addr addr;
2722 assert_single_node_only();
2724 if (argc < 2) {
2725 usage();
2728 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2729 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
2730 return -1;
2733 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
2734 if (ret != 0) {
2735 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
2736 return ret;
2739 return 0;
2743 register a server id
2745 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2747 int ret;
2748 struct ctdb_server_id server_id;
2750 if (argc < 3) {
2751 usage();
2754 server_id.pnn = strtoul(argv[0], NULL, 0);
2755 server_id.type = strtoul(argv[1], NULL, 0);
2756 server_id.server_id = strtoul(argv[2], NULL, 0);
2758 ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
2759 if (ret != 0) {
2760 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
2761 return ret;
2763 DEBUG(DEBUG_ERR,("Srvid registered. Sleeping for 999 seconds\n"));
2764 sleep(999);
2765 return -1;
2769 unregister a server id
2771 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2773 int ret;
2774 struct ctdb_server_id server_id;
2776 if (argc < 3) {
2777 usage();
2780 server_id.pnn = strtoul(argv[0], NULL, 0);
2781 server_id.type = strtoul(argv[1], NULL, 0);
2782 server_id.server_id = strtoul(argv[2], NULL, 0);
2784 ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
2785 if (ret != 0) {
2786 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
2787 return ret;
2789 return -1;
2793 check if a server id exists
2795 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2797 uint32_t status;
2798 int ret;
2799 struct ctdb_server_id server_id;
2801 if (argc < 3) {
2802 usage();
2805 server_id.pnn = strtoul(argv[0], NULL, 0);
2806 server_id.type = strtoul(argv[1], NULL, 0);
2807 server_id.server_id = strtoul(argv[2], NULL, 0);
2809 ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
2810 if (ret != 0) {
2811 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
2812 return ret;
2815 if (status) {
2816 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
2817 } else {
2818 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
2820 return 0;
2824 get a list of all server ids that are registered on a node
2826 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
2828 int i, ret;
2829 struct ctdb_server_id_list *server_ids;
2831 ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
2832 if (ret != 0) {
2833 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
2834 return ret;
2837 for (i=0; i<server_ids->num; i++) {
2838 printf("Server id %d:%d:%d\n",
2839 server_ids->server_ids[i].pnn,
2840 server_ids->server_ids[i].type,
2841 server_ids->server_ids[i].server_id);
2844 return -1;
2848 check if a server id exists
2850 static int check_srvids(struct ctdb_context *ctdb, int argc, const char **argv)
2852 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
2853 uint64_t *ids;
2854 uint8_t *result;
2855 int i;
2857 if (argc < 1) {
2858 talloc_free(tmp_ctx);
2859 usage();
2862 ids = talloc_array(tmp_ctx, uint64_t, argc);
2863 result = talloc_array(tmp_ctx, uint8_t, argc);
2865 for (i = 0; i < argc; i++) {
2866 ids[i] = strtoull(argv[i], NULL, 0);
2869 if (!ctdb_client_check_message_handlers(ctdb, ids, argc, result)) {
2870 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n",
2871 options.pnn));
2872 talloc_free(tmp_ctx);
2873 return -1;
2876 for (i=0; i < argc; i++) {
2877 printf("Server id %d:%llu %s\n", options.pnn, (long long)ids[i],
2878 result[i] ? "exists" : "does not exist");
2881 talloc_free(tmp_ctx);
2882 return 0;
2886 send a tcp tickle ack
2888 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2890 int ret;
2891 ctdb_sock_addr src, dst;
2893 if (argc < 2) {
2894 usage();
2897 if (!parse_ip_port(argv[0], &src)) {
2898 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2899 return -1;
2902 if (!parse_ip_port(argv[1], &dst)) {
2903 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2904 return -1;
2907 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
2908 if (ret==0) {
2909 return 0;
2911 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
2913 return -1;
2918 display public ip status
2920 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
2922 int i, ret;
2923 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2924 struct ctdb_all_public_ips *ips;
2926 if (options.pnn == CTDB_BROADCAST_ALL) {
2927 /* read the list of public ips from all nodes */
2928 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
2929 } else {
2930 /* read the public ip list from this node */
2931 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2933 if (ret != 0) {
2934 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
2935 talloc_free(tmp_ctx);
2936 return ret;
2939 if (options.machinereadable){
2940 printf(":Public IP:Node:");
2941 if (options.verbose){
2942 printf("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2944 printf("\n");
2945 } else {
2946 if (options.pnn == CTDB_BROADCAST_ALL) {
2947 printf("Public IPs on ALL nodes\n");
2948 } else {
2949 printf("Public IPs on node %u\n", options.pnn);
2953 for (i=1;i<=ips->num;i++) {
2954 struct ctdb_control_public_ip_info *info = NULL;
2955 int32_t pnn;
2956 char *aciface = NULL;
2957 char *avifaces = NULL;
2958 char *cifaces = NULL;
2960 if (options.pnn == CTDB_BROADCAST_ALL) {
2961 pnn = ips->ips[ips->num-i].pnn;
2962 } else {
2963 pnn = options.pnn;
2966 if (pnn != -1) {
2967 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
2968 &ips->ips[ips->num-i].addr, &info);
2969 } else {
2970 ret = -1;
2973 if (ret == 0) {
2974 int j;
2975 for (j=0; j < info->num; j++) {
2976 if (cifaces == NULL) {
2977 cifaces = talloc_strdup(info,
2978 info->ifaces[j].name);
2979 } else {
2980 cifaces = talloc_asprintf_append(cifaces,
2981 ",%s",
2982 info->ifaces[j].name);
2985 if (info->active_idx == j) {
2986 aciface = info->ifaces[j].name;
2989 if (info->ifaces[j].link_state == 0) {
2990 continue;
2993 if (avifaces == NULL) {
2994 avifaces = talloc_strdup(info, info->ifaces[j].name);
2995 } else {
2996 avifaces = talloc_asprintf_append(avifaces,
2997 ",%s",
2998 info->ifaces[j].name);
3003 if (options.machinereadable){
3004 printf(":%s:%d:",
3005 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
3006 ips->ips[ips->num-i].pnn);
3007 if (options.verbose){
3008 printf("%s:%s:%s:",
3009 aciface?aciface:"",
3010 avifaces?avifaces:"",
3011 cifaces?cifaces:"");
3013 printf("\n");
3014 } else {
3015 if (options.verbose) {
3016 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
3017 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
3018 ips->ips[ips->num-i].pnn,
3019 aciface?aciface:"",
3020 avifaces?avifaces:"",
3021 cifaces?cifaces:"");
3022 } else {
3023 printf("%s %d\n",
3024 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
3025 ips->ips[ips->num-i].pnn);
3028 talloc_free(info);
3031 talloc_free(tmp_ctx);
3032 return 0;
3036 public ip info
3038 static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
3040 int i, ret;
3041 ctdb_sock_addr addr;
3042 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3043 struct ctdb_control_public_ip_info *info;
3045 if (argc != 1) {
3046 talloc_free(tmp_ctx);
3047 usage();
3050 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
3051 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
3052 return -1;
3055 /* read the public ip info from this node */
3056 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), options.pnn,
3057 tmp_ctx, &addr, &info);
3058 if (ret != 0) {
3059 DEBUG(DEBUG_ERR, ("Unable to get public ip[%s]info from node %u\n",
3060 argv[0], options.pnn));
3061 talloc_free(tmp_ctx);
3062 return ret;
3065 printf("Public IP[%s] info on node %u\n",
3066 ctdb_addr_to_str(&info->ip.addr),
3067 options.pnn);
3069 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
3070 ctdb_addr_to_str(&info->ip.addr),
3071 info->ip.pnn, info->num);
3073 for (i=0; i<info->num; i++) {
3074 info->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
3076 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
3077 i+1, info->ifaces[i].name,
3078 info->ifaces[i].link_state?"up":"down",
3079 (unsigned int)info->ifaces[i].references,
3080 (i==info->active_idx)?" (active)":"");
3083 talloc_free(tmp_ctx);
3084 return 0;
3088 display interfaces status
3090 static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
3092 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3093 int i;
3094 struct ctdb_control_get_ifaces *ifaces;
3095 int ret;
3097 /* read the public ip list from this node */
3098 ret = ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ifaces);
3099 if (ret != 0) {
3100 DEBUG(DEBUG_ERR, ("Unable to get interfaces from node %u\n",
3101 options.pnn));
3102 talloc_free(tmp_ctx);
3103 return -1;
3106 if (options.machinereadable){
3107 printf(":Name:LinkStatus:References:\n");
3108 } else {
3109 printf("Interfaces on node %u\n", options.pnn);
3112 for (i=0; i<ifaces->num; i++) {
3113 if (options.machinereadable){
3114 printf(":%s:%s:%u\n",
3115 ifaces->ifaces[i].name,
3116 ifaces->ifaces[i].link_state?"1":"0",
3117 (unsigned int)ifaces->ifaces[i].references);
3118 } else {
3119 printf("name:%s link:%s references:%u\n",
3120 ifaces->ifaces[i].name,
3121 ifaces->ifaces[i].link_state?"up":"down",
3122 (unsigned int)ifaces->ifaces[i].references);
3126 talloc_free(tmp_ctx);
3127 return 0;
3132 set link status of an interface
3134 static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
3136 int ret;
3137 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3138 struct ctdb_control_iface_info info;
3140 ZERO_STRUCT(info);
3142 if (argc != 2) {
3143 usage();
3146 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
3147 DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
3148 argv[0]));
3149 talloc_free(tmp_ctx);
3150 return -1;
3152 strcpy(info.name, argv[0]);
3154 if (strcmp(argv[1], "up") == 0) {
3155 info.link_state = 1;
3156 } else if (strcmp(argv[1], "down") == 0) {
3157 info.link_state = 0;
3158 } else {
3159 DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
3160 argv[1]));
3161 talloc_free(tmp_ctx);
3162 return -1;
3165 /* read the public ip list from this node */
3166 ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
3167 tmp_ctx, &info);
3168 if (ret != 0) {
3169 DEBUG(DEBUG_ERR, ("Unable to set link state for interfaces %s node %u\n",
3170 argv[0], options.pnn));
3171 talloc_free(tmp_ctx);
3172 return ret;
3175 talloc_free(tmp_ctx);
3176 return 0;
3180 display pid of a ctdb daemon
3182 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
3184 uint32_t pid;
3185 int ret;
3187 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
3188 if (ret != 0) {
3189 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
3190 return ret;
3192 printf("Pid:%d\n", pid);
3194 return 0;
3197 typedef bool update_flags_handler_t(struct ctdb_context *ctdb, void *data);
3199 static int update_flags_and_ipreallocate(struct ctdb_context *ctdb,
3200 void *data,
3201 update_flags_handler_t handler,
3202 uint32_t flag,
3203 const char *desc,
3204 bool set_flag)
3206 struct ctdb_node_map *nodemap = NULL;
3207 bool flag_is_set;
3208 int ret;
3210 /* Check if the node is already in the desired state */
3211 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3212 if (ret != 0) {
3213 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3214 exit(10);
3216 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3217 if (set_flag == flag_is_set) {
3218 DEBUG(DEBUG_NOTICE, ("Node %d is %s %s\n", options.pnn,
3219 (set_flag ? "already" : "not"), desc));
3220 return 0;
3223 do {
3224 if (!handler(ctdb, data)) {
3225 DEBUG(DEBUG_WARNING,
3226 ("Failed to send control to set state %s on node %u, try again\n",
3227 desc, options.pnn));
3230 sleep(1);
3232 /* Read the nodemap and verify the change took effect.
3233 * Even if the above control/hanlder timed out then it
3234 * could still have worked!
3236 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
3237 ctdb, &nodemap);
3238 if (ret != 0) {
3239 DEBUG(DEBUG_WARNING,
3240 ("Unable to get nodemap from local node, try again\n"));
3242 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3243 } while (nodemap == NULL || (set_flag != flag_is_set));
3245 return ipreallocate(ctdb);
3248 /* Administratively disable a node */
3249 static bool update_flags_disabled(struct ctdb_context *ctdb, void *data)
3251 int ret;
3253 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3254 NODE_FLAGS_PERMANENTLY_DISABLED, 0);
3255 return ret == 0;
3258 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
3260 return update_flags_and_ipreallocate(ctdb, NULL,
3261 update_flags_disabled,
3262 NODE_FLAGS_PERMANENTLY_DISABLED,
3263 "disabled",
3264 true /* set_flag*/);
3267 /* Administratively re-enable a node */
3268 static bool update_flags_not_disabled(struct ctdb_context *ctdb, void *data)
3270 int ret;
3272 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3273 0, NODE_FLAGS_PERMANENTLY_DISABLED);
3274 return ret == 0;
3277 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
3279 return update_flags_and_ipreallocate(ctdb, NULL,
3280 update_flags_not_disabled,
3281 NODE_FLAGS_PERMANENTLY_DISABLED,
3282 "disabled",
3283 false /* set_flag*/);
3286 /* Stop a node */
3287 static bool update_flags_stopped(struct ctdb_context *ctdb, void *data)
3289 int ret;
3291 ret = ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn);
3293 return ret == 0;
3296 static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
3298 return update_flags_and_ipreallocate(ctdb, NULL,
3299 update_flags_stopped,
3300 NODE_FLAGS_STOPPED,
3301 "stopped",
3302 true /* set_flag*/);
3305 /* Continue a stopped node */
3306 static bool update_flags_not_stopped(struct ctdb_context *ctdb, void *data)
3308 int ret;
3310 ret = ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn);
3312 return ret == 0;
3315 static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
3317 return update_flags_and_ipreallocate(ctdb, NULL,
3318 update_flags_not_stopped,
3319 NODE_FLAGS_STOPPED,
3320 "stopped",
3321 false /* set_flag */);
3324 static uint32_t get_generation(struct ctdb_context *ctdb)
3326 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3327 struct ctdb_vnn_map *vnnmap=NULL;
3328 int ret;
3329 uint32_t generation;
3331 /* wait until the recmaster is not in recovery mode */
3332 while (1) {
3333 uint32_t recmode, recmaster;
3335 if (vnnmap != NULL) {
3336 talloc_free(vnnmap);
3337 vnnmap = NULL;
3340 /* get the recmaster */
3341 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), CTDB_CURRENT_NODE, &recmaster);
3342 if (ret != 0) {
3343 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
3344 talloc_free(tmp_ctx);
3345 exit(10);
3348 /* get recovery mode */
3349 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), recmaster, &recmode);
3350 if (ret != 0) {
3351 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
3352 talloc_free(tmp_ctx);
3353 exit(10);
3356 /* get the current generation number */
3357 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, tmp_ctx, &vnnmap);
3358 if (ret != 0) {
3359 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
3360 talloc_free(tmp_ctx);
3361 exit(10);
3364 if ((recmode == CTDB_RECOVERY_NORMAL) && (vnnmap->generation != 1)) {
3365 generation = vnnmap->generation;
3366 talloc_free(tmp_ctx);
3367 return generation;
3369 sleep(1);
3373 /* Ban a node */
3374 static bool update_state_banned(struct ctdb_context *ctdb, void *data)
3376 struct ctdb_ban_time *bantime = (struct ctdb_ban_time *)data;
3377 int ret;
3379 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, bantime);
3381 return ret == 0;
3384 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
3386 struct ctdb_ban_time bantime;
3388 if (argc < 1) {
3389 usage();
3392 bantime.pnn = options.pnn;
3393 bantime.time = strtoul(argv[0], NULL, 0);
3395 if (bantime.time == 0) {
3396 DEBUG(DEBUG_ERR, ("Invalid ban time specified - must be >0\n"));
3397 return -1;
3400 return update_flags_and_ipreallocate(ctdb, &bantime,
3401 update_state_banned,
3402 NODE_FLAGS_BANNED,
3403 "banned",
3404 true /* set_flag*/);
3408 /* Unban a node */
3409 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
3411 struct ctdb_ban_time bantime;
3413 bantime.pnn = options.pnn;
3414 bantime.time = 0;
3416 return update_flags_and_ipreallocate(ctdb, &bantime,
3417 update_state_banned,
3418 NODE_FLAGS_BANNED,
3419 "banned",
3420 false /* set_flag*/);
3424 show ban information for a node
3426 static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
3428 int ret;
3429 struct ctdb_node_map *nodemap=NULL;
3430 struct ctdb_ban_time *bantime;
3432 /* verify the node exists */
3433 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3434 if (ret != 0) {
3435 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3436 return ret;
3439 ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
3440 if (ret != 0) {
3441 DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
3442 return -1;
3445 if (bantime->time == 0) {
3446 printf("Node %u is not banned\n", bantime->pnn);
3447 } else {
3448 printf("Node %u is banned, %d seconds remaining\n",
3449 bantime->pnn, bantime->time);
3452 return 0;
3456 shutdown a daemon
3458 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
3460 int ret;
3462 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
3463 if (ret != 0) {
3464 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
3465 return ret;
3468 return 0;
3472 trigger a recovery
3474 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
3476 int ret;
3477 uint32_t generation, next_generation;
3479 /* record the current generation number */
3480 generation = get_generation(ctdb);
3482 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
3483 if (ret != 0) {
3484 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
3485 return ret;
3488 /* wait until we are in a new generation */
3489 while (1) {
3490 next_generation = get_generation(ctdb);
3491 if (next_generation != generation) {
3492 return 0;
3494 sleep(1);
3497 return 0;
3502 display monitoring mode of a remote node
3504 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
3506 uint32_t monmode;
3507 int ret;
3509 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
3510 if (ret != 0) {
3511 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
3512 return ret;
3514 if (!options.machinereadable){
3515 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
3516 } else {
3517 printf(":mode:\n");
3518 printf(":%d:\n",monmode);
3520 return 0;
3525 display capabilities of a remote node
3527 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
3529 uint32_t capabilities;
3530 int ret;
3532 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
3533 if (ret != 0) {
3534 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
3535 return -1;
3538 if (!options.machinereadable){
3539 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
3540 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
3541 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
3542 printf("NATGW: %s\n", (capabilities&CTDB_CAP_NATGW)?"YES":"NO");
3543 } else {
3544 printf(":RECMASTER:LMASTER:LVS:NATGW:\n");
3545 printf(":%d:%d:%d:%d:\n",
3546 !!(capabilities&CTDB_CAP_RECMASTER),
3547 !!(capabilities&CTDB_CAP_LMASTER),
3548 !!(capabilities&CTDB_CAP_LVS),
3549 !!(capabilities&CTDB_CAP_NATGW));
3551 return 0;
3555 display lvs configuration
3558 static uint32_t lvs_exclude_flags[] = {
3559 /* Look for a nice healthy node */
3560 NODE_FLAGS_INACTIVE|NODE_FLAGS_DISABLED,
3561 /* If not found, an UNHEALTHY node will do */
3562 NODE_FLAGS_INACTIVE|NODE_FLAGS_PERMANENTLY_DISABLED,
3566 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
3568 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3569 struct ctdb_node_map *orig_nodemap=NULL;
3570 struct ctdb_node_map *nodemap;
3571 int i, ret;
3573 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn,
3574 tmp_ctx, &orig_nodemap);
3575 if (ret != 0) {
3576 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3577 talloc_free(tmp_ctx);
3578 return -1;
3581 nodemap = filter_nodemap_by_capabilities(ctdb, orig_nodemap,
3582 CTDB_CAP_LVS, false);
3583 if (nodemap == NULL) {
3584 /* No memory */
3585 ret = -1;
3586 goto done;
3589 ret = 0;
3591 for (i = 0; lvs_exclude_flags[i] != 0; i++) {
3592 struct ctdb_node_map *t =
3593 filter_nodemap_by_flags(ctdb, nodemap,
3594 lvs_exclude_flags[i]);
3595 if (t == NULL) {
3596 /* No memory */
3597 ret = -1;
3598 goto done;
3600 if (t->num > 0) {
3601 /* At least 1 node without excluded flags */
3602 int j;
3603 for (j = 0; j < t->num; j++) {
3604 printf("%d:%s\n", t->nodes[j].pnn,
3605 ctdb_addr_to_str(&t->nodes[j].addr));
3607 goto done;
3609 talloc_free(t);
3611 done:
3612 talloc_free(tmp_ctx);
3613 return ret;
3617 display who is the lvs master
3619 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
3621 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3622 struct ctdb_node_map *nodemap=NULL;
3623 int i, ret;
3625 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn,
3626 tmp_ctx, &nodemap);
3627 if (ret != 0) {
3628 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3629 talloc_free(tmp_ctx);
3630 return -1;
3633 for (i = 0; lvs_exclude_flags[i] != 0; i++) {
3634 struct ctdb_node_map *t =
3635 filter_nodemap_by_flags(ctdb, nodemap,
3636 lvs_exclude_flags[i]);
3637 if (t == NULL) {
3638 /* No memory */
3639 ret = -1;
3640 goto done;
3642 if (t->num > 0) {
3643 struct ctdb_node_map *n;
3644 n = filter_nodemap_by_capabilities(ctdb,
3646 CTDB_CAP_LVS,
3647 true);
3648 if (n == NULL) {
3649 /* No memory */
3650 ret = -1;
3651 goto done;
3653 if (n->num > 0) {
3654 ret = 0;
3655 printf(options.machinereadable ?
3656 "%d\n" : "Node %d is LVS master\n",
3657 n->nodes[0].pnn);
3658 goto done;
3661 talloc_free(t);
3664 printf("There is no LVS master\n");
3665 ret = 255;
3666 done:
3667 talloc_free(tmp_ctx);
3668 return ret;
3672 disable monitoring on a node
3674 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3677 int ret;
3679 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
3680 if (ret != 0) {
3681 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
3682 return ret;
3684 printf("Monitoring mode:%s\n","DISABLED");
3686 return 0;
3690 enable monitoring on a node
3692 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3695 int ret;
3697 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
3698 if (ret != 0) {
3699 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
3700 return ret;
3702 printf("Monitoring mode:%s\n","ACTIVE");
3704 return 0;
3708 display remote list of keys/data for a db
3710 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
3712 const char *db_name;
3713 struct ctdb_db_context *ctdb_db;
3714 int ret;
3715 struct ctdb_dump_db_context c;
3716 uint8_t flags;
3718 if (argc < 1) {
3719 usage();
3722 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3723 return -1;
3726 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3727 if (ctdb_db == NULL) {
3728 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3729 return -1;
3732 if (options.printlmaster) {
3733 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn,
3734 ctdb, &ctdb->vnn_map);
3735 if (ret != 0) {
3736 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
3737 options.pnn));
3738 return ret;
3742 ZERO_STRUCT(c);
3743 c.f = stdout;
3744 c.printemptyrecords = (bool)options.printemptyrecords;
3745 c.printdatasize = (bool)options.printdatasize;
3746 c.printlmaster = (bool)options.printlmaster;
3747 c.printhash = (bool)options.printhash;
3748 c.printrecordflags = (bool)options.printrecordflags;
3750 /* traverse and dump the cluster tdb */
3751 ret = ctdb_dump_db(ctdb_db, &c);
3752 if (ret == -1) {
3753 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
3754 DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
3755 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3756 db_name));
3757 return -1;
3759 talloc_free(ctdb_db);
3761 printf("Dumped %d records\n", ret);
3762 return 0;
3765 struct cattdb_data {
3766 struct ctdb_context *ctdb;
3767 uint32_t count;
3770 static int cattdb_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
3772 struct cattdb_data *d = private_data;
3773 struct ctdb_dump_db_context c;
3775 d->count++;
3777 ZERO_STRUCT(c);
3778 c.f = stdout;
3779 c.printemptyrecords = (bool)options.printemptyrecords;
3780 c.printdatasize = (bool)options.printdatasize;
3781 c.printlmaster = false;
3782 c.printhash = (bool)options.printhash;
3783 c.printrecordflags = true;
3785 return ctdb_dumpdb_record(d->ctdb, key, data, &c);
3789 cat the local tdb database using same format as catdb
3791 static int control_cattdb(struct ctdb_context *ctdb, int argc, const char **argv)
3793 const char *db_name;
3794 struct ctdb_db_context *ctdb_db;
3795 struct cattdb_data d;
3796 uint8_t flags;
3798 if (argc < 1) {
3799 usage();
3802 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3803 return -1;
3806 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3807 if (ctdb_db == NULL) {
3808 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3809 return -1;
3812 /* traverse the local tdb */
3813 d.count = 0;
3814 d.ctdb = ctdb;
3815 if (tdb_traverse_read(ctdb_db->ltdb->tdb, cattdb_traverse, &d) == -1) {
3816 printf("Failed to cattdb data\n");
3817 exit(10);
3819 talloc_free(ctdb_db);
3821 printf("Dumped %d records\n", d.count);
3822 return 0;
3826 display the content of a database key
3828 static int control_readkey(struct ctdb_context *ctdb, int argc, const char **argv)
3830 const char *db_name;
3831 struct ctdb_db_context *ctdb_db;
3832 struct ctdb_record_handle *h;
3833 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3834 TDB_DATA key, data;
3835 uint8_t flags;
3837 if (argc < 2) {
3838 usage();
3841 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3842 return -1;
3845 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3846 if (ctdb_db == NULL) {
3847 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3848 return -1;
3851 key.dptr = discard_const(argv[1]);
3852 key.dsize = strlen((char *)key.dptr);
3854 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3855 if (h == NULL) {
3856 printf("Failed to fetch record '%s' on node %d\n",
3857 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3858 talloc_free(tmp_ctx);
3859 exit(10);
3862 printf("Data: size:%d ptr:[%.*s]\n", (int)data.dsize, (int)data.dsize, data.dptr);
3864 talloc_free(tmp_ctx);
3865 talloc_free(ctdb_db);
3866 return 0;
3870 display the content of a database key
3872 static int control_writekey(struct ctdb_context *ctdb, int argc, const char **argv)
3874 const char *db_name;
3875 struct ctdb_db_context *ctdb_db;
3876 struct ctdb_record_handle *h;
3877 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3878 TDB_DATA key, data;
3879 uint8_t flags;
3881 if (argc < 3) {
3882 usage();
3885 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3886 return -1;
3889 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3890 if (ctdb_db == NULL) {
3891 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3892 return -1;
3895 key.dptr = discard_const(argv[1]);
3896 key.dsize = strlen((char *)key.dptr);
3898 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3899 if (h == NULL) {
3900 printf("Failed to fetch record '%s' on node %d\n",
3901 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3902 talloc_free(tmp_ctx);
3903 exit(10);
3906 data.dptr = discard_const(argv[2]);
3907 data.dsize = strlen((char *)data.dptr);
3909 if (ctdb_record_store(h, data) != 0) {
3910 printf("Failed to store record\n");
3913 talloc_free(h);
3914 talloc_free(tmp_ctx);
3915 talloc_free(ctdb_db);
3916 return 0;
3920 fetch a record from a persistent database
3922 static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3924 const char *db_name;
3925 struct ctdb_db_context *ctdb_db;
3926 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3927 struct ctdb_transaction_handle *h;
3928 TDB_DATA key, data;
3929 int fd, ret;
3930 bool persistent;
3931 uint8_t flags;
3933 if (argc < 2) {
3934 talloc_free(tmp_ctx);
3935 usage();
3938 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3939 talloc_free(tmp_ctx);
3940 return -1;
3943 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
3944 if (!persistent) {
3945 DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", db_name));
3946 talloc_free(tmp_ctx);
3947 return -1;
3950 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3951 if (ctdb_db == NULL) {
3952 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3953 talloc_free(tmp_ctx);
3954 return -1;
3957 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3958 if (h == NULL) {
3959 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3960 talloc_free(tmp_ctx);
3961 return -1;
3964 key.dptr = discard_const(argv[1]);
3965 key.dsize = strlen(argv[1]);
3966 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
3967 if (ret != 0) {
3968 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
3969 talloc_free(tmp_ctx);
3970 return -1;
3973 if (data.dsize == 0 || data.dptr == NULL) {
3974 DEBUG(DEBUG_ERR,("Record is empty\n"));
3975 talloc_free(tmp_ctx);
3976 return -1;
3979 if (argc == 3) {
3980 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3981 if (fd == -1) {
3982 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3983 talloc_free(tmp_ctx);
3984 return -1;
3986 sys_write(fd, data.dptr, data.dsize);
3987 close(fd);
3988 } else {
3989 sys_write(1, data.dptr, data.dsize);
3992 /* abort the transaction */
3993 talloc_free(h);
3996 talloc_free(tmp_ctx);
3997 return 0;
4001 fetch a record from a tdb-file
4003 static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv)
4005 const char *tdb_file;
4006 TDB_CONTEXT *tdb;
4007 TDB_DATA key, data;
4008 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
4009 int fd;
4011 if (argc < 2) {
4012 usage();
4015 tdb_file = argv[0];
4017 tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
4018 if (tdb == NULL) {
4019 printf("Failed to open TDB file %s\n", tdb_file);
4020 return -1;
4023 if (!strncmp(argv[1], "0x", 2)) {
4024 key = hextodata(tmp_ctx, argv[1] + 2);
4025 if (key.dsize == 0) {
4026 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4027 return -1;
4029 } else {
4030 key.dptr = discard_const(argv[1]);
4031 key.dsize = strlen(argv[1]);
4034 data = tdb_fetch(tdb, key);
4035 if (data.dptr == NULL || data.dsize < sizeof(struct ctdb_ltdb_header)) {
4036 printf("Failed to read record %s from tdb %s\n", argv[1], tdb_file);
4037 tdb_close(tdb);
4038 return -1;
4041 tdb_close(tdb);
4043 if (argc == 3) {
4044 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
4045 if (fd == -1) {
4046 printf("Failed to open output file %s\n", argv[2]);
4047 return -1;
4049 if (options.verbose){
4050 sys_write(fd, data.dptr, data.dsize);
4051 } else {
4052 sys_write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
4054 close(fd);
4055 } else {
4056 if (options.verbose){
4057 sys_write(1, data.dptr, data.dsize);
4058 } else {
4059 sys_write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
4063 talloc_free(tmp_ctx);
4064 return 0;
4068 store a record and header to a tdb-file
4070 static int control_tstore(struct ctdb_context *ctdb, int argc, const char **argv)
4072 const char *tdb_file;
4073 TDB_CONTEXT *tdb;
4074 TDB_DATA key, value, data;
4075 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
4076 struct ctdb_ltdb_header header;
4078 if (argc < 3) {
4079 usage();
4082 tdb_file = argv[0];
4084 tdb = tdb_open(tdb_file, 0, 0, O_RDWR, 0);
4085 if (tdb == NULL) {
4086 printf("Failed to open TDB file %s\n", tdb_file);
4087 return -1;
4090 if (!strncmp(argv[1], "0x", 2)) {
4091 key = hextodata(tmp_ctx, argv[1] + 2);
4092 if (key.dsize == 0) {
4093 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4094 return -1;
4096 } else {
4097 key.dptr = discard_const(argv[1]);
4098 key.dsize = strlen(argv[1]);
4101 if (!strncmp(argv[2], "0x", 2)) {
4102 value = hextodata(tmp_ctx, argv[2] + 2);
4103 if (value.dsize == 0) {
4104 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[2]);
4105 return -1;
4107 } else {
4108 value.dptr = discard_const(argv[2]);
4109 value.dsize = strlen(argv[2]);
4112 ZERO_STRUCT(header);
4113 if (argc > 3) {
4114 header.rsn = atoll(argv[3]);
4116 if (argc > 4) {
4117 header.dmaster = atoi(argv[4]);
4119 if (argc > 5) {
4120 header.flags = atoi(argv[5]);
4123 data.dsize = sizeof(struct ctdb_ltdb_header) + value.dsize;
4124 data.dptr = talloc_size(tmp_ctx, data.dsize);
4125 if (data.dptr == NULL) {
4126 printf("Failed to allocate header+value\n");
4127 return -1;
4130 *(struct ctdb_ltdb_header *)data.dptr = header;
4131 memcpy(data.dptr + sizeof(struct ctdb_ltdb_header), value.dptr, value.dsize);
4133 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0) {
4134 printf("Failed to write record %s to tdb %s\n", argv[1], tdb_file);
4135 tdb_close(tdb);
4136 return -1;
4139 tdb_close(tdb);
4141 talloc_free(tmp_ctx);
4142 return 0;
4146 write a record to a persistent database
4148 static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv)
4150 const char *db_name;
4151 struct ctdb_db_context *ctdb_db;
4152 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4153 struct ctdb_transaction_handle *h;
4154 struct stat st;
4155 TDB_DATA key, data;
4156 int fd, ret;
4158 if (argc < 3) {
4159 talloc_free(tmp_ctx);
4160 usage();
4163 fd = open(argv[2], O_RDONLY);
4164 if (fd == -1) {
4165 DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s %s\n", argv[2], strerror(errno)));
4166 talloc_free(tmp_ctx);
4167 return -1;
4170 ret = fstat(fd, &st);
4171 if (ret == -1) {
4172 DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
4173 close(fd);
4174 talloc_free(tmp_ctx);
4175 return -1;
4178 if (!S_ISREG(st.st_mode)) {
4179 DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
4180 close(fd);
4181 talloc_free(tmp_ctx);
4182 return -1;
4185 data.dsize = st.st_size;
4186 if (data.dsize == 0) {
4187 data.dptr = NULL;
4188 } else {
4189 data.dptr = talloc_size(tmp_ctx, data.dsize);
4190 if (data.dptr == NULL) {
4191 DEBUG(DEBUG_ERR,("Failed to talloc %d of memory to store record data\n", (int)data.dsize));
4192 close(fd);
4193 talloc_free(tmp_ctx);
4194 return -1;
4196 ret = sys_read(fd, data.dptr, data.dsize);
4197 if (ret != data.dsize) {
4198 DEBUG(DEBUG_ERR,("Failed to read %d bytes of record data\n", (int)data.dsize));
4199 close(fd);
4200 talloc_free(tmp_ctx);
4201 return -1;
4204 close(fd);
4207 db_name = argv[0];
4209 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
4210 if (ctdb_db == NULL) {
4211 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4212 talloc_free(tmp_ctx);
4213 return -1;
4216 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4217 if (h == NULL) {
4218 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4219 talloc_free(tmp_ctx);
4220 return -1;
4223 key.dptr = discard_const(argv[1]);
4224 key.dsize = strlen(argv[1]);
4225 ret = ctdb_transaction_store(h, key, data);
4226 if (ret != 0) {
4227 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4228 talloc_free(tmp_ctx);
4229 return -1;
4232 ret = ctdb_transaction_commit(h);
4233 if (ret != 0) {
4234 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4235 talloc_free(tmp_ctx);
4236 return -1;
4240 talloc_free(tmp_ctx);
4241 return 0;
4245 * delete a record from a persistent database
4247 static int control_pdelete(struct ctdb_context *ctdb, int argc, const char **argv)
4249 const char *db_name;
4250 struct ctdb_db_context *ctdb_db;
4251 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4252 struct ctdb_transaction_handle *h;
4253 TDB_DATA key;
4254 int ret;
4255 bool persistent;
4256 uint8_t flags;
4258 if (argc < 2) {
4259 talloc_free(tmp_ctx);
4260 usage();
4263 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
4264 talloc_free(tmp_ctx);
4265 return -1;
4268 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
4269 if (!persistent) {
4270 DEBUG(DEBUG_ERR, ("Database '%s' is not persistent\n", db_name));
4271 talloc_free(tmp_ctx);
4272 return -1;
4275 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4276 if (ctdb_db == NULL) {
4277 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n", db_name));
4278 talloc_free(tmp_ctx);
4279 return -1;
4282 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4283 if (h == NULL) {
4284 DEBUG(DEBUG_ERR, ("Failed to start transaction on database %s\n", db_name));
4285 talloc_free(tmp_ctx);
4286 return -1;
4289 key.dptr = discard_const(argv[1]);
4290 key.dsize = strlen(argv[1]);
4291 ret = ctdb_transaction_store(h, key, tdb_null);
4292 if (ret != 0) {
4293 DEBUG(DEBUG_ERR, ("Failed to delete record\n"));
4294 talloc_free(tmp_ctx);
4295 return -1;
4298 ret = ctdb_transaction_commit(h);
4299 if (ret != 0) {
4300 DEBUG(DEBUG_ERR, ("Failed to commit transaction\n"));
4301 talloc_free(tmp_ctx);
4302 return -1;
4305 talloc_free(tmp_ctx);
4306 return 0;
4309 static const char *ptrans_parse_string(TALLOC_CTX *mem_ctx, const char *s,
4310 TDB_DATA *data)
4312 const char *t;
4313 size_t n;
4314 const char *ret; /* Next byte after successfully parsed value */
4316 /* Error, unless someone says otherwise */
4317 ret = NULL;
4318 /* Indicates no value to parse */
4319 *data = tdb_null;
4321 /* Skip whitespace */
4322 n = strspn(s, " \t");
4323 t = s + n;
4325 if (t[0] == '"') {
4326 /* Quoted ASCII string - no wide characters! */
4327 t++;
4328 n = strcspn(t, "\"");
4329 if (t[n] == '"') {
4330 if (n > 0) {
4331 data->dsize = n;
4332 data->dptr = talloc_memdup(mem_ctx, t, n);
4333 CTDB_NOMEM_ABORT(data->dptr);
4335 ret = t + n + 1;
4336 } else {
4337 DEBUG(DEBUG_WARNING,("Unmatched \" in input %s\n", s));
4339 } else {
4340 DEBUG(DEBUG_WARNING,("Unsupported input format in %s\n", s));
4343 return ret;
4346 static bool ptrans_get_key_value(TALLOC_CTX *mem_ctx, FILE *file,
4347 TDB_DATA *key, TDB_DATA *value)
4349 char line [1024]; /* FIXME: make this more flexible? */
4350 const char *t;
4351 char *ptr;
4353 ptr = fgets(line, sizeof(line), file);
4355 if (ptr == NULL) {
4356 return false;
4359 /* Get key */
4360 t = ptrans_parse_string(mem_ctx, line, key);
4361 if (t == NULL || key->dptr == NULL) {
4362 /* Line Ignored but not EOF */
4363 return true;
4366 /* Get value */
4367 t = ptrans_parse_string(mem_ctx, t, value);
4368 if (t == NULL) {
4369 /* Line Ignored but not EOF */
4370 talloc_free(key->dptr);
4371 *key = tdb_null;
4372 return true;
4375 return true;
4379 * Update a persistent database as per file/stdin
4381 static int control_ptrans(struct ctdb_context *ctdb,
4382 int argc, const char **argv)
4384 const char *db_name;
4385 struct ctdb_db_context *ctdb_db;
4386 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4387 struct ctdb_transaction_handle *h;
4388 TDB_DATA key, value;
4389 FILE *file;
4390 int ret;
4392 if (argc < 1) {
4393 talloc_free(tmp_ctx);
4394 usage();
4397 file = stdin;
4398 if (argc == 2) {
4399 file = fopen(argv[1], "r");
4400 if (file == NULL) {
4401 DEBUG(DEBUG_ERR,("Unable to open file for reading '%s'\n", argv[1]));
4402 talloc_free(tmp_ctx);
4403 return -1;
4407 db_name = argv[0];
4409 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
4410 if (ctdb_db == NULL) {
4411 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4412 goto error;
4415 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4416 if (h == NULL) {
4417 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4418 goto error;
4421 while (ptrans_get_key_value(tmp_ctx, file, &key, &value)) {
4422 if (key.dsize != 0) {
4423 ret = ctdb_transaction_store(h, key, value);
4424 /* Minimise memory use */
4425 talloc_free(key.dptr);
4426 if (value.dptr != NULL) {
4427 talloc_free(value.dptr);
4429 if (ret != 0) {
4430 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4431 ctdb_transaction_cancel(h);
4432 goto error;
4437 ret = ctdb_transaction_commit(h);
4438 if (ret != 0) {
4439 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4440 goto error;
4443 if (file != stdin) {
4444 fclose(file);
4446 talloc_free(tmp_ctx);
4447 return 0;
4449 error:
4450 if (file != stdin) {
4451 fclose(file);
4454 talloc_free(tmp_ctx);
4455 return -1;
4459 check if a service is bound to a port or not
4461 static int control_chktcpport(struct ctdb_context *ctdb, int argc, const char **argv)
4463 int s, ret;
4464 int v;
4465 int port;
4466 struct sockaddr_in sin;
4468 if (argc != 1) {
4469 printf("Use: ctdb chktcport <port>\n");
4470 return EINVAL;
4473 port = atoi(argv[0]);
4475 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
4476 if (s == -1) {
4477 printf("Failed to open local socket\n");
4478 return errno;
4481 v = fcntl(s, F_GETFL, 0);
4482 if (v == -1 || fcntl(s, F_SETFL, v | O_NONBLOCK) != 0) {
4483 printf("Unable to set socket non-blocking: %s\n", strerror(errno));
4486 bzero(&sin, sizeof(sin));
4487 sin.sin_family = PF_INET;
4488 sin.sin_port = htons(port);
4489 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
4490 close(s);
4491 if (ret == -1) {
4492 printf("Failed to bind to local socket: %d %s\n", errno, strerror(errno));
4493 return errno;
4496 return 0;
4501 static void log_handler(struct ctdb_context *ctdb, uint64_t srvid,
4502 TDB_DATA data, void *private_data)
4504 DEBUG(DEBUG_ERR,("Log data received\n"));
4505 if (data.dsize > 0) {
4506 printf("%s", data.dptr);
4509 exit(0);
4513 display a list of log messages from the in memory ringbuffer
4515 static int control_getlog(struct ctdb_context *ctdb, int argc, const char **argv)
4517 int ret, i;
4518 bool main_daemon;
4519 struct ctdb_get_log_addr log_addr;
4520 TDB_DATA data;
4521 struct timeval tv;
4523 /* Process options */
4524 main_daemon = true;
4525 log_addr.pnn = ctdb_get_pnn(ctdb);
4526 log_addr.level = DEBUG_NOTICE;
4527 for (i = 0; i < argc; i++) {
4528 if (strcmp(argv[i], "recoverd") == 0) {
4529 main_daemon = false;
4530 } else {
4531 if (isalpha(argv[i][0]) || argv[i][0] == '-') {
4532 log_addr.level = get_debug_by_desc(argv[i]);
4533 } else {
4534 log_addr.level = strtol(argv[i], NULL, 0);
4539 /* Our message port is our PID */
4540 log_addr.srvid = getpid();
4542 data.dptr = (unsigned char *)&log_addr;
4543 data.dsize = sizeof(log_addr);
4545 DEBUG(DEBUG_ERR, ("Pulling logs from node %u\n", options.pnn));
4547 ctdb_client_set_message_handler(ctdb, log_addr.srvid, log_handler, NULL);
4548 sleep(1);
4550 DEBUG(DEBUG_ERR,("Listen for response on %d\n", (int)log_addr.srvid));
4552 if (main_daemon) {
4553 int32_t res;
4554 char *errmsg;
4555 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4557 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_GET_LOG,
4558 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
4559 if (ret != 0 || res != 0) {
4560 DEBUG(DEBUG_ERR,("Failed to get logs - %s\n", errmsg));
4561 talloc_free(tmp_ctx);
4562 return -1;
4564 talloc_free(tmp_ctx);
4565 } else {
4566 ret = ctdb_client_send_message(ctdb, options.pnn,
4567 CTDB_SRVID_GETLOG, data);
4568 if (ret != 0) {
4569 DEBUG(DEBUG_ERR,("Failed to send getlog request message to %u\n", options.pnn));
4570 return -1;
4574 tv = timeval_current();
4575 /* this loop will terminate when we have received the reply */
4576 while (timeval_elapsed(&tv) < (double)options.timelimit) {
4577 event_loop_once(ctdb->ev);
4580 DEBUG(DEBUG_INFO,("Timed out waiting for log data.\n"));
4582 return 0;
4586 clear the in memory log area
4588 static int control_clearlog(struct ctdb_context *ctdb, int argc, const char **argv)
4590 int ret;
4592 if (argc == 0 || (argc >= 1 && strcmp(argv[0], "recoverd") != 0)) {
4593 /* "recoverd" not given - get logs from main daemon */
4594 int32_t res;
4595 char *errmsg;
4596 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4598 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_CLEAR_LOG,
4599 0, tdb_null, tmp_ctx, NULL, &res, NULL, &errmsg);
4600 if (ret != 0 || res != 0) {
4601 DEBUG(DEBUG_ERR,("Failed to clear logs\n"));
4602 talloc_free(tmp_ctx);
4603 return -1;
4606 talloc_free(tmp_ctx);
4607 } else {
4608 TDB_DATA data; /* unused in recoverd... */
4609 data.dsize = 0;
4611 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_CLEARLOG, data);
4612 if (ret != 0) {
4613 DEBUG(DEBUG_ERR,("Failed to send clearlog request message to %u\n", options.pnn));
4614 return -1;
4618 return 0;
4621 /* Reload public IPs on a specified nodes */
4622 static int control_reloadips(struct ctdb_context *ctdb, int argc, const char **argv)
4624 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4625 uint32_t *nodes;
4626 uint32_t pnn_mode;
4627 uint32_t timeout;
4628 int ret;
4630 assert_single_node_only();
4632 if (argc > 1) {
4633 usage();
4636 /* Determine the nodes where IPs need to be reloaded */
4637 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
4638 options.pnn, true, &nodes, &pnn_mode)) {
4639 ret = -1;
4640 goto done;
4643 again:
4644 /* Disable takeover runs on all connected nodes. A reply
4645 * indicating success is needed from each node so all nodes
4646 * will need to be active. This will retry until maxruntime
4647 * is exceeded, hence no error handling.
4649 * A check could be added to not allow reloading of IPs when
4650 * there are disconnected nodes. However, this should
4651 * probably be left up to the administrator.
4653 timeout = LONGTIMEOUT;
4654 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, &timeout,
4655 "Disable takeover runs", true);
4657 /* Now tell all the desired nodes to reload their public IPs.
4658 * Keep trying this until it succeeds. This assumes all
4659 * failures are transient, which might not be true...
4661 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_RELOAD_PUBLIC_IPS,
4662 nodes, 0, LONGTIMELIMIT(),
4663 false, tdb_null,
4664 NULL, NULL, NULL) != 0) {
4665 DEBUG(DEBUG_ERR,
4666 ("Unable to reload IPs on some nodes, try again.\n"));
4667 goto again;
4670 /* It isn't strictly necessary to wait until takeover runs are
4671 * re-enabled but doing so can't hurt.
4673 timeout = 0;
4674 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, &timeout,
4675 "Enable takeover runs", true);
4677 ipreallocate(ctdb);
4679 ret = 0;
4680 done:
4681 talloc_free(tmp_ctx);
4682 return ret;
4686 display a list of the databases on a remote ctdb
4688 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
4690 int i, ret;
4691 struct ctdb_dbid_map *dbmap=NULL;
4693 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
4694 if (ret != 0) {
4695 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4696 return ret;
4699 if(options.machinereadable){
4700 printf(":ID:Name:Path:Persistent:Sticky:Unhealthy:ReadOnly:\n");
4701 for(i=0;i<dbmap->num;i++){
4702 const char *path;
4703 const char *name;
4704 const char *health;
4705 bool persistent;
4706 bool readonly;
4707 bool sticky;
4709 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
4710 dbmap->dbs[i].dbid, ctdb, &path);
4711 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
4712 dbmap->dbs[i].dbid, ctdb, &name);
4713 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
4714 dbmap->dbs[i].dbid, ctdb, &health);
4715 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4716 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4717 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4718 printf(":0x%08X:%s:%s:%d:%d:%d:%d:\n",
4719 dbmap->dbs[i].dbid, name, path,
4720 !!(persistent), !!(sticky),
4721 !!(health), !!(readonly));
4723 return 0;
4726 printf("Number of databases:%d\n", dbmap->num);
4727 for(i=0;i<dbmap->num;i++){
4728 const char *path;
4729 const char *name;
4730 const char *health;
4731 bool persistent;
4732 bool readonly;
4733 bool sticky;
4735 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
4736 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
4737 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
4738 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4739 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4740 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4741 printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
4742 dbmap->dbs[i].dbid, name, path,
4743 persistent?" PERSISTENT":"",
4744 sticky?" STICKY":"",
4745 readonly?" READONLY":"",
4746 health?" UNHEALTHY":"");
4749 return 0;
4753 display the status of a database on a remote ctdb
4755 static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
4757 const char *db_name;
4758 uint32_t db_id;
4759 uint8_t flags;
4760 const char *path;
4761 const char *health;
4763 if (argc < 1) {
4764 usage();
4767 if (!db_exists(ctdb, argv[0], &db_id, &db_name, &flags)) {
4768 return -1;
4771 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &path);
4772 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &health);
4773 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
4774 db_id, db_name, path,
4775 (flags & CTDB_DB_FLAGS_PERSISTENT ? "yes" : "no"),
4776 (flags & CTDB_DB_FLAGS_STICKY ? "yes" : "no"),
4777 (flags & CTDB_DB_FLAGS_READONLY ? "yes" : "no"),
4778 (health ? health : "OK"));
4780 return 0;
4784 check if the local node is recmaster or not
4785 it will return 1 if this node is the recmaster and 0 if it is not
4786 or if the local ctdb daemon could not be contacted
4788 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
4790 uint32_t mypnn, recmaster;
4791 int ret;
4793 assert_single_node_only();
4795 mypnn = getpnn(ctdb);
4797 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
4798 if (ret != 0) {
4799 printf("Failed to get the recmaster\n");
4800 return 1;
4803 if (recmaster != mypnn) {
4804 printf("this node is not the recmaster\n");
4805 return 1;
4808 printf("this node is the recmaster\n");
4809 return 0;
4813 ping a node
4815 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
4817 int ret;
4818 struct timeval tv = timeval_current();
4819 ret = ctdb_ctrl_ping(ctdb, options.pnn);
4820 if (ret == -1) {
4821 printf("Unable to get ping response from node %u\n", options.pnn);
4822 return -1;
4823 } else {
4824 printf("response from %u time=%.6f sec (%d clients)\n",
4825 options.pnn, timeval_elapsed(&tv), ret);
4827 return 0;
4832 get a node's runstate
4834 static int control_runstate(struct ctdb_context *ctdb, int argc, const char **argv)
4836 int ret;
4837 enum ctdb_runstate runstate;
4839 ret = ctdb_ctrl_get_runstate(ctdb, TIMELIMIT(), options.pnn, &runstate);
4840 if (ret == -1) {
4841 printf("Unable to get runstate response from node %u\n",
4842 options.pnn);
4843 return -1;
4844 } else {
4845 bool found = true;
4846 enum ctdb_runstate t;
4847 int i;
4848 for (i=0; i<argc; i++) {
4849 found = false;
4850 t = runstate_from_string(argv[i]);
4851 if (t == CTDB_RUNSTATE_UNKNOWN) {
4852 printf("Invalid run state (%s)\n", argv[i]);
4853 return -1;
4856 if (t == runstate) {
4857 found = true;
4858 break;
4862 if (!found) {
4863 printf("CTDB not in required run state (got %s)\n",
4864 runstate_to_string((enum ctdb_runstate)runstate));
4865 return -1;
4869 printf("%s\n", runstate_to_string(runstate));
4870 return 0;
4875 get a tunable
4877 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
4879 const char *name;
4880 uint32_t value;
4881 int ret;
4883 if (argc < 1) {
4884 usage();
4887 name = argv[0];
4888 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
4889 if (ret != 0) {
4890 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
4891 return -1;
4894 printf("%-23s = %u\n", name, value);
4895 return 0;
4899 set a tunable
4901 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
4903 const char *name;
4904 uint32_t value;
4905 int ret;
4907 if (argc < 2) {
4908 usage();
4911 name = argv[0];
4912 value = strtoul(argv[1], NULL, 0);
4914 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
4915 if (ret == -1) {
4916 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
4917 return -1;
4919 return 0;
4923 list all tunables
4925 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
4927 uint32_t count;
4928 const char **list;
4929 int ret, i;
4931 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
4932 if (ret == -1) {
4933 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
4934 return -1;
4937 for (i=0;i<count;i++) {
4938 control_getvar(ctdb, 1, &list[i]);
4941 talloc_free(list);
4943 return 0;
4947 display debug level on a node
4949 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4951 int ret;
4952 int32_t level;
4954 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
4955 if (ret != 0) {
4956 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
4957 return ret;
4958 } else {
4959 if (options.machinereadable){
4960 printf(":Name:Level:\n");
4961 printf(":%s:%d:\n",get_debug_by_level(level),level);
4962 } else {
4963 printf("Node %u is at debug level %s (%d)\n", options.pnn, get_debug_by_level(level), level);
4966 return 0;
4970 display reclock file of a node
4972 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4974 int ret;
4975 const char *reclock;
4977 ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
4978 if (ret != 0) {
4979 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4980 return ret;
4981 } else {
4982 if (options.machinereadable){
4983 if (reclock != NULL) {
4984 printf("%s", reclock);
4986 } else {
4987 if (reclock == NULL) {
4988 printf("No reclock file used.\n");
4989 } else {
4990 printf("Reclock file:%s\n", reclock);
4994 return 0;
4998 set the reclock file of a node
5000 static int control_setreclock(struct ctdb_context *ctdb, int argc, const char **argv)
5002 int ret;
5003 const char *reclock;
5005 if (argc == 0) {
5006 reclock = NULL;
5007 } else if (argc == 1) {
5008 reclock = argv[0];
5009 } else {
5010 usage();
5013 ret = ctdb_ctrl_setreclock(ctdb, TIMELIMIT(), options.pnn, reclock);
5014 if (ret != 0) {
5015 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
5016 return ret;
5018 return 0;
5022 set the natgw state on/off
5024 static int control_setnatgwstate(struct ctdb_context *ctdb, int argc, const char **argv)
5026 int ret;
5027 uint32_t natgwstate;
5029 if (argc == 0) {
5030 usage();
5033 if (!strcmp(argv[0], "on")) {
5034 natgwstate = 1;
5035 } else if (!strcmp(argv[0], "off")) {
5036 natgwstate = 0;
5037 } else {
5038 usage();
5041 ret = ctdb_ctrl_setnatgwstate(ctdb, TIMELIMIT(), options.pnn, natgwstate);
5042 if (ret != 0) {
5043 DEBUG(DEBUG_ERR, ("Unable to set the natgw state for node %u\n", options.pnn));
5044 return ret;
5047 return 0;
5051 set the lmaster role on/off
5053 static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
5055 int ret;
5056 uint32_t lmasterrole;
5058 if (argc == 0) {
5059 usage();
5062 if (!strcmp(argv[0], "on")) {
5063 lmasterrole = 1;
5064 } else if (!strcmp(argv[0], "off")) {
5065 lmasterrole = 0;
5066 } else {
5067 usage();
5070 ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
5071 if (ret != 0) {
5072 DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
5073 return ret;
5076 return 0;
5080 set the recmaster role on/off
5082 static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
5084 int ret;
5085 uint32_t recmasterrole;
5087 if (argc == 0) {
5088 usage();
5091 if (!strcmp(argv[0], "on")) {
5092 recmasterrole = 1;
5093 } else if (!strcmp(argv[0], "off")) {
5094 recmasterrole = 0;
5095 } else {
5096 usage();
5099 ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
5100 if (ret != 0) {
5101 DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
5102 return ret;
5105 return 0;
5109 set debug level on a node or all nodes
5111 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
5113 int i, ret;
5114 int32_t level;
5116 if (argc == 0) {
5117 printf("You must specify the debug level. Valid levels are:\n");
5118 for (i=0; debug_levels[i].description != NULL; i++) {
5119 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
5122 return 0;
5125 if (isalpha(argv[0][0]) || argv[0][0] == '-') {
5126 level = get_debug_by_desc(argv[0]);
5127 } else {
5128 level = strtol(argv[0], NULL, 0);
5131 for (i=0; debug_levels[i].description != NULL; i++) {
5132 if (level == debug_levels[i].level) {
5133 break;
5136 if (debug_levels[i].description == NULL) {
5137 printf("Invalid debug level, must be one of\n");
5138 for (i=0; debug_levels[i].description != NULL; i++) {
5139 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
5141 return -1;
5144 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
5145 if (ret != 0) {
5146 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
5148 return 0;
5153 thaw a node
5155 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
5157 int ret;
5158 uint32_t priority;
5160 if (argc == 1) {
5161 priority = strtol(argv[0], NULL, 0);
5162 } else {
5163 priority = 0;
5165 DEBUG(DEBUG_ERR,("Thaw by priority %u\n", priority));
5167 ret = ctdb_ctrl_thaw_priority(ctdb, TIMELIMIT(), options.pnn, priority);
5168 if (ret != 0) {
5169 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
5171 return 0;
5176 attach to a database
5178 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
5180 const char *db_name;
5181 struct ctdb_db_context *ctdb_db;
5182 bool persistent = false;
5184 if (argc < 1) {
5185 usage();
5187 db_name = argv[0];
5188 if (argc > 2) {
5189 usage();
5191 if (argc == 2) {
5192 if (strcmp(argv[1], "persistent") != 0) {
5193 usage();
5195 persistent = true;
5198 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
5199 if (ctdb_db == NULL) {
5200 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
5201 return -1;
5204 return 0;
5208 * detach from a database
5210 static int control_detach(struct ctdb_context *ctdb, int argc,
5211 const char **argv)
5213 uint32_t db_id;
5214 uint8_t flags;
5215 int ret, i, status = 0;
5216 struct ctdb_node_map *nodemap = NULL;
5217 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5218 uint32_t recmode;
5220 if (argc < 1) {
5221 usage();
5224 assert_single_node_only();
5226 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), options.pnn,
5227 &recmode);
5228 if (ret != 0) {
5229 DEBUG(DEBUG_ERR, ("Database cannot be detached "
5230 "when recovery is active\n"));
5231 talloc_free(tmp_ctx);
5232 return -1;
5235 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5236 &nodemap);
5237 if (ret != 0) {
5238 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5239 options.pnn));
5240 talloc_free(tmp_ctx);
5241 return -1;
5244 for (i=0; i<nodemap->num; i++) {
5245 uint32_t value;
5247 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
5248 continue;
5251 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
5252 continue;
5255 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
5256 DEBUG(DEBUG_ERR, ("Database cannot be detached on "
5257 "inactive (stopped or banned) node "
5258 "%u\n", nodemap->nodes[i].pnn));
5259 talloc_free(tmp_ctx);
5260 return -1;
5263 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(),
5264 nodemap->nodes[i].pnn,
5265 "AllowClientDBAttach",
5266 &value);
5267 if (ret != 0) {
5268 DEBUG(DEBUG_ERR, ("Unable to get tunable "
5269 "AllowClientDBAttach from node %u\n",
5270 nodemap->nodes[i].pnn));
5271 talloc_free(tmp_ctx);
5272 return -1;
5275 if (value == 1) {
5276 DEBUG(DEBUG_ERR, ("Database access is still active on "
5277 "node %u. Set AllowClientDBAttach=0 "
5278 "on all nodes.\n",
5279 nodemap->nodes[i].pnn));
5280 talloc_free(tmp_ctx);
5281 return -1;
5285 talloc_free(tmp_ctx);
5287 for (i=0; i<argc; i++) {
5288 if (!db_exists(ctdb, argv[i], &db_id, NULL, &flags)) {
5289 continue;
5292 if (flags & CTDB_DB_FLAGS_PERSISTENT) {
5293 DEBUG(DEBUG_ERR, ("Persistent database '%s' "
5294 "cannot be detached\n", argv[i]));
5295 status = -1;
5296 continue;
5299 ret = ctdb_detach(ctdb, db_id);
5300 if (ret != 0) {
5301 DEBUG(DEBUG_ERR, ("Database '%s' detach failed\n",
5302 argv[i]));
5303 status = ret;
5307 return status;
5311 set db priority
5313 static int control_setdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
5315 struct ctdb_db_priority db_prio;
5316 int ret;
5318 if (argc < 2) {
5319 usage();
5322 db_prio.db_id = strtoul(argv[0], NULL, 0);
5323 db_prio.priority = strtoul(argv[1], NULL, 0);
5325 ret = ctdb_ctrl_set_db_priority(ctdb, TIMELIMIT(), options.pnn, &db_prio);
5326 if (ret != 0) {
5327 DEBUG(DEBUG_ERR,("Unable to set db prio\n"));
5328 return -1;
5331 return 0;
5335 get db priority
5337 static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
5339 uint32_t db_id, priority;
5340 int ret;
5342 if (argc < 1) {
5343 usage();
5346 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5347 return -1;
5350 ret = ctdb_ctrl_get_db_priority(ctdb, TIMELIMIT(), options.pnn, db_id, &priority);
5351 if (ret != 0) {
5352 DEBUG(DEBUG_ERR,("Unable to get db prio\n"));
5353 return -1;
5356 DEBUG(DEBUG_ERR,("Priority:%u\n", priority));
5358 return 0;
5362 set the sticky records capability for a database
5364 static int control_setdbsticky(struct ctdb_context *ctdb, int argc, const char **argv)
5366 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5367 uint32_t db_id;
5368 int ret;
5370 if (argc < 1) {
5371 usage();
5374 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5375 return -1;
5378 ret = ctdb_ctrl_set_db_sticky(ctdb, options.pnn, db_id);
5379 if (ret != 0) {
5380 DEBUG(DEBUG_ERR,("Unable to set db to support sticky records\n"));
5381 talloc_free(tmp_ctx);
5382 return -1;
5385 talloc_free(tmp_ctx);
5386 return 0;
5390 set the readonly capability for a database
5392 static int control_setdbreadonly(struct ctdb_context *ctdb, int argc, const char **argv)
5394 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5395 uint32_t db_id;
5396 int ret;
5398 if (argc < 1) {
5399 usage();
5402 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5403 return -1;
5406 ret = ctdb_ctrl_set_db_readonly(ctdb, options.pnn, db_id);
5407 if (ret != 0) {
5408 DEBUG(DEBUG_ERR,("Unable to set db to support readonly\n"));
5409 talloc_free(tmp_ctx);
5410 return -1;
5413 talloc_free(tmp_ctx);
5414 return 0;
5418 get db seqnum
5420 static int control_getdbseqnum(struct ctdb_context *ctdb, int argc, const char **argv)
5422 uint32_t db_id;
5423 uint64_t seqnum;
5424 int ret;
5426 if (argc < 1) {
5427 usage();
5430 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5431 return -1;
5434 ret = ctdb_ctrl_getdbseqnum(ctdb, TIMELIMIT(), options.pnn, db_id, &seqnum);
5435 if (ret != 0) {
5436 DEBUG(DEBUG_ERR, ("Unable to get seqnum from node."));
5437 return -1;
5440 printf("Sequence number:%lld\n", (long long)seqnum);
5442 return 0;
5446 run an eventscript on a node
5448 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
5450 TDB_DATA data;
5451 int ret;
5452 int32_t res;
5453 char *errmsg;
5454 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5456 if (argc != 1) {
5457 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5458 return -1;
5461 data.dptr = (unsigned char *)discard_const(argv[0]);
5462 data.dsize = strlen((char *)data.dptr) + 1;
5464 DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
5466 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
5467 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
5468 if (ret != 0 || res != 0) {
5469 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
5470 talloc_free(tmp_ctx);
5471 return -1;
5473 talloc_free(tmp_ctx);
5474 return 0;
5477 #define DB_VERSION 1
5478 #define MAX_DB_NAME 64
5479 struct db_file_header {
5480 unsigned long version;
5481 time_t timestamp;
5482 unsigned long persistent;
5483 unsigned long size;
5484 const char name[MAX_DB_NAME];
5487 struct backup_data {
5488 struct ctdb_marshall_buffer *records;
5489 uint32_t len;
5490 uint32_t total;
5491 bool traverse_error;
5494 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
5496 struct backup_data *bd = talloc_get_type(private, struct backup_data);
5497 struct ctdb_rec_data *rec;
5499 /* add the record */
5500 rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
5501 if (rec == NULL) {
5502 bd->traverse_error = true;
5503 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
5504 return -1;
5506 bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
5507 if (bd->records == NULL) {
5508 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
5509 bd->traverse_error = true;
5510 return -1;
5512 bd->records->count++;
5513 memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
5514 bd->len += rec->length;
5515 talloc_free(rec);
5517 bd->total++;
5518 return 0;
5522 * backup a database to a file
5524 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
5526 const char *db_name;
5527 int ret;
5528 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5529 struct db_file_header dbhdr;
5530 struct ctdb_db_context *ctdb_db;
5531 struct backup_data *bd;
5532 int fh = -1;
5533 int status = -1;
5534 const char *reason = NULL;
5535 uint32_t db_id;
5536 uint8_t flags;
5538 assert_single_node_only();
5540 if (argc != 2) {
5541 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5542 return -1;
5545 if (!db_exists(ctdb, argv[0], &db_id, &db_name, &flags)) {
5546 return -1;
5549 ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
5550 db_id, tmp_ctx, &reason);
5551 if (ret != 0) {
5552 DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
5553 argv[0]));
5554 talloc_free(tmp_ctx);
5555 return -1;
5557 if (reason) {
5558 uint32_t allow_unhealthy = 0;
5560 ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
5561 "AllowUnhealthyDBRead",
5562 &allow_unhealthy);
5564 if (allow_unhealthy != 1) {
5565 DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
5566 argv[0], reason));
5568 DEBUG(DEBUG_ERR,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
5569 allow_unhealthy));
5570 talloc_free(tmp_ctx);
5571 return -1;
5574 DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
5575 argv[0], argv[0]));
5576 DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
5577 "tunnable AllowUnhealthyDBRead = %u\n",
5578 allow_unhealthy));
5581 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5582 if (ctdb_db == NULL) {
5583 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
5584 talloc_free(tmp_ctx);
5585 return -1;
5589 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
5590 if (ret == -1) {
5591 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
5592 talloc_free(tmp_ctx);
5593 return -1;
5597 bd = talloc_zero(tmp_ctx, struct backup_data);
5598 if (bd == NULL) {
5599 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
5600 talloc_free(tmp_ctx);
5601 return -1;
5604 bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
5605 if (bd->records == NULL) {
5606 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
5607 talloc_free(tmp_ctx);
5608 return -1;
5611 bd->len = offsetof(struct ctdb_marshall_buffer, data);
5612 bd->records->db_id = ctdb_db->db_id;
5613 /* traverse the database collecting all records */
5614 if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
5615 bd->traverse_error) {
5616 DEBUG(DEBUG_ERR,("Traverse error\n"));
5617 talloc_free(tmp_ctx);
5618 return -1;
5621 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
5624 fh = open(argv[1], O_RDWR|O_CREAT, 0600);
5625 if (fh == -1) {
5626 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
5627 talloc_free(tmp_ctx);
5628 return -1;
5631 ZERO_STRUCT(dbhdr);
5632 dbhdr.version = DB_VERSION;
5633 dbhdr.timestamp = time(NULL);
5634 dbhdr.persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
5635 dbhdr.size = bd->len;
5636 if (strlen(argv[0]) >= MAX_DB_NAME) {
5637 DEBUG(DEBUG_ERR,("Too long dbname\n"));
5638 goto done;
5640 strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME-1);
5641 ret = sys_write(fh, &dbhdr, sizeof(dbhdr));
5642 if (ret == -1) {
5643 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5644 goto done;
5646 ret = sys_write(fh, bd->records, bd->len);
5647 if (ret == -1) {
5648 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5649 goto done;
5652 status = 0;
5653 done:
5654 if (fh != -1) {
5655 ret = close(fh);
5656 if (ret == -1) {
5657 DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
5661 DEBUG(DEBUG_ERR,("Database backed up to %s\n", argv[1]));
5663 talloc_free(tmp_ctx);
5664 return status;
5668 * restore a database from a file
5670 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
5672 int ret;
5673 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5674 TDB_DATA outdata;
5675 TDB_DATA data;
5676 struct db_file_header dbhdr;
5677 struct ctdb_db_context *ctdb_db;
5678 struct ctdb_node_map *nodemap=NULL;
5679 struct ctdb_vnn_map *vnnmap=NULL;
5680 int i, fh;
5681 struct ctdb_control_wipe_database w;
5682 uint32_t *nodes;
5683 uint32_t generation;
5684 struct tm *tm;
5685 char tbuf[100];
5686 char *dbname;
5688 assert_single_node_only();
5690 if (argc < 1 || argc > 2) {
5691 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5692 return -1;
5695 fh = open(argv[0], O_RDONLY);
5696 if (fh == -1) {
5697 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5698 talloc_free(tmp_ctx);
5699 return -1;
5702 sys_read(fh, &dbhdr, sizeof(dbhdr));
5703 if (dbhdr.version != DB_VERSION) {
5704 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5705 close(fh);
5706 talloc_free(tmp_ctx);
5707 return -1;
5710 dbname = discard_const(dbhdr.name);
5711 if (argc == 2) {
5712 dbname = discard_const(argv[1]);
5715 outdata.dsize = dbhdr.size;
5716 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5717 if (outdata.dptr == NULL) {
5718 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5719 close(fh);
5720 talloc_free(tmp_ctx);
5721 return -1;
5723 sys_read(fh, outdata.dptr, outdata.dsize);
5724 close(fh);
5726 tm = localtime(&dbhdr.timestamp);
5727 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5728 printf("Restoring database '%s' from backup @ %s\n",
5729 dbname, tbuf);
5732 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), dbname, dbhdr.persistent, 0);
5733 if (ctdb_db == NULL) {
5734 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbname));
5735 talloc_free(tmp_ctx);
5736 return -1;
5739 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
5740 if (ret != 0) {
5741 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
5742 talloc_free(tmp_ctx);
5743 return ret;
5747 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
5748 if (ret != 0) {
5749 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
5750 talloc_free(tmp_ctx);
5751 return ret;
5754 /* freeze all nodes */
5755 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5756 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5757 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5758 nodes, i,
5759 TIMELIMIT(),
5760 false, tdb_null,
5761 NULL, NULL,
5762 NULL) != 0) {
5763 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5764 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5765 talloc_free(tmp_ctx);
5766 return -1;
5770 generation = vnnmap->generation;
5771 data.dptr = (void *)&generation;
5772 data.dsize = sizeof(generation);
5774 /* start a cluster wide transaction */
5775 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5776 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5777 nodes, 0,
5778 TIMELIMIT(), false, data,
5779 NULL, NULL,
5780 NULL) != 0) {
5781 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
5782 return -1;
5786 w.db_id = ctdb_db->db_id;
5787 w.transaction_id = generation;
5789 data.dptr = (void *)&w;
5790 data.dsize = sizeof(w);
5792 /* wipe all the remote databases. */
5793 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5794 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5795 nodes, 0,
5796 TIMELIMIT(), false, data,
5797 NULL, NULL,
5798 NULL) != 0) {
5799 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5800 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5801 talloc_free(tmp_ctx);
5802 return -1;
5805 /* push the database */
5806 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5807 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
5808 nodes, 0,
5809 TIMELIMIT(), false, outdata,
5810 NULL, NULL,
5811 NULL) != 0) {
5812 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
5813 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5814 talloc_free(tmp_ctx);
5815 return -1;
5818 data.dptr = (void *)&ctdb_db->db_id;
5819 data.dsize = sizeof(ctdb_db->db_id);
5821 /* mark the database as healthy */
5822 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5823 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5824 nodes, 0,
5825 TIMELIMIT(), false, data,
5826 NULL, NULL,
5827 NULL) != 0) {
5828 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5829 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5830 talloc_free(tmp_ctx);
5831 return -1;
5834 data.dptr = (void *)&generation;
5835 data.dsize = sizeof(generation);
5837 /* commit all the changes */
5838 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5839 nodes, 0,
5840 TIMELIMIT(), false, data,
5841 NULL, NULL,
5842 NULL) != 0) {
5843 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5844 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5845 talloc_free(tmp_ctx);
5846 return -1;
5850 /* thaw all nodes */
5851 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5852 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5853 nodes, 0,
5854 TIMELIMIT(),
5855 false, tdb_null,
5856 NULL, NULL,
5857 NULL) != 0) {
5858 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5859 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5860 talloc_free(tmp_ctx);
5861 return -1;
5865 talloc_free(tmp_ctx);
5866 return 0;
5870 * dump a database backup from a file
5872 static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
5874 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5875 TDB_DATA outdata;
5876 struct db_file_header dbhdr;
5877 int i, fh;
5878 struct tm *tm;
5879 char tbuf[100];
5880 struct ctdb_rec_data *rec = NULL;
5881 struct ctdb_marshall_buffer *m;
5882 struct ctdb_dump_db_context c;
5884 assert_single_node_only();
5886 if (argc != 1) {
5887 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5888 return -1;
5891 fh = open(argv[0], O_RDONLY);
5892 if (fh == -1) {
5893 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5894 talloc_free(tmp_ctx);
5895 return -1;
5898 sys_read(fh, &dbhdr, sizeof(dbhdr));
5899 if (dbhdr.version != DB_VERSION) {
5900 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5901 close(fh);
5902 talloc_free(tmp_ctx);
5903 return -1;
5906 outdata.dsize = dbhdr.size;
5907 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5908 if (outdata.dptr == NULL) {
5909 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5910 close(fh);
5911 talloc_free(tmp_ctx);
5912 return -1;
5914 sys_read(fh, outdata.dptr, outdata.dsize);
5915 close(fh);
5916 m = (struct ctdb_marshall_buffer *)outdata.dptr;
5918 tm = localtime(&dbhdr.timestamp);
5919 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5920 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
5921 dbhdr.name, m->db_id, tbuf);
5923 ZERO_STRUCT(c);
5924 c.f = stdout;
5925 c.printemptyrecords = (bool)options.printemptyrecords;
5926 c.printdatasize = (bool)options.printdatasize;
5927 c.printlmaster = false;
5928 c.printhash = (bool)options.printhash;
5929 c.printrecordflags = (bool)options.printrecordflags;
5931 for (i=0; i < m->count; i++) {
5932 uint32_t reqid = 0;
5933 TDB_DATA key, data;
5935 /* we do not want the header splitted, so we pass NULL*/
5936 rec = ctdb_marshall_loop_next(m, rec, &reqid,
5937 NULL, &key, &data);
5939 ctdb_dumpdb_record(ctdb, key, data, &c);
5942 printf("Dumped %d records\n", i);
5943 talloc_free(tmp_ctx);
5944 return 0;
5948 * wipe a database from a file
5950 static int control_wipedb(struct ctdb_context *ctdb, int argc,
5951 const char **argv)
5953 const char *db_name;
5954 int ret;
5955 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5956 TDB_DATA data;
5957 struct ctdb_db_context *ctdb_db;
5958 struct ctdb_node_map *nodemap = NULL;
5959 struct ctdb_vnn_map *vnnmap = NULL;
5960 int i;
5961 struct ctdb_control_wipe_database w;
5962 uint32_t *nodes;
5963 uint32_t generation;
5964 uint8_t flags;
5966 assert_single_node_only();
5968 if (argc != 1) {
5969 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5970 return -1;
5973 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
5974 return -1;
5977 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5978 if (ctdb_db == NULL) {
5979 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
5980 argv[0]));
5981 talloc_free(tmp_ctx);
5982 return -1;
5985 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
5986 &nodemap);
5987 if (ret != 0) {
5988 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5989 options.pnn));
5990 talloc_free(tmp_ctx);
5991 return ret;
5994 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5995 &vnnmap);
5996 if (ret != 0) {
5997 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
5998 options.pnn));
5999 talloc_free(tmp_ctx);
6000 return ret;
6003 /* freeze all nodes */
6004 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
6005 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
6006 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
6007 nodes, i,
6008 TIMELIMIT(),
6009 false, tdb_null,
6010 NULL, NULL,
6011 NULL);
6012 if (ret != 0) {
6013 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
6014 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
6015 CTDB_RECOVERY_ACTIVE);
6016 talloc_free(tmp_ctx);
6017 return -1;
6021 generation = vnnmap->generation;
6022 data.dptr = (void *)&generation;
6023 data.dsize = sizeof(generation);
6025 /* start a cluster wide transaction */
6026 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
6027 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
6028 nodes, 0,
6029 TIMELIMIT(), false, data,
6030 NULL, NULL,
6031 NULL);
6032 if (ret!= 0) {
6033 DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
6034 "transactions.\n"));
6035 return -1;
6038 w.db_id = ctdb_db->db_id;
6039 w.transaction_id = generation;
6041 data.dptr = (void *)&w;
6042 data.dsize = sizeof(w);
6044 /* wipe all the remote databases. */
6045 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
6046 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
6047 nodes, 0,
6048 TIMELIMIT(), false, data,
6049 NULL, NULL,
6050 NULL) != 0) {
6051 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
6052 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
6053 talloc_free(tmp_ctx);
6054 return -1;
6057 data.dptr = (void *)&ctdb_db->db_id;
6058 data.dsize = sizeof(ctdb_db->db_id);
6060 /* mark the database as healthy */
6061 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
6062 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
6063 nodes, 0,
6064 TIMELIMIT(), false, data,
6065 NULL, NULL,
6066 NULL) != 0) {
6067 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
6068 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
6069 talloc_free(tmp_ctx);
6070 return -1;
6073 data.dptr = (void *)&generation;
6074 data.dsize = sizeof(generation);
6076 /* commit all the changes */
6077 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
6078 nodes, 0,
6079 TIMELIMIT(), false, data,
6080 NULL, NULL,
6081 NULL) != 0) {
6082 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
6083 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
6084 talloc_free(tmp_ctx);
6085 return -1;
6088 /* thaw all nodes */
6089 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
6090 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
6091 nodes, 0,
6092 TIMELIMIT(),
6093 false, tdb_null,
6094 NULL, NULL,
6095 NULL) != 0) {
6096 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
6097 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
6098 talloc_free(tmp_ctx);
6099 return -1;
6102 DEBUG(DEBUG_ERR, ("Database wiped.\n"));
6104 talloc_free(tmp_ctx);
6105 return 0;
6109 dump memory usage
6111 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
6113 TDB_DATA data;
6114 int ret;
6115 int32_t res;
6116 char *errmsg;
6117 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
6118 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
6119 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
6120 if (ret != 0 || res != 0) {
6121 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
6122 talloc_free(tmp_ctx);
6123 return -1;
6125 sys_write(1, data.dptr, data.dsize);
6126 talloc_free(tmp_ctx);
6127 return 0;
6131 handler for memory dumps
6133 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid,
6134 TDB_DATA data, void *private_data)
6136 sys_write(1, data.dptr, data.dsize);
6137 exit(0);
6141 dump memory usage on the recovery daemon
6143 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
6145 int ret;
6146 TDB_DATA data;
6147 struct srvid_request rd;
6149 rd.pnn = ctdb_get_pnn(ctdb);
6150 rd.srvid = getpid();
6152 /* register a message port for receiveing the reply so that we
6153 can receive the reply
6155 ctdb_client_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
6158 data.dptr = (uint8_t *)&rd;
6159 data.dsize = sizeof(rd);
6161 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
6162 if (ret != 0) {
6163 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
6164 return -1;
6167 /* this loop will terminate when we have received the reply */
6168 while (1) {
6169 event_loop_once(ctdb->ev);
6172 return 0;
6176 send a message to a srvid
6178 static int control_msgsend(struct ctdb_context *ctdb, int argc, const char **argv)
6180 unsigned long srvid;
6181 int ret;
6182 TDB_DATA data;
6184 if (argc < 2) {
6185 usage();
6188 srvid = strtoul(argv[0], NULL, 0);
6190 data.dptr = (uint8_t *)discard_const(argv[1]);
6191 data.dsize= strlen(argv[1]);
6193 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, srvid, data);
6194 if (ret != 0) {
6195 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
6196 return -1;
6199 return 0;
6203 handler for msglisten
6205 static void msglisten_handler(struct ctdb_context *ctdb, uint64_t srvid,
6206 TDB_DATA data, void *private_data)
6208 int i;
6210 printf("Message received: ");
6211 for (i=0;i<data.dsize;i++) {
6212 printf("%c", data.dptr[i]);
6214 printf("\n");
6218 listen for messages on a messageport
6220 static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
6222 uint64_t srvid;
6224 srvid = getpid();
6226 /* register a message port and listen for messages
6228 ctdb_client_set_message_handler(ctdb, srvid, msglisten_handler, NULL);
6229 printf("Listening for messages on srvid:%d\n", (int)srvid);
6231 while (1) {
6232 event_loop_once(ctdb->ev);
6235 return 0;
6239 list all nodes in the cluster
6240 we parse the nodes file directly
6242 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
6244 TALLOC_CTX *mem_ctx = talloc_new(NULL);
6245 struct pnn_node *pnn_nodes;
6246 struct pnn_node *pnn_node;
6248 assert_single_node_only();
6250 pnn_nodes = read_nodes_file(mem_ctx);
6251 if (pnn_nodes == NULL) {
6252 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
6253 talloc_free(mem_ctx);
6254 return -1;
6257 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
6258 const char *addr = ctdb_addr_to_str(&pnn_node->addr);
6259 if (options.machinereadable){
6260 printf(":%d:%s:\n", pnn_node->pnn, addr);
6261 } else {
6262 printf("%s\n", addr);
6265 talloc_free(mem_ctx);
6267 return 0;
6271 reload the nodes file on the local node
6273 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
6275 int i, ret;
6276 int mypnn;
6277 struct ctdb_node_map *nodemap=NULL;
6279 assert_single_node_only();
6281 mypnn = ctdb_get_pnn(ctdb);
6283 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
6284 if (ret != 0) {
6285 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
6286 return ret;
6289 /* reload the nodes file on all remote nodes */
6290 for (i=0;i<nodemap->num;i++) {
6291 if (nodemap->nodes[i].pnn == mypnn) {
6292 continue;
6294 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
6295 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
6296 nodemap->nodes[i].pnn);
6297 if (ret != 0) {
6298 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
6302 /* reload the nodes file on the local node */
6303 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
6304 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
6305 if (ret != 0) {
6306 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
6309 /* initiate a recovery */
6310 control_recover(ctdb, argc, argv);
6312 return 0;
6316 static const struct {
6317 const char *name;
6318 int (*fn)(struct ctdb_context *, int, const char **);
6319 bool auto_all;
6320 bool without_daemon; /* can be run without daemon running ? */
6321 const char *msg;
6322 const char *args;
6323 } ctdb_commands[] = {
6324 { "version", control_version, true, true, "show version of ctdb" },
6325 { "status", control_status, true, false, "show node status" },
6326 { "uptime", control_uptime, true, false, "show node uptime" },
6327 { "ping", control_ping, true, false, "ping all nodes" },
6328 { "runstate", control_runstate, true, false, "get/check runstate of a node", "[setup|first_recovery|startup|running]" },
6329 { "getvar", control_getvar, true, false, "get a tunable variable", "<name>"},
6330 { "setvar", control_setvar, true, false, "set a tunable variable", "<name> <value>"},
6331 { "listvars", control_listvars, true, false, "list tunable variables"},
6332 { "statistics", control_statistics, false, false, "show statistics" },
6333 { "statisticsreset", control_statistics_reset, true, false, "reset statistics"},
6334 { "stats", control_stats, false, false, "show rolling statistics", "[number of history records]" },
6335 { "ip", control_ip, false, false, "show which public ip's that ctdb manages" },
6336 { "ipinfo", control_ipinfo, true, false, "show details about a public ip that ctdb manages", "<ip>" },
6337 { "ifaces", control_ifaces, true, false, "show which interfaces that ctdb manages" },
6338 { "setifacelink", control_setifacelink, true, false, "set interface link status", "<iface> <status>" },
6339 { "process-exists", control_process_exists, true, false, "check if a process exists on a node", "<pid>"},
6340 { "getdbmap", control_getdbmap, true, false, "show the database map" },
6341 { "getdbstatus", control_getdbstatus, true, false, "show the status of a database", "<dbname|dbid>" },
6342 { "catdb", control_catdb, true, false, "dump a ctdb database" , "<dbname|dbid>"},
6343 { "cattdb", control_cattdb, true, false, "dump a local tdb database" , "<dbname|dbid>"},
6344 { "getmonmode", control_getmonmode, true, false, "show monitoring mode" },
6345 { "getcapabilities", control_getcapabilities, true, false, "show node capabilities" },
6346 { "pnn", control_pnn, true, false, "show the pnn of the currnet node" },
6347 { "lvs", control_lvs, true, false, "show lvs configuration" },
6348 { "lvsmaster", control_lvsmaster, true, false, "show which node is the lvs master" },
6349 { "disablemonitor", control_disable_monmode,true, false, "set monitoring mode to DISABLE" },
6350 { "enablemonitor", control_enable_monmode, true, false, "set monitoring mode to ACTIVE" },
6351 { "setdebug", control_setdebug, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
6352 { "getdebug", control_getdebug, true, false, "get debug level" },
6353 { "getlog", control_getlog, true, false, "get the log data from the in memory ringbuffer", "[<level>] [recoverd]" },
6354 { "clearlog", control_clearlog, true, false, "clear the log data from the in memory ringbuffer", "[recoverd]" },
6355 { "attach", control_attach, true, false, "attach to a database", "<dbname> [persistent]" },
6356 { "detach", control_detach, false, false, "detach from a database", "<dbname|dbid> [<dbname|dbid> ...]" },
6357 { "dumpmemory", control_dumpmemory, true, false, "dump memory map to stdout" },
6358 { "rddumpmemory", control_rddumpmemory, true, false, "dump memory map from the recovery daemon to stdout" },
6359 { "getpid", control_getpid, true, false, "get ctdbd process ID" },
6360 { "disable", control_disable, true, false, "disable a nodes public IP" },
6361 { "enable", control_enable, true, false, "enable a nodes public IP" },
6362 { "stop", control_stop, true, false, "stop a node" },
6363 { "continue", control_continue, true, false, "re-start a stopped node" },
6364 { "ban", control_ban, true, false, "ban a node from the cluster", "<bantime>"},
6365 { "unban", control_unban, true, false, "unban a node" },
6366 { "showban", control_showban, true, false, "show ban information"},
6367 { "shutdown", control_shutdown, true, false, "shutdown ctdbd" },
6368 { "recover", control_recover, true, false, "force recovery" },
6369 { "sync", control_ipreallocate, false, false, "wait until ctdbd has synced all state changes" },
6370 { "ipreallocate", control_ipreallocate, false, false, "force the recovery daemon to perform a ip reallocation procedure" },
6371 { "thaw", control_thaw, true, false, "thaw databases", "[priority:1-3]" },
6372 { "isnotrecmaster", control_isnotrecmaster, false, false, "check if the local node is recmaster or not" },
6373 { "killtcp", kill_tcp, false, false, "kill a tcp connection.", "[<srcip:port> <dstip:port>]" },
6374 { "gratiousarp", control_gratious_arp, false, false, "send a gratious arp", "<ip> <interface>" },
6375 { "tickle", tickle_tcp, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
6376 { "gettickles", control_get_tickles, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
6377 { "addtickle", control_add_tickle, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
6379 { "deltickle", control_del_tickle, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
6381 { "regsrvid", regsrvid, false, false, "register a server id", "<pnn> <type> <id>" },
6382 { "unregsrvid", unregsrvid, false, false, "unregister a server id", "<pnn> <type> <id>" },
6383 { "chksrvid", chksrvid, false, false, "check if a server id exists", "<pnn> <type> <id>" },
6384 { "getsrvids", getsrvids, false, false, "get a list of all server ids"},
6385 { "check_srvids", check_srvids, false, false, "check if a srvid exists", "<id>+" },
6386 { "repack", ctdb_repack, false, false, "repack all databases", "[max_freelist]"},
6387 { "listnodes", control_listnodes, false, true, "list all nodes in the cluster"},
6388 { "reloadnodes", control_reload_nodes_file, false, false, "reload the nodes file and restart the transport on all nodes"},
6389 { "moveip", control_moveip, false, false, "move/failover an ip address to another node", "<ip> <node>"},
6390 { "rebalanceip", control_rebalanceip, false, false, "release an ip from the node and let recd rebalance it", "<ip>"},
6391 { "addip", control_addip, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
6392 { "delip", control_delip, false, false, "delete an ip address from a node", "<ip>"},
6393 { "eventscript", control_eventscript, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
6394 { "backupdb", control_backupdb, false, false, "backup the database into a file.", "<dbname|dbid> <file>"},
6395 { "restoredb", control_restoredb, false, false, "restore the database from a file.", "<file> [dbname]"},
6396 { "dumpdbbackup", control_dumpdbbackup, false, true, "dump database backup from a file.", "<file>"},
6397 { "wipedb", control_wipedb, false, false, "wipe the contents of a database.", "<dbname|dbid>"},
6398 { "recmaster", control_recmaster, true, false, "show the pnn for the recovery master."},
6399 { "scriptstatus", control_scriptstatus, true, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
6400 { "enablescript", control_enablescript, true, false, "enable an eventscript", "<script>"},
6401 { "disablescript", control_disablescript, true, false, "disable an eventscript", "<script>"},
6402 { "natgwlist", control_natgwlist, true, false, "show the nodes belonging to this natgw configuration"},
6403 { "xpnn", control_xpnn, false, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
6404 { "getreclock", control_getreclock, true, false, "Show the reclock file of a node"},
6405 { "setreclock", control_setreclock, true, false, "Set/clear the reclock file of a node", "[filename]"},
6406 { "setnatgwstate", control_setnatgwstate, false, false, "Set NATGW state to on/off", "{on|off}"},
6407 { "setlmasterrole", control_setlmasterrole, false, false, "Set LMASTER role to on/off", "{on|off}"},
6408 { "setrecmasterrole", control_setrecmasterrole, false, false, "Set RECMASTER role to on/off", "{on|off}"},
6409 { "setdbprio", control_setdbprio, false, false, "Set DB priority", "<dbname|dbid> <prio:1-3>"},
6410 { "getdbprio", control_getdbprio, false, false, "Get DB priority", "<dbname|dbid>"},
6411 { "setdbreadonly", control_setdbreadonly, false, false, "Set DB readonly capable", "<dbname|dbid>"},
6412 { "setdbsticky", control_setdbsticky, false, false, "Set DB sticky-records capable", "<dbname|dbid>"},
6413 { "msglisten", control_msglisten, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
6414 { "msgsend", control_msgsend, false, false, "Send a message to srvid", "<srvid> <message>"},
6415 { "pfetch", control_pfetch, false, false, "fetch a record from a persistent database", "<dbname|dbid> <key> [<file>]" },
6416 { "pstore", control_pstore, false, false, "write a record to a persistent database", "<dbname|dbid> <key> <file containing record>" },
6417 { "pdelete", control_pdelete, false, false, "delete a record from a persistent database", "<dbname|dbid> <key>" },
6418 { "ptrans", control_ptrans, false, false, "update a persistent database (from stdin)", "<dbname|dbid>" },
6419 { "tfetch", control_tfetch, false, true, "fetch a record from a [c]tdb-file [-v]", "<tdb-file> <key> [<file>]" },
6420 { "tstore", control_tstore, false, true, "store a record (including ltdb header)", "<tdb-file> <key> <data> [<rsn> <dmaster> <flags>]" },
6421 { "readkey", control_readkey, true, false, "read the content off a database key", "<dbname|dbid> <key>" },
6422 { "writekey", control_writekey, true, false, "write to a database key", "<dbname|dbid> <key> <value>" },
6423 { "checktcpport", control_chktcpport, false, true, "check if a service is bound to a specific tcp port or not", "<port>" },
6424 { "rebalancenode", control_rebalancenode, false, false, "mark nodes as forced IP rebalancing targets", "[<pnn-list>]"},
6425 { "getdbseqnum", control_getdbseqnum, false, false, "get the sequence number off a database", "<dbname|dbid>" },
6426 { "nodestatus", control_nodestatus, true, false, "show and return node status", "[<pnn-list>]" },
6427 { "dbstatistics", control_dbstatistics, false, false, "show db statistics", "<dbname|dbid>" },
6428 { "reloadips", control_reloadips, false, false, "reload the public addresses file on specified nodes" , "[<pnn-list>]" },
6429 { "ipiface", control_ipiface, false, true, "Find which interface an ip address is hosted on", "<ip>" },
6433 show usage message
6435 static void usage(void)
6437 int i;
6438 printf(
6439 "Usage: ctdb [options] <control>\n" \
6440 "Options:\n" \
6441 " -n <node> choose node number, or 'all' (defaults to local node)\n"
6442 " -Y generate machinereadable output\n"
6443 " -v generate verbose output\n"
6444 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
6445 printf("Controls:\n");
6446 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6447 printf(" %-15s %-27s %s\n",
6448 ctdb_commands[i].name,
6449 ctdb_commands[i].args?ctdb_commands[i].args:"",
6450 ctdb_commands[i].msg);
6452 exit(1);
6456 static void ctdb_alarm(int sig)
6458 printf("Maximum runtime exceeded - exiting\n");
6459 _exit(ERR_TIMEOUT);
6463 main program
6465 int main(int argc, const char *argv[])
6467 struct ctdb_context *ctdb;
6468 char *nodestring = NULL;
6469 struct poptOption popt_options[] = {
6470 POPT_AUTOHELP
6471 POPT_CTDB_CMDLINE
6472 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
6473 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
6474 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
6475 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
6476 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
6477 { "print-emptyrecords", 0, POPT_ARG_NONE, &options.printemptyrecords, 0, "print the empty records when dumping databases (catdb, cattdb, dumpdbbackup)", NULL },
6478 { "print-datasize", 0, POPT_ARG_NONE, &options.printdatasize, 0, "do not print record data when dumping databases, only the data size", NULL },
6479 { "print-lmaster", 0, POPT_ARG_NONE, &options.printlmaster, 0, "print the record's lmaster in catdb", NULL },
6480 { "print-hash", 0, POPT_ARG_NONE, &options.printhash, 0, "print the record's hash when dumping databases", NULL },
6481 { "print-recordflags", 0, POPT_ARG_NONE, &options.printrecordflags, 0, "print the record flags in catdb and dumpdbbackup", NULL },
6482 POPT_TABLEEND
6484 int opt;
6485 const char **extra_argv;
6486 int extra_argc = 0;
6487 int ret=-1, i;
6488 poptContext pc;
6489 struct event_context *ev;
6490 const char *control;
6492 setlinebuf(stdout);
6494 /* set some defaults */
6495 options.maxruntime = 0;
6496 options.timelimit = 10;
6497 options.pnn = CTDB_CURRENT_NODE;
6499 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
6501 while ((opt = poptGetNextOpt(pc)) != -1) {
6502 switch (opt) {
6503 default:
6504 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
6505 poptBadOption(pc, 0), poptStrerror(opt)));
6506 exit(1);
6510 /* setup the remaining options for the main program to use */
6511 extra_argv = poptGetArgs(pc);
6512 if (extra_argv) {
6513 extra_argv++;
6514 while (extra_argv[extra_argc]) extra_argc++;
6517 if (extra_argc < 1) {
6518 usage();
6521 if (options.maxruntime == 0) {
6522 const char *ctdb_timeout;
6523 ctdb_timeout = getenv("CTDB_TIMEOUT");
6524 if (ctdb_timeout != NULL) {
6525 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
6526 } else {
6527 /* default timeout is 120 seconds */
6528 options.maxruntime = 120;
6532 signal(SIGALRM, ctdb_alarm);
6533 alarm(options.maxruntime);
6535 control = extra_argv[0];
6537 /* Default value for CTDB_BASE - don't override */
6538 setenv("CTDB_BASE", CTDB_ETCDIR, 0);
6540 ev = event_context_init(NULL);
6541 if (!ev) {
6542 DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
6543 exit(1);
6546 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6547 if (strcmp(control, ctdb_commands[i].name) == 0) {
6548 break;
6552 if (i == ARRAY_SIZE(ctdb_commands)) {
6553 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
6554 exit(1);
6557 if (ctdb_commands[i].without_daemon == true) {
6558 if (nodestring != NULL) {
6559 DEBUG(DEBUG_ERR, ("Can't specify node(s) with \"ctdb %s\"\n", control));
6560 exit(1);
6562 return ctdb_commands[i].fn(NULL, extra_argc-1, extra_argv+1);
6565 /* initialise ctdb */
6566 ctdb = ctdb_cmdline_client(ev, TIMELIMIT());
6568 if (ctdb == NULL) {
6569 uint32_t pnn;
6570 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
6572 pnn = find_node_xpnn();
6573 if (pnn == -1) {
6574 DEBUG(DEBUG_ERR,
6575 ("Is this node part of a CTDB cluster?\n"));
6577 exit(1);
6580 /* setup the node number(s) to contact */
6581 if (!parse_nodestring(ctdb, ctdb, nodestring, CTDB_CURRENT_NODE, false,
6582 &options.nodes, &options.pnn)) {
6583 usage();
6586 if (options.pnn == CTDB_CURRENT_NODE) {
6587 options.pnn = options.nodes[0];
6590 if (ctdb_commands[i].auto_all &&
6591 ((options.pnn == CTDB_BROADCAST_ALL) ||
6592 (options.pnn == CTDB_MULTICAST))) {
6593 int j;
6595 ret = 0;
6596 for (j = 0; j < talloc_array_length(options.nodes); j++) {
6597 options.pnn = options.nodes[j];
6598 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6600 } else {
6601 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6604 talloc_free(ctdb);
6605 talloc_free(ev);
6606 (void)poptFreeContext(pc);
6608 return ret;