tools/ctdb: Factor, simplify and improve robustness of ipreallocate code
[Samba.git] / ctdb / tools / ctdb.c
blobeaa784cbffaf49316f511afb3cee7f2df8941249
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.h"
30 #include "../include/ctdb_client.h"
31 #include "../include/ctdb_private.h"
32 #include "../common/rb_tree.h"
33 #include "db_wrap.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 struct ctdb_connection *ctdb_connection;
41 static void usage(void);
43 static struct {
44 int timelimit;
45 uint32_t pnn;
46 uint32_t *nodes;
47 int machinereadable;
48 int verbose;
49 int maxruntime;
50 int printemptyrecords;
51 int printdatasize;
52 int printlmaster;
53 int printhash;
54 int printrecordflags;
55 } options;
57 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
58 #define LONGTIMELIMIT() timeval_current_ofs(options.timelimit*10, 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 /* Pretty print the flags to a static buffer in human-readable format.
73 * This never returns NULL!
75 static const char *pretty_print_flags(uint32_t flags)
77 int j;
78 static const struct {
79 uint32_t flag;
80 const char *name;
81 } flag_names[] = {
82 { NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
83 { NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
84 { NODE_FLAGS_BANNED, "BANNED" },
85 { NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
86 { NODE_FLAGS_DELETED, "DELETED" },
87 { NODE_FLAGS_STOPPED, "STOPPED" },
88 { NODE_FLAGS_INACTIVE, "INACTIVE" },
90 static char flags_str[512]; /* Big enough to contain all flag names */
92 flags_str[0] = '\0';
93 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
94 if (flags & flag_names[j].flag) {
95 if (flags_str[0] == '\0') {
96 (void) strcpy(flags_str, flag_names[j].name);
97 } else {
98 (void) strcat(flags_str, "|");
99 (void) strcat(flags_str, flag_names[j].name);
103 if (flags_str[0] == '\0') {
104 (void) strcpy(flags_str, "OK");
107 return flags_str;
110 static int h2i(char h)
112 if (h >= 'a' && h <= 'f') return h - 'a' + 10;
113 if (h >= 'A' && h <= 'F') return h - 'f' + 10;
114 return h - '0';
117 static TDB_DATA hextodata(TALLOC_CTX *mem_ctx, const char *str)
119 int i, len;
120 TDB_DATA key = {NULL, 0};
122 len = strlen(str);
123 if (len & 0x01) {
124 DEBUG(DEBUG_ERR,("Key specified with odd number of hexadecimal digits\n"));
125 return key;
128 key.dsize = len>>1;
129 key.dptr = talloc_size(mem_ctx, key.dsize);
131 for (i=0; i < len/2; i++) {
132 key.dptr[i] = h2i(str[i*2]) << 4 | h2i(str[i*2+1]);
134 return key;
137 /* Parse a nodestring. Parameter dd_ok controls what happens to nodes
138 * that are disconnected or deleted. If dd_ok is true those nodes are
139 * included in the output list of nodes. If dd_ok is false, those
140 * nodes are filtered from the "all" case and cause an error if
141 * explicitly specified.
143 static bool parse_nodestring(struct ctdb_context *ctdb,
144 const char * nodestring,
145 uint32_t current_pnn,
146 bool dd_ok,
147 uint32_t **nodes,
148 uint32_t *pnn_mode)
150 int n;
151 uint32_t i;
152 struct ctdb_node_map *nodemap;
154 *nodes = NULL;
156 if (!ctdb_getnodemap(ctdb_connection, CTDB_CURRENT_NODE, &nodemap)) {
157 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
158 exit(10);
161 if (nodestring != NULL) {
162 *nodes = talloc_array(ctdb, uint32_t, 0);
163 CTDB_NOMEM_ABORT(*nodes);
165 n = 0;
167 if (strcmp(nodestring, "all") == 0) {
168 *pnn_mode = CTDB_BROADCAST_ALL;
170 /* all */
171 for (i = 0; i < nodemap->num; i++) {
172 if ((nodemap->nodes[i].flags &
173 (NODE_FLAGS_DISCONNECTED |
174 NODE_FLAGS_DELETED)) && !dd_ok) {
175 continue;
177 *nodes = talloc_realloc(ctdb, *nodes,
178 uint32_t, n+1);
179 CTDB_NOMEM_ABORT(*nodes);
180 (*nodes)[n] = i;
181 n++;
183 } else {
184 /* x{,y...} */
185 char *ns, *tok;
187 ns = talloc_strdup(ctdb, nodestring);
188 tok = strtok(ns, ",");
189 while (tok != NULL) {
190 uint32_t pnn;
191 i = (uint32_t)strtoul(tok, NULL, 0);
192 if (i >= nodemap->num) {
193 DEBUG(DEBUG_ERR, ("Node %u does not exist\n", i));
194 exit(ERR_NONODE);
196 if ((nodemap->nodes[i].flags &
197 (NODE_FLAGS_DISCONNECTED |
198 NODE_FLAGS_DELETED)) && !dd_ok) {
199 DEBUG(DEBUG_ERR, ("Node %u has status %s\n", i, pretty_print_flags(nodemap->nodes[i].flags)));
200 exit(ERR_DISNODE);
202 if (!ctdb_getpnn(ctdb_connection, i, &pnn)) {
203 DEBUG(DEBUG_ERR, ("Can not access node %u. Node is not operational.\n", i));
204 exit(10);
207 *nodes = talloc_realloc(ctdb, *nodes,
208 uint32_t, n+1);
209 CTDB_NOMEM_ABORT(*nodes);
211 (*nodes)[n] = i;
212 n++;
214 tok = strtok(NULL, ",");
216 talloc_free(ns);
218 if (n == 1) {
219 *pnn_mode = (*nodes)[0];
220 } else {
221 *pnn_mode = CTDB_MULTICAST;
224 } else {
225 /* default - no nodes specified */
226 *nodes = talloc_array(ctdb, uint32_t, 1);
227 CTDB_NOMEM_ABORT(*nodes);
228 *pnn_mode = CTDB_CURRENT_NODE;
230 if (!ctdb_getpnn(ctdb_connection, current_pnn,
231 &((*nodes)[0]))) {
232 return false;
236 ctdb_free_nodemap(nodemap);
238 return true;
242 check if a database exists
244 static bool db_exists(struct ctdb_context *ctdb, const char *dbarg, uint32_t *dbid, uint8_t *flags)
246 int i, ret;
247 struct ctdb_dbid_map *dbmap=NULL;
248 bool dbid_given = false, found = false;
249 uint32_t id;
250 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
252 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
253 if (ret != 0) {
254 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
255 goto fail;
258 if (strncmp(dbarg, "0x", 2) == 0) {
259 id = strtoul(dbarg, NULL, 0);
260 dbid_given = true;
263 for(i=0; i<dbmap->num; i++) {
264 if (dbid_given) {
265 if (id == dbmap->dbs[i].dbid) {
266 found = true;
267 break;
269 } else {
270 const char *name;
271 ret = ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
272 if (ret != 0) {
273 DEBUG(DEBUG_ERR, ("Unable to get dbname from dbid %u\n", dbmap->dbs[i].dbid));
274 goto fail;
277 if (strcmp(name, dbarg) == 0) {
278 id = dbmap->dbs[i].dbid;
279 found = true;
280 break;
285 if (found) {
286 if (dbid) *dbid = id;
287 if (flags) *flags = dbmap->dbs[i].flags;
288 } else {
289 DEBUG(DEBUG_ERR,("No database matching '%s' found\n", dbarg));
292 fail:
293 talloc_free(tmp_ctx);
294 return found;
298 see if a process exists
300 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
302 uint32_t pnn, pid;
303 int ret;
304 if (argc < 1) {
305 usage();
308 if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
309 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
310 return -1;
313 ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
314 if (ret == 0) {
315 printf("%u:%u exists\n", pnn, pid);
316 } else {
317 printf("%u:%u does not exist\n", pnn, pid);
319 return ret;
323 display statistics structure
325 static void show_statistics(struct ctdb_statistics *s, int show_header)
327 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
328 int i;
329 const char *prefix=NULL;
330 int preflen=0;
331 int tmp, days, hours, minutes, seconds;
332 const struct {
333 const char *name;
334 uint32_t offset;
335 } fields[] = {
336 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
337 STATISTICS_FIELD(num_clients),
338 STATISTICS_FIELD(frozen),
339 STATISTICS_FIELD(recovering),
340 STATISTICS_FIELD(num_recoveries),
341 STATISTICS_FIELD(client_packets_sent),
342 STATISTICS_FIELD(client_packets_recv),
343 STATISTICS_FIELD(node_packets_sent),
344 STATISTICS_FIELD(node_packets_recv),
345 STATISTICS_FIELD(keepalive_packets_sent),
346 STATISTICS_FIELD(keepalive_packets_recv),
347 STATISTICS_FIELD(node.req_call),
348 STATISTICS_FIELD(node.reply_call),
349 STATISTICS_FIELD(node.req_dmaster),
350 STATISTICS_FIELD(node.reply_dmaster),
351 STATISTICS_FIELD(node.reply_error),
352 STATISTICS_FIELD(node.req_message),
353 STATISTICS_FIELD(node.req_control),
354 STATISTICS_FIELD(node.reply_control),
355 STATISTICS_FIELD(client.req_call),
356 STATISTICS_FIELD(client.req_message),
357 STATISTICS_FIELD(client.req_control),
358 STATISTICS_FIELD(timeouts.call),
359 STATISTICS_FIELD(timeouts.control),
360 STATISTICS_FIELD(timeouts.traverse),
361 STATISTICS_FIELD(locks.num_calls),
362 STATISTICS_FIELD(locks.num_current),
363 STATISTICS_FIELD(locks.num_pending),
364 STATISTICS_FIELD(locks.num_failed),
365 STATISTICS_FIELD(total_calls),
366 STATISTICS_FIELD(pending_calls),
367 STATISTICS_FIELD(childwrite_calls),
368 STATISTICS_FIELD(pending_childwrite_calls),
369 STATISTICS_FIELD(memory_used),
370 STATISTICS_FIELD(max_hop_count),
371 STATISTICS_FIELD(total_ro_delegations),
372 STATISTICS_FIELD(total_ro_revokes),
375 tmp = s->statistics_current_time.tv_sec - s->statistics_start_time.tv_sec;
376 seconds = tmp%60;
377 tmp /= 60;
378 minutes = tmp%60;
379 tmp /= 60;
380 hours = tmp%24;
381 tmp /= 24;
382 days = tmp;
384 if (options.machinereadable){
385 if (show_header) {
386 printf("CTDB version:");
387 printf("Current time of statistics:");
388 printf("Statistics collected since:");
389 for (i=0;i<ARRAY_SIZE(fields);i++) {
390 printf("%s:", fields[i].name);
392 printf("num_reclock_ctdbd_latency:");
393 printf("min_reclock_ctdbd_latency:");
394 printf("avg_reclock_ctdbd_latency:");
395 printf("max_reclock_ctdbd_latency:");
397 printf("num_reclock_recd_latency:");
398 printf("min_reclock_recd_latency:");
399 printf("avg_reclock_recd_latency:");
400 printf("max_reclock_recd_latency:");
402 printf("num_call_latency:");
403 printf("min_call_latency:");
404 printf("avg_call_latency:");
405 printf("max_call_latency:");
407 printf("num_lockwait_latency:");
408 printf("min_lockwait_latency:");
409 printf("avg_lockwait_latency:");
410 printf("max_lockwait_latency:");
412 printf("num_childwrite_latency:");
413 printf("min_childwrite_latency:");
414 printf("avg_childwrite_latency:");
415 printf("max_childwrite_latency:");
416 printf("\n");
418 printf("%d:", CTDB_VERSION);
419 printf("%d:", (int)s->statistics_current_time.tv_sec);
420 printf("%d:", (int)s->statistics_start_time.tv_sec);
421 for (i=0;i<ARRAY_SIZE(fields);i++) {
422 printf("%d:", *(uint32_t *)(fields[i].offset+(uint8_t *)s));
424 printf("%d:", s->reclock.ctdbd.num);
425 printf("%.6f:", s->reclock.ctdbd.min);
426 printf("%.6f:", s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0);
427 printf("%.6f:", s->reclock.ctdbd.max);
429 printf("%d:", s->reclock.recd.num);
430 printf("%.6f:", s->reclock.recd.min);
431 printf("%.6f:", s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0);
432 printf("%.6f:", s->reclock.recd.max);
434 printf("%d:", s->call_latency.num);
435 printf("%.6f:", s->call_latency.min);
436 printf("%.6f:", s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0);
437 printf("%.6f:", s->call_latency.max);
439 printf("%d:", s->childwrite_latency.num);
440 printf("%.6f:", s->childwrite_latency.min);
441 printf("%.6f:", s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0);
442 printf("%.6f:", s->childwrite_latency.max);
443 printf("\n");
444 } else {
445 printf("CTDB version %u\n", CTDB_VERSION);
446 printf("Current time of statistics : %s", ctime(&s->statistics_current_time.tv_sec));
447 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&s->statistics_start_time.tv_sec));
449 for (i=0;i<ARRAY_SIZE(fields);i++) {
450 if (strchr(fields[i].name, '.')) {
451 preflen = strcspn(fields[i].name, ".")+1;
452 if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
453 prefix = fields[i].name;
454 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
456 } else {
457 preflen = 0;
459 printf(" %*s%-22s%*s%10u\n",
460 preflen?4:0, "",
461 fields[i].name+preflen,
462 preflen?0:4, "",
463 *(uint32_t *)(fields[i].offset+(uint8_t *)s));
465 printf(" hop_count_buckets:");
466 for (i=0;i<MAX_COUNT_BUCKETS;i++) {
467 printf(" %d", s->hop_count_bucket[i]);
469 printf("\n");
470 printf(" lock_buckets:");
471 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
472 printf(" %d", s->locks.buckets[i]);
474 printf("\n");
475 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);
477 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);
479 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);
481 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);
482 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);
485 talloc_free(tmp_ctx);
489 display remote ctdb statistics combined from all nodes
491 static int control_statistics_all(struct ctdb_context *ctdb)
493 int ret, i;
494 struct ctdb_statistics statistics;
495 uint32_t *nodes;
496 uint32_t num_nodes;
498 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
499 CTDB_NO_MEMORY(ctdb, nodes);
501 ZERO_STRUCT(statistics);
503 for (i=0;i<num_nodes;i++) {
504 struct ctdb_statistics s1;
505 int j;
506 uint32_t *v1 = (uint32_t *)&s1;
507 uint32_t *v2 = (uint32_t *)&statistics;
508 uint32_t num_ints =
509 offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
510 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
511 if (ret != 0) {
512 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
513 return ret;
515 for (j=0;j<num_ints;j++) {
516 v2[j] += v1[j];
518 statistics.max_hop_count =
519 MAX(statistics.max_hop_count, s1.max_hop_count);
520 statistics.call_latency.max =
521 MAX(statistics.call_latency.max, s1.call_latency.max);
523 talloc_free(nodes);
524 printf("Gathered statistics for %u nodes\n", num_nodes);
525 show_statistics(&statistics, 1);
526 return 0;
530 display remote ctdb statistics
532 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
534 int ret;
535 struct ctdb_statistics statistics;
537 if (options.pnn == CTDB_BROADCAST_ALL) {
538 return control_statistics_all(ctdb);
541 ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
542 if (ret != 0) {
543 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
544 return ret;
546 show_statistics(&statistics, 1);
547 return 0;
552 reset remote ctdb statistics
554 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
556 int ret;
558 ret = ctdb_statistics_reset(ctdb, options.pnn);
559 if (ret != 0) {
560 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
561 return ret;
563 return 0;
568 display remote ctdb rolling statistics
570 static int control_stats(struct ctdb_context *ctdb, int argc, const char **argv)
572 int ret;
573 struct ctdb_statistics_wire *stats;
574 int i, num_records = -1;
576 if (argc ==1) {
577 num_records = atoi(argv[0]) - 1;
580 ret = ctdb_ctrl_getstathistory(ctdb, TIMELIMIT(), options.pnn, ctdb, &stats);
581 if (ret != 0) {
582 DEBUG(DEBUG_ERR, ("Unable to get rolling statistics from node %u\n", options.pnn));
583 return ret;
585 for (i=0;i<stats->num;i++) {
586 if (stats->stats[i].statistics_start_time.tv_sec == 0) {
587 continue;
589 show_statistics(&stats->stats[i], i==0);
590 if (i == num_records) {
591 break;
594 return 0;
599 display remote ctdb db statistics
601 static int control_dbstatistics(struct ctdb_context *ctdb, int argc, const char **argv)
603 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
604 struct ctdb_db_statistics *dbstat;
605 int i;
606 uint32_t db_id;
607 int num_hot_keys;
609 if (argc < 1) {
610 usage();
613 if (!db_exists(ctdb, argv[0], &db_id, NULL)) {
614 return -1;
617 if (!ctdb_getdbstat(ctdb_connection, options.pnn, db_id, &dbstat)) {
618 DEBUG(DEBUG_ERR,("Failed to read db statistics from node\n"));
619 talloc_free(tmp_ctx);
620 return -1;
623 printf("DB Statistics: %s\n", argv[0]);
624 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_delegations", 4, "",
625 dbstat->db_ro_delegations);
626 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_revokes", 4, "",
627 dbstat->db_ro_delegations);
628 printf(" %s\n", "locks");
629 printf(" %*s%-22s%*s%10u\n", 4, "", "total", 0, "",
630 dbstat->locks.num_calls);
631 printf(" %*s%-22s%*s%10u\n", 4, "", "failed", 0, "",
632 dbstat->locks.num_failed);
633 printf(" %*s%-22s%*s%10u\n", 4, "", "current", 0, "",
634 dbstat->locks.num_current);
635 printf(" %*s%-22s%*s%10u\n", 4, "", "pending", 0, "",
636 dbstat->locks.num_pending);
637 printf(" %s", "hop_count_buckets:");
638 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
639 printf(" %d", dbstat->hop_count_bucket[i]);
641 printf("\n");
642 printf(" %s", "lock_buckets:");
643 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
644 printf(" %d", dbstat->locks.buckets[i]);
646 printf("\n");
647 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
648 "locks_latency MIN/AVG/MAX",
649 dbstat->locks.latency.min,
650 (dbstat->locks.latency.num ?
651 dbstat->locks.latency.total /dbstat->locks.latency.num :
652 0.0),
653 dbstat->locks.latency.max,
654 dbstat->locks.latency.num);
655 num_hot_keys = 0;
656 for (i=0; i<dbstat->num_hot_keys; i++) {
657 if (dbstat->hot_keys[i].count > 0) {
658 num_hot_keys++;
661 dbstat->num_hot_keys = num_hot_keys;
663 printf(" Num Hot Keys: %d\n", dbstat->num_hot_keys);
664 for (i = 0; i < dbstat->num_hot_keys; i++) {
665 int j;
666 printf(" Count:%d Key:", dbstat->hot_keys[i].count);
667 for (j = 0; j < dbstat->hot_keys[i].key.dsize; j++) {
668 printf("%02x", dbstat->hot_keys[i].key.dptr[j]&0xff);
670 printf("\n");
673 ctdb_free_dbstat(dbstat);
674 return 0;
678 display uptime of remote node
680 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
682 int ret;
683 struct ctdb_uptime *uptime = NULL;
684 int tmp, days, hours, minutes, seconds;
686 ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
687 if (ret != 0) {
688 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
689 return ret;
692 if (options.machinereadable){
693 printf(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
694 printf(":%u:%u:%u:%lf\n",
695 (unsigned int)uptime->current_time.tv_sec,
696 (unsigned int)uptime->ctdbd_start_time.tv_sec,
697 (unsigned int)uptime->last_recovery_finished.tv_sec,
698 timeval_delta(&uptime->last_recovery_finished,
699 &uptime->last_recovery_started)
701 return 0;
704 printf("Current time of node : %s", ctime(&uptime->current_time.tv_sec));
706 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
707 seconds = tmp%60;
708 tmp /= 60;
709 minutes = tmp%60;
710 tmp /= 60;
711 hours = tmp%24;
712 tmp /= 24;
713 days = tmp;
714 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
716 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
717 seconds = tmp%60;
718 tmp /= 60;
719 minutes = tmp%60;
720 tmp /= 60;
721 hours = tmp%24;
722 tmp /= 24;
723 days = tmp;
724 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
726 printf("Duration of last recovery/failover: %lf seconds\n",
727 timeval_delta(&uptime->last_recovery_finished,
728 &uptime->last_recovery_started));
730 return 0;
734 show the PNN of the current node
736 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
738 uint32_t mypnn;
739 bool ret;
741 ret = ctdb_getpnn(ctdb_connection, options.pnn, &mypnn);
742 if (!ret) {
743 DEBUG(DEBUG_ERR, ("Unable to get pnn from node."));
744 return -1;
747 printf("PNN:%d\n", mypnn);
748 return 0;
752 struct pnn_node {
753 struct pnn_node *next;
754 const char *addr;
755 int pnn;
758 static struct pnn_node *read_nodes_file(TALLOC_CTX *mem_ctx)
760 const char *nodes_list;
761 int nlines;
762 char **lines;
763 int i, pnn;
764 struct pnn_node *pnn_nodes = NULL;
765 struct pnn_node *pnn_node;
766 struct pnn_node *tmp_node;
768 /* read the nodes file */
769 nodes_list = getenv("CTDB_NODES");
770 if (nodes_list == NULL) {
771 nodes_list = "/etc/ctdb/nodes";
773 lines = file_lines_load(nodes_list, &nlines, mem_ctx);
774 if (lines == NULL) {
775 return NULL;
777 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
778 nlines--;
780 for (i=0, pnn=0; i<nlines; i++) {
781 char *node;
783 node = lines[i];
784 /* strip leading spaces */
785 while((*node == ' ') || (*node == '\t')) {
786 node++;
788 if (*node == '#') {
789 pnn++;
790 continue;
792 if (strcmp(node, "") == 0) {
793 continue;
795 pnn_node = talloc(mem_ctx, struct pnn_node);
796 pnn_node->pnn = pnn++;
797 pnn_node->addr = talloc_strdup(pnn_node, node);
798 pnn_node->next = pnn_nodes;
799 pnn_nodes = pnn_node;
802 /* swap them around so we return them in incrementing order */
803 pnn_node = pnn_nodes;
804 pnn_nodes = NULL;
805 while (pnn_node) {
806 tmp_node = pnn_node;
807 pnn_node = pnn_node->next;
809 tmp_node->next = pnn_nodes;
810 pnn_nodes = tmp_node;
813 return pnn_nodes;
817 show the PNN of the current node
818 discover the pnn by loading the nodes file and try to bind to all
819 addresses one at a time until the ip address is found.
821 static int control_xpnn(struct ctdb_context *ctdb, int argc, const char **argv)
823 TALLOC_CTX *mem_ctx = talloc_new(NULL);
824 struct pnn_node *pnn_nodes;
825 struct pnn_node *pnn_node;
827 pnn_nodes = read_nodes_file(mem_ctx);
828 if (pnn_nodes == NULL) {
829 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
830 talloc_free(mem_ctx);
831 return -1;
834 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
835 ctdb_sock_addr addr;
837 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
838 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
839 talloc_free(mem_ctx);
840 return -1;
843 if (ctdb_sys_have_ip(&addr)) {
844 printf("PNN:%d\n", pnn_node->pnn);
845 talloc_free(mem_ctx);
846 return 0;
850 printf("Failed to detect which PNN this node is\n");
851 talloc_free(mem_ctx);
852 return -1;
855 /* Helpers for ctdb status
857 static bool is_partially_online(struct ctdb_node_and_flags *node)
859 int j;
860 bool ret = false;
862 if (node->flags == 0) {
863 struct ctdb_ifaces_list *ifaces;
865 if (ctdb_getifaces(ctdb_connection, node->pnn, &ifaces)) {
866 for (j=0; j < ifaces->num; j++) {
867 if (ifaces->ifaces[j].link_state != 0) {
868 continue;
870 ret = true;
871 break;
873 ctdb_free_ifaces(ifaces);
877 return ret;
880 static void control_status_header_machine(void)
882 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
883 ":Inactive:PartiallyOnline:ThisNode:\n");
886 static int control_status_1_machine(int mypnn, struct ctdb_node_and_flags *node)
888 printf(":%d:%s:%d:%d:%d:%d:%d:%d:%d:%c:\n", node->pnn,
889 ctdb_addr_to_str(&node->addr),
890 !!(node->flags&NODE_FLAGS_DISCONNECTED),
891 !!(node->flags&NODE_FLAGS_BANNED),
892 !!(node->flags&NODE_FLAGS_PERMANENTLY_DISABLED),
893 !!(node->flags&NODE_FLAGS_UNHEALTHY),
894 !!(node->flags&NODE_FLAGS_STOPPED),
895 !!(node->flags&NODE_FLAGS_INACTIVE),
896 is_partially_online(node) ? 1 : 0,
897 (node->pnn == mypnn)?'Y':'N');
899 return node->flags;
902 static int control_status_1_human(int mypnn, struct ctdb_node_and_flags *node)
904 printf("pnn:%d %-16s %s%s\n", node->pnn,
905 ctdb_addr_to_str(&node->addr),
906 is_partially_online(node) ? "PARTIALLYONLINE" : pretty_print_flags(node->flags),
907 node->pnn == mypnn?" (THIS NODE)":"");
909 return node->flags;
913 display remote ctdb status
915 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
917 int i;
918 struct ctdb_vnn_map *vnnmap=NULL;
919 struct ctdb_node_map *nodemap=NULL;
920 uint32_t recmode, recmaster, mypnn;
921 int num_deleted_nodes = 0;
923 if (!ctdb_getpnn(ctdb_connection, options.pnn, &mypnn)) {
924 return -1;
927 if (!ctdb_getnodemap(ctdb_connection, options.pnn, &nodemap)) {
928 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
929 return -1;
932 if (options.machinereadable) {
933 control_status_header_machine();
934 for (i=0;i<nodemap->num;i++) {
935 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
936 continue;
938 (void) control_status_1_machine(mypnn,
939 &nodemap->nodes[i]);
941 return 0;
944 for (i=0; i<nodemap->num; i++) {
945 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
946 num_deleted_nodes++;
949 if (num_deleted_nodes == 0) {
950 printf("Number of nodes:%d\n", nodemap->num);
951 } else {
952 printf("Number of nodes:%d (including %d deleted nodes)\n",
953 nodemap->num, num_deleted_nodes);
955 for(i=0;i<nodemap->num;i++){
956 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
957 continue;
959 (void) control_status_1_human(mypnn, &nodemap->nodes[i]);
962 if (!ctdb_getvnnmap(ctdb_connection, options.pnn, &vnnmap)) {
963 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
964 return -1;
966 if (vnnmap->generation == INVALID_GENERATION) {
967 printf("Generation:INVALID\n");
968 } else {
969 printf("Generation:%d\n",vnnmap->generation);
971 printf("Size:%d\n",vnnmap->size);
972 for(i=0;i<vnnmap->size;i++){
973 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
975 ctdb_free_vnnmap(vnnmap);
977 if (!ctdb_getrecmode(ctdb_connection, options.pnn, &recmode)) {
978 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
979 return -1;
981 printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
983 if (!ctdb_getrecmaster(ctdb_connection, options.pnn, &recmaster)) {
984 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
985 return -1;
987 printf("Recovery master:%d\n",recmaster);
989 return 0;
992 static int control_nodestatus(struct ctdb_context *ctdb, int argc, const char **argv)
994 int i, ret;
995 struct ctdb_node_map *nodemap=NULL;
996 uint32_t * nodes;
997 uint32_t pnn_mode, mypnn;
999 if (argc > 1) {
1000 usage();
1003 if (!parse_nodestring(ctdb, argc == 1 ? argv[0] : NULL,
1004 options.pnn, true, &nodes, &pnn_mode)) {
1005 return -1;
1008 if (options.machinereadable) {
1009 control_status_header_machine();
1010 } else if (pnn_mode == CTDB_BROADCAST_ALL) {
1011 printf("Number of nodes:%d\n", (int) talloc_array_length(nodes));
1014 if (!ctdb_getpnn(ctdb_connection, options.pnn, &mypnn)) {
1015 DEBUG(DEBUG_ERR, ("Unable to get PNN from local node\n"));
1016 return -1;
1019 if (!ctdb_getnodemap(ctdb_connection, options.pnn, &nodemap)) {
1020 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1021 return -1;
1024 ret = 0;
1026 for (i = 0; i < talloc_array_length(nodes); i++) {
1027 if (options.machinereadable) {
1028 ret |= control_status_1_machine(mypnn,
1029 &nodemap->nodes[nodes[i]]);
1030 } else {
1031 ret |= control_status_1_human(mypnn,
1032 &nodemap->nodes[nodes[i]]);
1035 return ret;
1038 struct natgw_node {
1039 struct natgw_node *next;
1040 const char *addr;
1043 static int find_natgw(struct ctdb_context *ctdb,
1044 struct ctdb_node_map *nodemap, uint32_t flags,
1045 uint32_t *pnn, const char **ip)
1047 int i;
1048 uint32_t capabilities;
1050 for (i=0;i<nodemap->num;i++) {
1051 if (!(nodemap->nodes[i].flags & flags)) {
1052 if (!ctdb_getcapabilities(ctdb_connection, nodemap->nodes[i].pnn, &capabilities)) {
1053 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", nodemap->nodes[i].pnn));
1054 return -1;
1056 if (!(capabilities&CTDB_CAP_NATGW)) {
1057 continue;
1059 *pnn = nodemap->nodes[i].pnn;
1060 *ip = ctdb_addr_to_str(&nodemap->nodes[i].addr);
1061 return 0;
1065 return 2; /* matches ENOENT */
1069 display the list of nodes belonging to this natgw configuration
1071 static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **argv)
1073 int i, ret;
1074 const char *natgw_list;
1075 int nlines;
1076 char **lines;
1077 struct natgw_node *natgw_nodes = NULL;
1078 struct natgw_node *natgw_node;
1079 struct ctdb_node_map *nodemap=NULL;
1080 uint32_t mypnn, pnn;
1081 const char *ip;
1083 /* When we have some nodes that could be the NATGW, make a
1084 * series of attempts to find the first node that doesn't have
1085 * certain status flags set.
1087 uint32_t exclude_flags[] = {
1088 /* Look for a nice healthy node */
1089 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_UNHEALTHY,
1090 /* If not found, an UNHEALTHY/BANNED node will do */
1091 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED,
1092 /* If not found, a STOPPED node will do */
1093 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED,
1097 /* read the natgw nodes file into a linked list */
1098 natgw_list = getenv("CTDB_NATGW_NODES");
1099 if (natgw_list == NULL) {
1100 natgw_list = "/etc/ctdb/natgw_nodes";
1102 lines = file_lines_load(natgw_list, &nlines, ctdb);
1103 if (lines == NULL) {
1104 ctdb_set_error(ctdb, "Failed to load natgw node list '%s'\n", natgw_list);
1105 return -1;
1107 for (i=0;i<nlines;i++) {
1108 char *node;
1110 node = lines[i];
1111 /* strip leading spaces */
1112 while((*node == ' ') || (*node == '\t')) {
1113 node++;
1115 if (*node == '#') {
1116 continue;
1118 if (strcmp(node, "") == 0) {
1119 continue;
1121 natgw_node = talloc(ctdb, struct natgw_node);
1122 natgw_node->addr = talloc_strdup(natgw_node, node);
1123 CTDB_NO_MEMORY(ctdb, natgw_node->addr);
1124 natgw_node->next = natgw_nodes;
1125 natgw_nodes = natgw_node;
1128 if (!ctdb_getnodemap(ctdb_connection, CTDB_CURRENT_NODE, &nodemap)) {
1129 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node.\n"));
1130 return -1;
1133 /* Trim the nodemap so it only includes connected nodes in the
1134 * current natgw group.
1136 i=0;
1137 while(i<nodemap->num) {
1138 for(natgw_node=natgw_nodes;natgw_node;natgw_node=natgw_node->next) {
1139 if (!strcmp(natgw_node->addr, ctdb_addr_to_str(&nodemap->nodes[i].addr))) {
1140 break;
1144 /* this node was not in the natgw so we just remove it from
1145 * the list
1147 if ((natgw_node == NULL)
1148 || (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) ) {
1149 int j;
1151 for (j=i+1; j<nodemap->num; j++) {
1152 nodemap->nodes[j-1] = nodemap->nodes[j];
1154 nodemap->num--;
1155 continue;
1158 i++;
1161 ret = 2; /* matches ENOENT */
1162 pnn = -1;
1163 ip = "0.0.0.0";
1164 for (i = 0; exclude_flags[i] != 0; i++) {
1165 ret = find_natgw(ctdb, nodemap,
1166 exclude_flags[i],
1167 &pnn, &ip);
1168 if (ret == -1) {
1169 goto done;
1171 if (ret == 0) {
1172 break;
1176 if (options.machinereadable) {
1177 printf(":Node:IP:\n");
1178 printf(":%d:%s:\n", pnn, ip);
1179 } else {
1180 printf("%d %s\n", pnn, ip);
1183 /* print the pruned list of nodes belonging to this natgw list */
1184 if (!ctdb_getpnn(ctdb_connection, options.pnn, &mypnn)) {
1185 DEBUG(DEBUG_NOTICE, ("Unable to get PNN from node %u\n", options.pnn));
1186 /* This is actually harmless and will only result in
1187 * the "this node" indication being missing
1189 mypnn = -1;
1191 if (options.machinereadable) {
1192 control_status_header_machine();
1193 } else {
1194 printf("Number of nodes:%d\n", nodemap->num);
1196 for(i=0;i<nodemap->num;i++){
1197 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1198 continue;
1200 if (options.machinereadable) {
1201 control_status_1_machine(mypnn, &(nodemap->nodes[i]));
1202 } else {
1203 control_status_1_human(mypnn, &(nodemap->nodes[i]));
1207 done:
1208 ctdb_free_nodemap(nodemap);
1209 return ret;
1213 display the status of the scripts for monitoring (or other events)
1215 static int control_one_scriptstatus(struct ctdb_context *ctdb,
1216 enum ctdb_eventscript_call type)
1218 struct ctdb_scripts_wire *script_status;
1219 int ret, i;
1221 ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
1222 if (ret != 0) {
1223 DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
1224 return ret;
1227 if (script_status == NULL) {
1228 if (!options.machinereadable) {
1229 printf("%s cycle never run\n",
1230 ctdb_eventscript_call_names[type]);
1232 return 0;
1235 if (!options.machinereadable) {
1236 printf("%d scripts were executed last %s cycle\n",
1237 script_status->num_scripts,
1238 ctdb_eventscript_call_names[type]);
1240 for (i=0; i<script_status->num_scripts; i++) {
1241 const char *status = NULL;
1243 switch (script_status->scripts[i].status) {
1244 case -ETIME:
1245 status = "TIMEDOUT";
1246 break;
1247 case -ENOEXEC:
1248 status = "DISABLED";
1249 break;
1250 case 0:
1251 status = "OK";
1252 break;
1253 default:
1254 if (script_status->scripts[i].status > 0)
1255 status = "ERROR";
1256 break;
1258 if (options.machinereadable) {
1259 printf(":%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
1260 ctdb_eventscript_call_names[type],
1261 script_status->scripts[i].name,
1262 script_status->scripts[i].status,
1263 status,
1264 (long)script_status->scripts[i].start.tv_sec,
1265 (long)script_status->scripts[i].start.tv_usec,
1266 (long)script_status->scripts[i].finished.tv_sec,
1267 (long)script_status->scripts[i].finished.tv_usec,
1268 script_status->scripts[i].output);
1269 continue;
1271 if (status)
1272 printf("%-20s Status:%s ",
1273 script_status->scripts[i].name, status);
1274 else
1275 /* Some other error, eg from stat. */
1276 printf("%-20s Status:CANNOT RUN (%s)",
1277 script_status->scripts[i].name,
1278 strerror(-script_status->scripts[i].status));
1280 if (script_status->scripts[i].status >= 0) {
1281 printf("Duration:%.3lf ",
1282 timeval_delta(&script_status->scripts[i].finished,
1283 &script_status->scripts[i].start));
1285 if (script_status->scripts[i].status != -ENOEXEC) {
1286 printf("%s",
1287 ctime(&script_status->scripts[i].start.tv_sec));
1288 if (script_status->scripts[i].status != 0) {
1289 printf(" OUTPUT:%s\n",
1290 script_status->scripts[i].output);
1292 } else {
1293 printf("\n");
1296 return 0;
1300 static int control_scriptstatus(struct ctdb_context *ctdb,
1301 int argc, const char **argv)
1303 int ret;
1304 enum ctdb_eventscript_call type, min, max;
1305 const char *arg;
1307 if (argc > 1) {
1308 DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
1309 return -1;
1312 if (argc == 0)
1313 arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
1314 else
1315 arg = argv[0];
1317 for (type = 0; type < CTDB_EVENT_MAX; type++) {
1318 if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
1319 min = type;
1320 max = type+1;
1321 break;
1324 if (type == CTDB_EVENT_MAX) {
1325 if (strcmp(arg, "all") == 0) {
1326 min = 0;
1327 max = CTDB_EVENT_MAX;
1328 } else {
1329 DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
1330 return -1;
1334 if (options.machinereadable) {
1335 printf(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1338 for (type = min; type < max; type++) {
1339 ret = control_one_scriptstatus(ctdb, type);
1340 if (ret != 0) {
1341 return ret;
1345 return 0;
1349 enable an eventscript
1351 static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1353 int ret;
1355 if (argc < 1) {
1356 usage();
1359 ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1360 if (ret != 0) {
1361 DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
1362 return ret;
1365 return 0;
1369 disable an eventscript
1371 static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1373 int ret;
1375 if (argc < 1) {
1376 usage();
1379 ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1380 if (ret != 0) {
1381 DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
1382 return ret;
1385 return 0;
1389 display the pnn of the recovery master
1391 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1393 uint32_t recmaster;
1395 if (!ctdb_getrecmaster(ctdb_connection, options.pnn, &recmaster)) {
1396 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1397 return -1;
1399 printf("%d\n",recmaster);
1401 return 0;
1405 add a tickle to a public address
1407 static int control_add_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1409 struct ctdb_tcp_connection t;
1410 TDB_DATA data;
1411 int ret;
1413 if (argc < 2) {
1414 usage();
1417 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1418 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1419 return -1;
1421 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1422 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1423 return -1;
1426 data.dptr = (uint8_t *)&t;
1427 data.dsize = sizeof(t);
1429 /* tell all nodes about this tcp connection */
1430 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE,
1431 0, data, ctdb, NULL, NULL, NULL, NULL);
1432 if (ret != 0) {
1433 DEBUG(DEBUG_ERR,("Failed to add tickle\n"));
1434 return -1;
1437 return 0;
1442 delete a tickle from a node
1444 static int control_del_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1446 struct ctdb_tcp_connection t;
1447 TDB_DATA data;
1448 int ret;
1450 if (argc < 2) {
1451 usage();
1454 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1455 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1456 return -1;
1458 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1459 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1460 return -1;
1463 data.dptr = (uint8_t *)&t;
1464 data.dsize = sizeof(t);
1466 /* tell all nodes about this tcp connection */
1467 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_REMOVE,
1468 0, data, ctdb, NULL, NULL, NULL, NULL);
1469 if (ret != 0) {
1470 DEBUG(DEBUG_ERR,("Failed to remove tickle\n"));
1471 return -1;
1474 return 0;
1479 get a list of all tickles for this pnn
1481 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
1483 struct ctdb_control_tcp_tickle_list *list;
1484 ctdb_sock_addr addr;
1485 int i, ret;
1486 unsigned port = 0;
1488 if (argc < 1) {
1489 usage();
1492 if (argc == 2) {
1493 port = atoi(argv[1]);
1496 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1497 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1498 return -1;
1501 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
1502 if (ret == -1) {
1503 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
1504 return -1;
1507 if (options.machinereadable){
1508 printf(":source ip:port:destination ip:port:\n");
1509 for (i=0;i<list->tickles.num;i++) {
1510 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1511 continue;
1513 printf(":%s:%u", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1514 printf(":%s:%u:\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
1516 } else {
1517 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
1518 printf("Num tickles:%u\n", list->tickles.num);
1519 for (i=0;i<list->tickles.num;i++) {
1520 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1521 continue;
1523 printf("SRC: %s:%u ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1524 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));
1528 talloc_free(list);
1530 return 0;
1534 static int move_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1536 struct ctdb_all_public_ips *ips;
1537 struct ctdb_public_ip ip;
1538 int i, ret;
1539 uint32_t *nodes;
1540 uint32_t disable_time;
1541 TDB_DATA data;
1542 struct ctdb_node_map *nodemap=NULL;
1543 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1545 disable_time = 30;
1546 data.dptr = (uint8_t*)&disable_time;
1547 data.dsize = sizeof(disable_time);
1548 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1549 if (ret != 0) {
1550 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1551 return -1;
1556 /* read the public ip list from the node */
1557 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
1558 if (ret != 0) {
1559 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
1560 talloc_free(tmp_ctx);
1561 return -1;
1564 for (i=0;i<ips->num;i++) {
1565 if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
1566 break;
1569 if (i==ips->num) {
1570 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
1571 pnn, ctdb_addr_to_str(addr)));
1572 talloc_free(tmp_ctx);
1573 return -1;
1576 ip.pnn = pnn;
1577 ip.addr = *addr;
1579 data.dptr = (uint8_t *)&ip;
1580 data.dsize = sizeof(ip);
1582 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1583 if (ret != 0) {
1584 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1585 talloc_free(tmp_ctx);
1586 return ret;
1589 nodes = list_of_active_nodes_except_pnn(ctdb, nodemap, tmp_ctx, pnn);
1590 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1591 nodes, 0,
1592 LONGTIMELIMIT(),
1593 false, data,
1594 NULL, NULL,
1595 NULL);
1596 if (ret != 0) {
1597 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1598 talloc_free(tmp_ctx);
1599 return -1;
1602 ret = ctdb_ctrl_takeover_ip(ctdb, LONGTIMELIMIT(), pnn, &ip);
1603 if (ret != 0) {
1604 DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
1605 talloc_free(tmp_ctx);
1606 return -1;
1609 /* update the recovery daemon so it now knows to expect the new
1610 node assignment for this ip.
1612 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_RECD_UPDATE_IP, data);
1613 if (ret != 0) {
1614 DEBUG(DEBUG_ERR,("Failed to send message to update the ip on the recovery master.\n"));
1615 return -1;
1618 talloc_free(tmp_ctx);
1619 return 0;
1624 * scans all other nodes and returns a pnn for another node that can host this
1625 * ip address or -1
1627 static int
1628 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1630 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1631 struct ctdb_all_public_ips *ips;
1632 struct ctdb_node_map *nodemap=NULL;
1633 int i, j, ret;
1635 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1636 if (ret != 0) {
1637 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1638 talloc_free(tmp_ctx);
1639 return ret;
1642 for(i=0;i<nodemap->num;i++){
1643 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1644 continue;
1646 if (nodemap->nodes[i].pnn == options.pnn) {
1647 continue;
1650 /* read the public ip list from this node */
1651 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
1652 if (ret != 0) {
1653 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1654 return -1;
1657 for (j=0;j<ips->num;j++) {
1658 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1659 talloc_free(tmp_ctx);
1660 return nodemap->nodes[i].pnn;
1663 talloc_free(ips);
1666 talloc_free(tmp_ctx);
1667 return -1;
1670 /* If pnn is -1 then try to find a node to move IP to... */
1671 static bool try_moveip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1673 bool pnn_specified = (pnn == -1 ? false : true);
1674 int retries = 0;
1676 while (retries < 5) {
1677 if (!pnn_specified) {
1678 pnn = find_other_host_for_public_ip(ctdb, addr);
1679 if (pnn == -1) {
1680 return false;
1682 DEBUG(DEBUG_NOTICE,
1683 ("Trying to move public IP to node %u\n", pnn));
1686 if (move_ip(ctdb, addr, pnn) == 0) {
1687 return true;
1690 sleep(3);
1691 retries++;
1694 return false;
1699 move/failover an ip address to a specific node
1701 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
1703 uint32_t pnn;
1704 ctdb_sock_addr addr;
1706 if (argc < 2) {
1707 usage();
1708 return -1;
1711 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1712 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1713 return -1;
1717 if (sscanf(argv[1], "%u", &pnn) != 1) {
1718 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
1719 return -1;
1722 if (!try_moveip(ctdb, &addr, pnn)) {
1723 DEBUG(DEBUG_ERR,("Failed to move IP to node %d.\n", pnn));
1724 return -1;
1727 return 0;
1730 static int rebalance_node(struct ctdb_context *ctdb, uint32_t pnn)
1732 uint32_t recmaster;
1733 TDB_DATA data;
1735 if (ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), pnn, &recmaster) != 0) {
1736 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", pnn));
1737 return -1;
1740 data.dptr = (uint8_t *)&pnn;
1741 data.dsize = sizeof(uint32_t);
1742 if (ctdb_client_send_message(ctdb, recmaster, CTDB_SRVID_REBALANCE_NODE, data) != 0) {
1743 DEBUG(DEBUG_ERR,("Failed to send message to force node reallocation\n"));
1744 return -1;
1747 return 0;
1752 rebalance a node by setting it to allow failback and triggering a
1753 takeover run
1755 static int control_rebalancenode(struct ctdb_context *ctdb, int argc, const char **argv)
1757 switch (options.pnn) {
1758 case CTDB_BROADCAST_ALL:
1759 case CTDB_CURRENT_NODE:
1760 DEBUG(DEBUG_ERR,("You must specify a node number with -n <pnn> for the node to rebalance\n"));
1761 return -1;
1764 return rebalance_node(ctdb, options.pnn);
1768 static int rebalance_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1770 struct ctdb_public_ip ip;
1771 int ret;
1772 uint32_t *nodes;
1773 uint32_t disable_time;
1774 TDB_DATA data;
1775 struct ctdb_node_map *nodemap=NULL;
1776 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1778 disable_time = 30;
1779 data.dptr = (uint8_t*)&disable_time;
1780 data.dsize = sizeof(disable_time);
1781 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1782 if (ret != 0) {
1783 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1784 return -1;
1787 ip.pnn = -1;
1788 ip.addr = *addr;
1790 data.dptr = (uint8_t *)&ip;
1791 data.dsize = sizeof(ip);
1793 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1794 if (ret != 0) {
1795 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1796 talloc_free(tmp_ctx);
1797 return ret;
1800 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
1801 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1802 nodes, 0,
1803 LONGTIMELIMIT(),
1804 false, data,
1805 NULL, NULL,
1806 NULL);
1807 if (ret != 0) {
1808 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1809 talloc_free(tmp_ctx);
1810 return -1;
1813 talloc_free(tmp_ctx);
1814 return 0;
1818 release an ip form all nodes and have it re-assigned by recd
1820 static int control_rebalanceip(struct ctdb_context *ctdb, int argc, const char **argv)
1822 ctdb_sock_addr addr;
1824 if (argc < 1) {
1825 usage();
1826 return -1;
1829 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1830 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1831 return -1;
1834 if (rebalance_ip(ctdb, &addr) != 0) {
1835 DEBUG(DEBUG_ERR,("Error when trying to reassign ip\n"));
1836 return -1;
1839 return 0;
1842 static int getips_store_callback(void *param, void *data)
1844 struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
1845 struct ctdb_all_public_ips *ips = param;
1846 int i;
1848 i = ips->num++;
1849 ips->ips[i].pnn = node_ip->pnn;
1850 ips->ips[i].addr = node_ip->addr;
1851 return 0;
1854 static int getips_count_callback(void *param, void *data)
1856 uint32_t *count = param;
1858 (*count)++;
1859 return 0;
1862 #define IP_KEYLEN 4
1863 static uint32_t *ip_key(ctdb_sock_addr *ip)
1865 static uint32_t key[IP_KEYLEN];
1867 bzero(key, sizeof(key));
1869 switch (ip->sa.sa_family) {
1870 case AF_INET:
1871 key[0] = ip->ip.sin_addr.s_addr;
1872 break;
1873 case AF_INET6: {
1874 uint32_t *s6_a32 = (uint32_t *)&(ip->ip6.sin6_addr.s6_addr);
1875 key[0] = s6_a32[3];
1876 key[1] = s6_a32[2];
1877 key[2] = s6_a32[1];
1878 key[3] = s6_a32[0];
1879 break;
1881 default:
1882 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
1883 return key;
1886 return key;
1889 static void *add_ip_callback(void *parm, void *data)
1891 return parm;
1894 static int
1895 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
1897 struct ctdb_all_public_ips *tmp_ips;
1898 struct ctdb_node_map *nodemap=NULL;
1899 trbt_tree_t *ip_tree;
1900 int i, j, len, ret;
1901 uint32_t count;
1903 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1904 if (ret != 0) {
1905 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1906 return ret;
1909 ip_tree = trbt_create(tmp_ctx, 0);
1911 for(i=0;i<nodemap->num;i++){
1912 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1913 continue;
1915 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
1916 continue;
1919 /* read the public ip list from this node */
1920 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
1921 if (ret != 0) {
1922 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1923 return -1;
1926 for (j=0; j<tmp_ips->num;j++) {
1927 struct ctdb_public_ip *node_ip;
1929 node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
1930 node_ip->pnn = tmp_ips->ips[j].pnn;
1931 node_ip->addr = tmp_ips->ips[j].addr;
1933 trbt_insertarray32_callback(ip_tree,
1934 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
1935 add_ip_callback,
1936 node_ip);
1938 talloc_free(tmp_ips);
1941 /* traverse */
1942 count = 0;
1943 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
1945 len = offsetof(struct ctdb_all_public_ips, ips) +
1946 count*sizeof(struct ctdb_public_ip);
1947 tmp_ips = talloc_zero_size(tmp_ctx, len);
1948 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
1950 *ips = tmp_ips;
1952 return 0;
1956 static bool ipreallocate_finished;
1959 handler for receiving the response to ipreallocate
1961 static void ip_reallocate_handler(struct ctdb_context *ctdb, uint64_t srvid,
1962 TDB_DATA data, void *private_data)
1964 ipreallocate_finished = true;
1967 static void ctdb_every_second(struct event_context *ev, struct timed_event *te, struct timeval t, void *p)
1969 struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
1971 event_add_timed(ctdb->ev, ctdb,
1972 timeval_current_ofs(1, 0),
1973 ctdb_every_second, ctdb);
1976 /* Send an ipreallocate to the recovery daemon on all nodes. Only the
1977 * recovery master will answer.
1979 static int ipreallocate(struct ctdb_context *ctdb)
1981 int ret;
1982 TDB_DATA data;
1983 struct takeover_run_reply rd;
1984 struct timeval tv;
1986 /* Time ticks to enable timeouts to be processed */
1987 event_add_timed(ctdb->ev, ctdb,
1988 timeval_current_ofs(1, 0),
1989 ctdb_every_second, ctdb);
1991 rd.pnn = ctdb_get_pnn(ctdb);
1992 rd.srvid = getpid();
1994 /* Register message port for reply from recovery master */
1995 ctdb_client_set_message_handler(ctdb, rd.srvid, ip_reallocate_handler, NULL);
1997 data.dptr = (uint8_t *)&rd;
1998 data.dsize = sizeof(rd);
2000 again:
2001 /* Send to all connected nodes. Only recmaster replies */
2002 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED,
2003 CTDB_SRVID_TAKEOVER_RUN, data);
2004 if (ret != 0) {
2005 /* This can only happen if the socket is closed and
2006 * there's no way to recover from that, so don't try
2007 * again.
2009 DEBUG(DEBUG_WARNING,
2010 ("Failed to send IP reallocation request to connected nodes\n"));
2011 return -1;
2014 tv = timeval_current();
2015 /* This loop terminates the reply is received */
2016 while (timeval_elapsed(&tv) < 5.0 && !ipreallocate_finished) {
2017 event_loop_once(ctdb->ev);
2020 if (!ipreallocate_finished) {
2021 DEBUG(DEBUG_NOTICE,
2022 ("Still waiting for confirmation of IP reallocation\n"));
2023 goto again;
2026 return 0;
2030 static int control_ipreallocate(struct ctdb_context *ctdb, int argc, const char **argv)
2032 return ipreallocate(ctdb);
2036 add a public ip address to a node
2038 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
2040 int i, ret;
2041 int len, retries = 0;
2042 unsigned mask;
2043 ctdb_sock_addr addr;
2044 struct ctdb_control_ip_iface *pub;
2045 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2046 struct ctdb_all_public_ips *ips;
2049 if (argc != 2) {
2050 talloc_free(tmp_ctx);
2051 usage();
2054 if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
2055 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
2056 talloc_free(tmp_ctx);
2057 return -1;
2060 /* read the public ip list from the node */
2061 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2062 if (ret != 0) {
2063 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", options.pnn));
2064 talloc_free(tmp_ctx);
2065 return -1;
2067 for (i=0;i<ips->num;i++) {
2068 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2069 DEBUG(DEBUG_ERR,("Can not add ip to node. Node already hosts this ip\n"));
2070 return 0;
2076 /* Dont timeout. This command waits for an ip reallocation
2077 which sometimes can take wuite a while if there has
2078 been a recent recovery
2080 alarm(0);
2082 len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
2083 pub = talloc_size(tmp_ctx, len);
2084 CTDB_NO_MEMORY(ctdb, pub);
2086 pub->addr = addr;
2087 pub->mask = mask;
2088 pub->len = strlen(argv[1])+1;
2089 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
2091 do {
2092 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
2093 if (ret != 0) {
2094 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options.pnn));
2095 sleep(3);
2096 retries++;
2098 } while (retries < 5 && ret != 0);
2099 if (ret != 0) {
2100 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Giving up.\n", options.pnn));
2101 talloc_free(tmp_ctx);
2102 return ret;
2105 if (rebalance_node(ctdb, options.pnn) != 0) {
2106 DEBUG(DEBUG_ERR,("Error when trying to rebalance node\n"));
2107 return ret;
2110 talloc_free(tmp_ctx);
2111 return 0;
2115 add a public ip address to a node
2117 static int control_ipiface(struct ctdb_context *ctdb, int argc, const char **argv)
2119 ctdb_sock_addr addr;
2121 if (argc != 1) {
2122 usage();
2125 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2126 printf("Badly formed ip : %s\n", argv[0]);
2127 return -1;
2130 printf("IP on interface %s\n", ctdb_sys_find_ifname(&addr));
2132 return 0;
2135 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
2137 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
2139 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2140 struct ctdb_node_map *nodemap=NULL;
2141 struct ctdb_all_public_ips *ips;
2142 int ret, i, j;
2144 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2145 if (ret != 0) {
2146 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
2147 return ret;
2150 /* remove it from the nodes that are not hosting the ip currently */
2151 for(i=0;i<nodemap->num;i++){
2152 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2153 continue;
2155 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
2156 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2157 continue;
2160 for (j=0;j<ips->num;j++) {
2161 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2162 break;
2165 if (j==ips->num) {
2166 continue;
2169 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
2170 continue;
2173 options.pnn = nodemap->nodes[i].pnn;
2174 control_delip(ctdb, argc, argv);
2178 /* remove it from every node (also the one hosting it) */
2179 for(i=0;i<nodemap->num;i++){
2180 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2181 continue;
2183 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
2184 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2185 continue;
2188 for (j=0;j<ips->num;j++) {
2189 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2190 break;
2193 if (j==ips->num) {
2194 continue;
2197 options.pnn = nodemap->nodes[i].pnn;
2198 control_delip(ctdb, argc, argv);
2201 talloc_free(tmp_ctx);
2202 return 0;
2206 delete a public ip address from a node
2208 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
2210 int i, ret;
2211 ctdb_sock_addr addr;
2212 struct ctdb_control_ip_iface pub;
2213 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2214 struct ctdb_all_public_ips *ips;
2216 if (argc != 1) {
2217 talloc_free(tmp_ctx);
2218 usage();
2221 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2222 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2223 return -1;
2226 if (options.pnn == CTDB_BROADCAST_ALL) {
2227 return control_delip_all(ctdb, argc, argv, &addr);
2230 pub.addr = addr;
2231 pub.mask = 0;
2232 pub.len = 0;
2234 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2235 if (ret != 0) {
2236 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
2237 talloc_free(tmp_ctx);
2238 return ret;
2241 for (i=0;i<ips->num;i++) {
2242 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2243 break;
2247 if (i==ips->num) {
2248 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
2249 ctdb_addr_to_str(&addr)));
2250 talloc_free(tmp_ctx);
2251 return -1;
2254 /* This is an optimisation. If this node is hosting the IP
2255 * then try to move it somewhere else without invoking a full
2256 * takeover run. We don't care if this doesn't work!
2258 if (ips->ips[i].pnn == options.pnn) {
2259 (void) try_moveip(ctdb, &addr, -1);
2262 ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
2263 if (ret != 0) {
2264 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
2265 talloc_free(tmp_ctx);
2266 return ret;
2269 talloc_free(tmp_ctx);
2270 return 0;
2273 static int kill_tcp_from_file(struct ctdb_context *ctdb,
2274 int argc, const char **argv)
2276 struct ctdb_control_killtcp *killtcp;
2277 int max_entries, current, i;
2278 struct timeval timeout;
2279 char line[128], src[128], dst[128];
2280 int linenum;
2281 TDB_DATA data;
2282 struct client_async_data *async_data;
2283 struct ctdb_client_control_state *state;
2285 if (argc != 0) {
2286 usage();
2289 if (options.pnn == CTDB_BROADCAST_ALL ||
2290 options.pnn == CTDB_MULTICAST) {
2291 DEBUG(DEBUG_ERR, ("Can not use killtcp to multiple nodes\n"));
2292 return -1;
2295 linenum = 1;
2296 killtcp = NULL;
2297 max_entries = 0;
2298 current = 0;
2299 while (!feof(stdin)) {
2300 if (fgets(line, sizeof(line), stdin) == NULL) {
2301 continue;
2304 /* Silently skip empty lines */
2305 if (line[0] == '\n') {
2306 continue;
2309 if (sscanf(line, "%s %s\n", src, dst) != 2) {
2310 DEBUG(DEBUG_ERR, ("Bad line [%d]: '%s'\n",
2311 linenum, line));
2312 talloc_free(killtcp);
2313 return -1;
2316 if (current >= max_entries) {
2317 max_entries += 1024;
2318 killtcp = talloc_realloc(ctdb, killtcp,
2319 struct ctdb_control_killtcp,
2320 max_entries);
2321 CTDB_NO_MEMORY(ctdb, killtcp);
2324 if (!parse_ip_port(src, &killtcp[current].src_addr)) {
2325 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2326 linenum, src));
2327 talloc_free(killtcp);
2328 return -1;
2331 if (!parse_ip_port(dst, &killtcp[current].dst_addr)) {
2332 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2333 linenum, dst));
2334 talloc_free(killtcp);
2335 return -1;
2338 current++;
2341 async_data = talloc_zero(ctdb, struct client_async_data);
2342 if (async_data == NULL) {
2343 talloc_free(killtcp);
2344 return -1;
2347 for (i = 0; i < current; i++) {
2349 data.dsize = sizeof(struct ctdb_control_killtcp);
2350 data.dptr = (unsigned char *)&killtcp[i];
2352 timeout = TIMELIMIT();
2353 state = ctdb_control_send(ctdb, options.pnn, 0,
2354 CTDB_CONTROL_KILL_TCP, 0, data,
2355 async_data, &timeout, NULL);
2357 if (state == NULL) {
2358 DEBUG(DEBUG_ERR,
2359 ("Failed to call async killtcp control to node %u\n",
2360 options.pnn));
2361 talloc_free(killtcp);
2362 return -1;
2365 ctdb_client_async_add(async_data, state);
2368 if (ctdb_client_async_wait(ctdb, async_data) != 0) {
2369 DEBUG(DEBUG_ERR,("killtcp failed\n"));
2370 talloc_free(killtcp);
2371 return -1;
2374 talloc_free(killtcp);
2375 return 0;
2380 kill a tcp connection
2382 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2384 int ret;
2385 struct ctdb_control_killtcp killtcp;
2387 if (argc == 0) {
2388 return kill_tcp_from_file(ctdb, argc, argv);
2391 if (argc < 2) {
2392 usage();
2395 if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
2396 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2397 return -1;
2400 if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
2401 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2402 return -1;
2405 ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
2406 if (ret != 0) {
2407 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
2408 return ret;
2411 return 0;
2416 send a gratious arp
2418 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
2420 int ret;
2421 ctdb_sock_addr addr;
2423 if (argc < 2) {
2424 usage();
2427 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2428 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
2429 return -1;
2432 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
2433 if (ret != 0) {
2434 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
2435 return ret;
2438 return 0;
2442 register a server id
2444 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2446 int ret;
2447 struct ctdb_server_id server_id;
2449 if (argc < 3) {
2450 usage();
2453 server_id.pnn = strtoul(argv[0], NULL, 0);
2454 server_id.type = strtoul(argv[1], NULL, 0);
2455 server_id.server_id = strtoul(argv[2], NULL, 0);
2457 ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
2458 if (ret != 0) {
2459 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
2460 return ret;
2462 DEBUG(DEBUG_ERR,("Srvid registered. Sleeping for 999 seconds\n"));
2463 sleep(999);
2464 return -1;
2468 unregister a server id
2470 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2472 int ret;
2473 struct ctdb_server_id server_id;
2475 if (argc < 3) {
2476 usage();
2479 server_id.pnn = strtoul(argv[0], NULL, 0);
2480 server_id.type = strtoul(argv[1], NULL, 0);
2481 server_id.server_id = strtoul(argv[2], NULL, 0);
2483 ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
2484 if (ret != 0) {
2485 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
2486 return ret;
2488 return -1;
2492 check if a server id exists
2494 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2496 uint32_t status;
2497 int ret;
2498 struct ctdb_server_id server_id;
2500 if (argc < 3) {
2501 usage();
2504 server_id.pnn = strtoul(argv[0], NULL, 0);
2505 server_id.type = strtoul(argv[1], NULL, 0);
2506 server_id.server_id = strtoul(argv[2], NULL, 0);
2508 ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
2509 if (ret != 0) {
2510 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
2511 return ret;
2514 if (status) {
2515 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
2516 } else {
2517 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
2519 return 0;
2523 get a list of all server ids that are registered on a node
2525 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
2527 int i, ret;
2528 struct ctdb_server_id_list *server_ids;
2530 ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
2531 if (ret != 0) {
2532 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
2533 return ret;
2536 for (i=0; i<server_ids->num; i++) {
2537 printf("Server id %d:%d:%d\n",
2538 server_ids->server_ids[i].pnn,
2539 server_ids->server_ids[i].type,
2540 server_ids->server_ids[i].server_id);
2543 return -1;
2547 check if a server id exists
2549 static int check_srvids(struct ctdb_context *ctdb, int argc, const char **argv)
2551 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
2552 uint64_t *ids;
2553 uint8_t *result;
2554 int i;
2556 if (argc < 1) {
2557 talloc_free(tmp_ctx);
2558 usage();
2561 ids = talloc_array(tmp_ctx, uint64_t, argc);
2562 result = talloc_array(tmp_ctx, uint8_t, argc);
2564 for (i = 0; i < argc; i++) {
2565 ids[i] = strtoull(argv[i], NULL, 0);
2568 if (!ctdb_check_message_handlers(ctdb_connection,
2569 options.pnn, argc, ids, result)) {
2570 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n",
2571 options.pnn));
2572 talloc_free(tmp_ctx);
2573 return -1;
2576 for (i=0; i < argc; i++) {
2577 printf("Server id %d:%llu %s\n", options.pnn, (long long)ids[i],
2578 result[i] ? "exists" : "does not exist");
2581 talloc_free(tmp_ctx);
2582 return 0;
2586 send a tcp tickle ack
2588 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2590 int ret;
2591 ctdb_sock_addr src, dst;
2593 if (argc < 2) {
2594 usage();
2597 if (!parse_ip_port(argv[0], &src)) {
2598 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2599 return -1;
2602 if (!parse_ip_port(argv[1], &dst)) {
2603 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2604 return -1;
2607 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
2608 if (ret==0) {
2609 return 0;
2611 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
2613 return -1;
2618 display public ip status
2620 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
2622 int i, ret;
2623 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2624 struct ctdb_all_public_ips *ips;
2626 if (options.pnn == CTDB_BROADCAST_ALL) {
2627 /* read the list of public ips from all nodes */
2628 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
2629 } else {
2630 /* read the public ip list from this node */
2631 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2633 if (ret != 0) {
2634 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
2635 talloc_free(tmp_ctx);
2636 return ret;
2639 if (options.machinereadable){
2640 printf(":Public IP:Node:");
2641 if (options.verbose){
2642 printf("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2644 printf("\n");
2645 } else {
2646 if (options.pnn == CTDB_BROADCAST_ALL) {
2647 printf("Public IPs on ALL nodes\n");
2648 } else {
2649 printf("Public IPs on node %u\n", options.pnn);
2653 for (i=1;i<=ips->num;i++) {
2654 struct ctdb_control_public_ip_info *info = NULL;
2655 int32_t pnn;
2656 char *aciface = NULL;
2657 char *avifaces = NULL;
2658 char *cifaces = NULL;
2660 if (options.pnn == CTDB_BROADCAST_ALL) {
2661 pnn = ips->ips[ips->num-i].pnn;
2662 } else {
2663 pnn = options.pnn;
2666 if (pnn != -1) {
2667 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
2668 &ips->ips[ips->num-i].addr, &info);
2669 } else {
2670 ret = -1;
2673 if (ret == 0) {
2674 int j;
2675 for (j=0; j < info->num; j++) {
2676 if (cifaces == NULL) {
2677 cifaces = talloc_strdup(info,
2678 info->ifaces[j].name);
2679 } else {
2680 cifaces = talloc_asprintf_append(cifaces,
2681 ",%s",
2682 info->ifaces[j].name);
2685 if (info->active_idx == j) {
2686 aciface = info->ifaces[j].name;
2689 if (info->ifaces[j].link_state == 0) {
2690 continue;
2693 if (avifaces == NULL) {
2694 avifaces = talloc_strdup(info, info->ifaces[j].name);
2695 } else {
2696 avifaces = talloc_asprintf_append(avifaces,
2697 ",%s",
2698 info->ifaces[j].name);
2703 if (options.machinereadable){
2704 printf(":%s:%d:",
2705 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2706 ips->ips[ips->num-i].pnn);
2707 if (options.verbose){
2708 printf("%s:%s:%s:",
2709 aciface?aciface:"",
2710 avifaces?avifaces:"",
2711 cifaces?cifaces:"");
2713 printf("\n");
2714 } else {
2715 if (options.verbose) {
2716 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
2717 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2718 ips->ips[ips->num-i].pnn,
2719 aciface?aciface:"",
2720 avifaces?avifaces:"",
2721 cifaces?cifaces:"");
2722 } else {
2723 printf("%s %d\n",
2724 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2725 ips->ips[ips->num-i].pnn);
2728 talloc_free(info);
2731 talloc_free(tmp_ctx);
2732 return 0;
2736 public ip info
2738 static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
2740 int i, ret;
2741 ctdb_sock_addr addr;
2742 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2743 struct ctdb_control_public_ip_info *info;
2745 if (argc != 1) {
2746 talloc_free(tmp_ctx);
2747 usage();
2750 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2751 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2752 return -1;
2755 /* read the public ip info from this node */
2756 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), options.pnn,
2757 tmp_ctx, &addr, &info);
2758 if (ret != 0) {
2759 DEBUG(DEBUG_ERR, ("Unable to get public ip[%s]info from node %u\n",
2760 argv[0], options.pnn));
2761 talloc_free(tmp_ctx);
2762 return ret;
2765 printf("Public IP[%s] info on node %u\n",
2766 ctdb_addr_to_str(&info->ip.addr),
2767 options.pnn);
2769 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
2770 ctdb_addr_to_str(&info->ip.addr),
2771 info->ip.pnn, info->num);
2773 for (i=0; i<info->num; i++) {
2774 info->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
2776 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
2777 i+1, info->ifaces[i].name,
2778 info->ifaces[i].link_state?"up":"down",
2779 (unsigned int)info->ifaces[i].references,
2780 (i==info->active_idx)?" (active)":"");
2783 talloc_free(tmp_ctx);
2784 return 0;
2788 display interfaces status
2790 static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
2792 int i;
2793 struct ctdb_ifaces_list *ifaces;
2795 /* read the public ip list from this node */
2796 if (!ctdb_getifaces(ctdb_connection, options.pnn, &ifaces)) {
2797 DEBUG(DEBUG_ERR, ("Unable to get interfaces from node %u\n",
2798 options.pnn));
2799 return -1;
2802 if (options.machinereadable){
2803 printf(":Name:LinkStatus:References:\n");
2804 } else {
2805 printf("Interfaces on node %u\n", options.pnn);
2808 for (i=0; i<ifaces->num; i++) {
2809 if (options.machinereadable){
2810 printf(":%s:%s:%u\n",
2811 ifaces->ifaces[i].name,
2812 ifaces->ifaces[i].link_state?"1":"0",
2813 (unsigned int)ifaces->ifaces[i].references);
2814 } else {
2815 printf("name:%s link:%s references:%u\n",
2816 ifaces->ifaces[i].name,
2817 ifaces->ifaces[i].link_state?"up":"down",
2818 (unsigned int)ifaces->ifaces[i].references);
2822 ctdb_free_ifaces(ifaces);
2823 return 0;
2828 set link status of an interface
2830 static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
2832 int ret;
2833 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2834 struct ctdb_control_iface_info info;
2836 ZERO_STRUCT(info);
2838 if (argc != 2) {
2839 usage();
2842 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
2843 DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
2844 argv[0]));
2845 talloc_free(tmp_ctx);
2846 return -1;
2848 strcpy(info.name, argv[0]);
2850 if (strcmp(argv[1], "up") == 0) {
2851 info.link_state = 1;
2852 } else if (strcmp(argv[1], "down") == 0) {
2853 info.link_state = 0;
2854 } else {
2855 DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
2856 argv[1]));
2857 talloc_free(tmp_ctx);
2858 return -1;
2861 /* read the public ip list from this node */
2862 ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
2863 tmp_ctx, &info);
2864 if (ret != 0) {
2865 DEBUG(DEBUG_ERR, ("Unable to set link state for interfaces %s node %u\n",
2866 argv[0], options.pnn));
2867 talloc_free(tmp_ctx);
2868 return ret;
2871 talloc_free(tmp_ctx);
2872 return 0;
2876 display pid of a ctdb daemon
2878 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
2880 uint32_t pid;
2881 int ret;
2883 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
2884 if (ret != 0) {
2885 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
2886 return ret;
2888 printf("Pid:%d\n", pid);
2890 return 0;
2894 disable a remote node
2896 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
2898 int ret;
2899 struct ctdb_node_map *nodemap=NULL;
2901 /* check if the node is already disabled */
2902 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2903 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2904 exit(10);
2906 if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2907 DEBUG(DEBUG_ERR,("Node %d is already disabled.\n", options.pnn));
2908 return 0;
2911 do {
2912 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, NODE_FLAGS_PERMANENTLY_DISABLED, 0);
2913 if (ret != 0) {
2914 DEBUG(DEBUG_ERR, ("Unable to disable node %u\n", options.pnn));
2915 return ret;
2918 sleep(1);
2920 /* read the nodemap and verify the change took effect */
2921 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2922 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2923 exit(10);
2926 } while (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED));
2927 ret = ipreallocate(ctdb);
2928 if (ret != 0) {
2929 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2930 return ret;
2933 return 0;
2937 enable a disabled remote node
2939 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
2941 int ret;
2943 struct ctdb_node_map *nodemap=NULL;
2946 /* check if the node is already enabled */
2947 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2948 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2949 exit(10);
2951 if (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED)) {
2952 DEBUG(DEBUG_ERR,("Node %d is already enabled.\n", options.pnn));
2953 return 0;
2956 do {
2957 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, 0, NODE_FLAGS_PERMANENTLY_DISABLED);
2958 if (ret != 0) {
2959 DEBUG(DEBUG_ERR, ("Unable to enable node %u\n", options.pnn));
2960 return ret;
2963 sleep(1);
2965 /* read the nodemap and verify the change took effect */
2966 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2967 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2968 exit(10);
2971 } while (nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED);
2973 ret = ipreallocate(ctdb);
2974 if (ret != 0) {
2975 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2976 return ret;
2979 return 0;
2983 stop a remote node
2985 static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
2987 int ret;
2988 struct ctdb_node_map *nodemap=NULL;
2990 do {
2991 ret = ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn);
2992 if (ret != 0) {
2993 DEBUG(DEBUG_ERR, ("Unable to stop node %u try again\n", options.pnn));
2996 sleep(1);
2998 /* read the nodemap and verify the change took effect */
2999 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
3000 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3003 } while (nodemap == NULL || !(nodemap->nodes[options.pnn].flags & NODE_FLAGS_STOPPED));
3004 ret = ipreallocate(ctdb);
3005 if (ret != 0) {
3006 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
3007 return ret;
3010 return 0;
3014 restart a stopped remote node
3016 static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
3018 int ret;
3020 struct ctdb_node_map *nodemap=NULL;
3022 do {
3023 ret = ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn);
3024 if (ret != 0) {
3025 DEBUG(DEBUG_ERR, ("Unable to continue node %u\n", options.pnn));
3026 return ret;
3029 sleep(1);
3031 /* read the nodemap and verify the change took effect */
3032 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
3033 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3036 } while (nodemap == NULL || nodemap->nodes[options.pnn].flags & NODE_FLAGS_STOPPED);
3037 ret = ipreallocate(ctdb);
3038 if (ret != 0) {
3039 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
3040 return ret;
3043 return 0;
3046 static uint32_t get_generation(struct ctdb_context *ctdb)
3048 struct ctdb_vnn_map *vnnmap=NULL;
3049 int ret;
3051 /* wait until the recmaster is not in recovery mode */
3052 while (1) {
3053 uint32_t recmode, recmaster;
3055 if (vnnmap != NULL) {
3056 talloc_free(vnnmap);
3057 vnnmap = NULL;
3060 /* get the recmaster */
3061 if (!ctdb_getrecmaster(ctdb_connection, CTDB_CURRENT_NODE, &recmaster)) {
3062 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
3063 exit(10);
3066 /* get recovery mode */
3067 if (!ctdb_getrecmode(ctdb_connection, recmaster, &recmode)) {
3068 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
3069 exit(10);
3072 /* get the current generation number */
3073 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, ctdb, &vnnmap);
3074 if (ret != 0) {
3075 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
3076 exit(10);
3079 if ((recmode == CTDB_RECOVERY_NORMAL)
3080 && (vnnmap->generation != 1)){
3081 return vnnmap->generation;
3083 sleep(1);
3088 ban a node from the cluster
3090 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
3092 int ret;
3093 struct ctdb_node_map *nodemap=NULL;
3094 struct ctdb_ban_time bantime;
3096 if (argc < 1) {
3097 usage();
3100 /* verify the node exists */
3101 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3102 if (ret != 0) {
3103 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3104 return ret;
3107 if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_BANNED) {
3108 DEBUG(DEBUG_ERR,("Node %u is already banned.\n", options.pnn));
3109 return -1;
3112 bantime.pnn = options.pnn;
3113 bantime.time = strtoul(argv[0], NULL, 0);
3115 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, &bantime);
3116 if (ret != 0) {
3117 DEBUG(DEBUG_ERR,("Banning node %d for %d seconds failed.\n", bantime.pnn, bantime.time));
3118 return -1;
3121 ret = ipreallocate(ctdb);
3122 if (ret != 0) {
3123 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
3124 return ret;
3127 return 0;
3132 unban a node from the cluster
3134 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
3136 int ret;
3137 struct ctdb_node_map *nodemap=NULL;
3138 struct ctdb_ban_time bantime;
3140 /* verify the node exists */
3141 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3142 if (ret != 0) {
3143 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3144 return ret;
3147 if (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_BANNED)) {
3148 DEBUG(DEBUG_ERR,("Node %u is not banned.\n", options.pnn));
3149 return -1;
3152 bantime.pnn = options.pnn;
3153 bantime.time = 0;
3155 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, &bantime);
3156 if (ret != 0) {
3157 DEBUG(DEBUG_ERR,("Unbanning node %d failed.\n", bantime.pnn));
3158 return -1;
3161 ret = ipreallocate(ctdb);
3162 if (ret != 0) {
3163 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
3164 return ret;
3167 return 0;
3172 show ban information for a node
3174 static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
3176 int ret;
3177 struct ctdb_node_map *nodemap=NULL;
3178 struct ctdb_ban_time *bantime;
3180 /* verify the node exists */
3181 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3182 if (ret != 0) {
3183 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3184 return ret;
3187 ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
3188 if (ret != 0) {
3189 DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
3190 return -1;
3193 if (bantime->time == 0) {
3194 printf("Node %u is not banned\n", bantime->pnn);
3195 } else {
3196 printf("Node %u is banned banned for %d seconds\n", bantime->pnn, bantime->time);
3199 return 0;
3203 shutdown a daemon
3205 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
3207 int ret;
3209 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
3210 if (ret != 0) {
3211 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
3212 return ret;
3215 return 0;
3219 trigger a recovery
3221 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
3223 int ret;
3224 uint32_t generation, next_generation;
3225 bool force;
3227 /* "force" option ignores freeze failure and forces recovery */
3228 force = (argc == 1) && (strcasecmp(argv[0], "force") == 0);
3230 /* record the current generation number */
3231 generation = get_generation(ctdb);
3233 ret = ctdb_ctrl_freeze_priority(ctdb, TIMELIMIT(), options.pnn, 1);
3234 if (ret != 0) {
3235 if (!force) {
3236 DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
3237 return ret;
3239 DEBUG(DEBUG_WARNING, ("Unable to freeze node but proceeding because \"force\" option given\n"));
3242 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
3243 if (ret != 0) {
3244 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
3245 return ret;
3248 /* wait until we are in a new generation */
3249 while (1) {
3250 next_generation = get_generation(ctdb);
3251 if (next_generation != generation) {
3252 return 0;
3254 sleep(1);
3257 return 0;
3262 display monitoring mode of a remote node
3264 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
3266 uint32_t monmode;
3267 int ret;
3269 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
3270 if (ret != 0) {
3271 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
3272 return ret;
3274 if (!options.machinereadable){
3275 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
3276 } else {
3277 printf(":mode:\n");
3278 printf(":%d:\n",monmode);
3280 return 0;
3285 display capabilities of a remote node
3287 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
3289 uint32_t capabilities;
3291 if (!ctdb_getcapabilities(ctdb_connection, options.pnn, &capabilities)) {
3292 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
3293 return -1;
3296 if (!options.machinereadable){
3297 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
3298 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
3299 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
3300 printf("NATGW: %s\n", (capabilities&CTDB_CAP_NATGW)?"YES":"NO");
3301 } else {
3302 printf(":RECMASTER:LMASTER:LVS:NATGW:\n");
3303 printf(":%d:%d:%d:%d:\n",
3304 !!(capabilities&CTDB_CAP_RECMASTER),
3305 !!(capabilities&CTDB_CAP_LMASTER),
3306 !!(capabilities&CTDB_CAP_LVS),
3307 !!(capabilities&CTDB_CAP_NATGW));
3309 return 0;
3313 display lvs configuration
3315 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
3317 uint32_t *capabilities;
3318 struct ctdb_node_map *nodemap=NULL;
3319 int i, ret;
3320 int healthy_count = 0;
3322 if (!ctdb_getnodemap(ctdb_connection, options.pnn, &nodemap)) {
3323 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3324 return -1;
3327 capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
3328 CTDB_NO_MEMORY(ctdb, capabilities);
3330 ret = 0;
3332 /* collect capabilities for all connected nodes */
3333 for (i=0; i<nodemap->num; i++) {
3334 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3335 continue;
3337 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3338 continue;
3341 if (!ctdb_getcapabilities(ctdb_connection, i, &capabilities[i])) {
3342 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
3343 ret = -1;
3344 goto done;
3347 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3348 continue;
3351 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
3352 healthy_count++;
3356 /* Print all LVS nodes */
3357 for (i=0; i<nodemap->num; i++) {
3358 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3359 continue;
3361 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3362 continue;
3364 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3365 continue;
3368 if (healthy_count != 0) {
3369 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
3370 continue;
3374 printf("%d:%s\n", i,
3375 ctdb_addr_to_str(&nodemap->nodes[i].addr));
3378 done:
3379 ctdb_free_nodemap(nodemap);
3380 return ret;
3384 display who is the lvs master
3386 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
3388 uint32_t *capabilities;
3389 struct ctdb_node_map *nodemap=NULL;
3390 int i, ret;
3391 int healthy_count = 0;
3393 if (!ctdb_getnodemap(ctdb_connection, options.pnn, &nodemap)) {
3394 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3395 return -1;
3398 capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
3399 CTDB_NO_MEMORY(ctdb, capabilities);
3401 ret = -1;
3403 /* collect capabilities for all connected nodes */
3404 for (i=0; i<nodemap->num; i++) {
3405 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3406 continue;
3408 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3409 continue;
3412 if (!ctdb_getcapabilities(ctdb_connection, i, &capabilities[i])) {
3413 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
3414 ret = -1;
3415 goto done;
3418 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3419 continue;
3422 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
3423 healthy_count++;
3427 /* find and show the lvsmaster */
3428 for (i=0; i<nodemap->num; i++) {
3429 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3430 continue;
3432 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3433 continue;
3435 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3436 continue;
3439 if (healthy_count != 0) {
3440 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
3441 continue;
3445 if (options.machinereadable){
3446 printf("%d\n", i);
3447 } else {
3448 printf("Node %d is LVS master\n", i);
3450 ret = 0;
3451 goto done;
3454 printf("There is no LVS master\n");
3455 done:
3456 ctdb_free_nodemap(nodemap);
3457 return ret;
3461 disable monitoring on a node
3463 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3466 int ret;
3468 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
3469 if (ret != 0) {
3470 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
3471 return ret;
3473 printf("Monitoring mode:%s\n","DISABLED");
3475 return 0;
3479 enable monitoring on a node
3481 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3484 int ret;
3486 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
3487 if (ret != 0) {
3488 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
3489 return ret;
3491 printf("Monitoring mode:%s\n","ACTIVE");
3493 return 0;
3497 display remote list of keys/data for a db
3499 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
3501 const char *db_name;
3502 struct ctdb_db_context *ctdb_db;
3503 int ret;
3504 struct ctdb_dump_db_context c;
3505 uint8_t flags;
3507 if (argc < 1) {
3508 usage();
3511 db_name = argv[0];
3513 if (!db_exists(ctdb, db_name, NULL, &flags)) {
3514 return -1;
3517 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3518 if (ctdb_db == NULL) {
3519 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3520 return -1;
3523 if (options.printlmaster) {
3524 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn,
3525 ctdb, &ctdb->vnn_map);
3526 if (ret != 0) {
3527 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
3528 options.pnn));
3529 return ret;
3533 ZERO_STRUCT(c);
3534 c.f = stdout;
3535 c.printemptyrecords = (bool)options.printemptyrecords;
3536 c.printdatasize = (bool)options.printdatasize;
3537 c.printlmaster = (bool)options.printlmaster;
3538 c.printhash = (bool)options.printhash;
3539 c.printrecordflags = (bool)options.printrecordflags;
3541 /* traverse and dump the cluster tdb */
3542 ret = ctdb_dump_db(ctdb_db, &c);
3543 if (ret == -1) {
3544 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
3545 DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
3546 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3547 db_name));
3548 return -1;
3550 talloc_free(ctdb_db);
3552 printf("Dumped %d records\n", ret);
3553 return 0;
3556 struct cattdb_data {
3557 struct ctdb_context *ctdb;
3558 uint32_t count;
3561 static int cattdb_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
3563 struct cattdb_data *d = private_data;
3564 struct ctdb_dump_db_context c;
3566 d->count++;
3568 ZERO_STRUCT(c);
3569 c.f = stdout;
3570 c.printemptyrecords = (bool)options.printemptyrecords;
3571 c.printdatasize = (bool)options.printdatasize;
3572 c.printlmaster = false;
3573 c.printhash = (bool)options.printhash;
3574 c.printrecordflags = true;
3576 return ctdb_dumpdb_record(d->ctdb, key, data, &c);
3580 cat the local tdb database using same format as catdb
3582 static int control_cattdb(struct ctdb_context *ctdb, int argc, const char **argv)
3584 const char *db_name;
3585 struct ctdb_db_context *ctdb_db;
3586 struct cattdb_data d;
3587 uint8_t flags;
3589 if (argc < 1) {
3590 usage();
3593 db_name = argv[0];
3595 if (!db_exists(ctdb, db_name, NULL, &flags)) {
3596 return -1;
3599 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3600 if (ctdb_db == NULL) {
3601 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3602 return -1;
3605 /* traverse the local tdb */
3606 d.count = 0;
3607 d.ctdb = ctdb;
3608 if (tdb_traverse_read(ctdb_db->ltdb->tdb, cattdb_traverse, &d) == -1) {
3609 printf("Failed to cattdb data\n");
3610 exit(10);
3612 talloc_free(ctdb_db);
3614 printf("Dumped %d records\n", d.count);
3615 return 0;
3619 display the content of a database key
3621 static int control_readkey(struct ctdb_context *ctdb, int argc, const char **argv)
3623 const char *db_name;
3624 struct ctdb_db_context *ctdb_db;
3625 struct ctdb_record_handle *h;
3626 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3627 TDB_DATA key, data;
3628 uint8_t flags;
3630 if (argc < 2) {
3631 usage();
3634 db_name = argv[0];
3636 if (!db_exists(ctdb, db_name, NULL, &flags)) {
3637 return -1;
3640 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3641 if (ctdb_db == NULL) {
3642 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3643 return -1;
3646 key.dptr = discard_const(argv[1]);
3647 key.dsize = strlen((char *)key.dptr);
3649 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3650 if (h == NULL) {
3651 printf("Failed to fetch record '%s' on node %d\n",
3652 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3653 talloc_free(tmp_ctx);
3654 exit(10);
3657 printf("Data: size:%d ptr:[%s]\n", (int)data.dsize, data.dptr);
3659 talloc_free(ctdb_db);
3660 talloc_free(tmp_ctx);
3661 return 0;
3665 display the content of a database key
3667 static int control_writekey(struct ctdb_context *ctdb, int argc, const char **argv)
3669 const char *db_name;
3670 struct ctdb_db_context *ctdb_db;
3671 struct ctdb_record_handle *h;
3672 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3673 TDB_DATA key, data;
3674 uint8_t flags;
3676 if (argc < 3) {
3677 usage();
3680 db_name = argv[0];
3682 if (!db_exists(ctdb, db_name, NULL, &flags)) {
3683 return -1;
3686 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3687 if (ctdb_db == NULL) {
3688 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3689 return -1;
3692 key.dptr = discard_const(argv[1]);
3693 key.dsize = strlen((char *)key.dptr);
3695 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3696 if (h == NULL) {
3697 printf("Failed to fetch record '%s' on node %d\n",
3698 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3699 talloc_free(tmp_ctx);
3700 exit(10);
3703 data.dptr = discard_const(argv[2]);
3704 data.dsize = strlen((char *)data.dptr);
3706 if (ctdb_record_store(h, data) != 0) {
3707 printf("Failed to store record\n");
3710 talloc_free(h);
3711 talloc_free(ctdb_db);
3712 talloc_free(tmp_ctx);
3713 return 0;
3717 fetch a record from a persistent database
3719 static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3721 const char *db_name;
3722 struct ctdb_db_context *ctdb_db;
3723 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3724 struct ctdb_transaction_handle *h;
3725 TDB_DATA key, data;
3726 int fd, ret;
3727 bool persistent;
3728 uint8_t flags;
3730 if (argc < 2) {
3731 talloc_free(tmp_ctx);
3732 usage();
3735 db_name = argv[0];
3737 if (!db_exists(ctdb, db_name, NULL, &flags)) {
3738 talloc_free(tmp_ctx);
3739 return -1;
3742 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
3743 if (!persistent) {
3744 DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", db_name));
3745 talloc_free(tmp_ctx);
3746 return -1;
3749 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3750 if (ctdb_db == NULL) {
3751 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3752 talloc_free(tmp_ctx);
3753 return -1;
3756 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3757 if (h == NULL) {
3758 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3759 talloc_free(tmp_ctx);
3760 return -1;
3763 key.dptr = discard_const(argv[1]);
3764 key.dsize = strlen(argv[1]);
3765 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
3766 if (ret != 0) {
3767 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
3768 talloc_free(tmp_ctx);
3769 return -1;
3772 if (data.dsize == 0 || data.dptr == NULL) {
3773 DEBUG(DEBUG_ERR,("Record is empty\n"));
3774 talloc_free(tmp_ctx);
3775 return -1;
3778 if (argc == 3) {
3779 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3780 if (fd == -1) {
3781 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3782 talloc_free(tmp_ctx);
3783 return -1;
3785 write(fd, data.dptr, data.dsize);
3786 close(fd);
3787 } else {
3788 write(1, data.dptr, data.dsize);
3791 /* abort the transaction */
3792 talloc_free(h);
3795 talloc_free(tmp_ctx);
3796 return 0;
3800 fetch a record from a tdb-file
3802 static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3804 const char *tdb_file;
3805 TDB_CONTEXT *tdb;
3806 TDB_DATA key, data;
3807 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
3808 int fd;
3810 if (argc < 2) {
3811 usage();
3814 tdb_file = argv[0];
3816 tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
3817 if (tdb == NULL) {
3818 printf("Failed to open TDB file %s\n", tdb_file);
3819 return -1;
3822 if (!strncmp(argv[1], "0x", 2)) {
3823 key = hextodata(tmp_ctx, argv[1] + 2);
3824 if (key.dsize == 0) {
3825 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
3826 return -1;
3828 } else {
3829 key.dptr = discard_const(argv[1]);
3830 key.dsize = strlen(argv[1]);
3833 data = tdb_fetch(tdb, key);
3834 if (data.dptr == NULL || data.dsize < sizeof(struct ctdb_ltdb_header)) {
3835 printf("Failed to read record %s from tdb %s\n", argv[1], tdb_file);
3836 tdb_close(tdb);
3837 return -1;
3840 tdb_close(tdb);
3842 if (argc == 3) {
3843 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3844 if (fd == -1) {
3845 printf("Failed to open output file %s\n", argv[2]);
3846 return -1;
3848 if (options.verbose){
3849 write(fd, data.dptr, data.dsize);
3850 } else {
3851 write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3853 close(fd);
3854 } else {
3855 if (options.verbose){
3856 write(1, data.dptr, data.dsize);
3857 } else {
3858 write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3862 talloc_free(tmp_ctx);
3863 return 0;
3867 store a record and header to a tdb-file
3869 static int control_tstore(struct ctdb_context *ctdb, int argc, const char **argv)
3871 const char *tdb_file;
3872 TDB_CONTEXT *tdb;
3873 TDB_DATA key, data;
3874 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
3876 if (argc < 3) {
3877 usage();
3880 tdb_file = argv[0];
3882 tdb = tdb_open(tdb_file, 0, 0, O_RDWR, 0);
3883 if (tdb == NULL) {
3884 printf("Failed to open TDB file %s\n", tdb_file);
3885 return -1;
3888 if (!strncmp(argv[1], "0x", 2)) {
3889 key = hextodata(tmp_ctx, argv[1] + 2);
3890 if (key.dsize == 0) {
3891 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
3892 return -1;
3894 } else {
3895 key.dptr = discard_const(argv[1]);
3896 key.dsize = strlen(argv[1]);
3899 if (!strncmp(argv[2], "0x", 2)) {
3900 data = hextodata(tmp_ctx, argv[2] + 2);
3901 if (data.dsize == 0) {
3902 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[2]);
3903 return -1;
3905 } else {
3906 data.dptr = discard_const(argv[2]);
3907 data.dsize = strlen(argv[2]);
3910 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
3911 printf("Not enough data. You must specify the full ctdb_ltdb_header too when storing\n");
3912 return -1;
3914 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0) {
3915 printf("Failed to write record %s to tdb %s\n", argv[1], tdb_file);
3916 tdb_close(tdb);
3917 return -1;
3920 tdb_close(tdb);
3922 talloc_free(tmp_ctx);
3923 return 0;
3927 write a record to a persistent database
3929 static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv)
3931 const char *db_name;
3932 struct ctdb_db_context *ctdb_db;
3933 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3934 struct ctdb_transaction_handle *h;
3935 struct stat st;
3936 TDB_DATA key, data;
3937 int fd, ret;
3939 if (argc < 3) {
3940 talloc_free(tmp_ctx);
3941 usage();
3944 fd = open(argv[2], O_RDONLY);
3945 if (fd == -1) {
3946 DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s %s\n", argv[2], strerror(errno)));
3947 talloc_free(tmp_ctx);
3948 return -1;
3951 ret = fstat(fd, &st);
3952 if (ret == -1) {
3953 DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
3954 close(fd);
3955 talloc_free(tmp_ctx);
3956 return -1;
3959 if (!S_ISREG(st.st_mode)) {
3960 DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
3961 close(fd);
3962 talloc_free(tmp_ctx);
3963 return -1;
3966 data.dsize = st.st_size;
3967 if (data.dsize == 0) {
3968 data.dptr = NULL;
3969 } else {
3970 data.dptr = talloc_size(tmp_ctx, data.dsize);
3971 if (data.dptr == NULL) {
3972 DEBUG(DEBUG_ERR,("Failed to talloc %d of memory to store record data\n", (int)data.dsize));
3973 close(fd);
3974 talloc_free(tmp_ctx);
3975 return -1;
3977 ret = read(fd, data.dptr, data.dsize);
3978 if (ret != data.dsize) {
3979 DEBUG(DEBUG_ERR,("Failed to read %d bytes of record data\n", (int)data.dsize));
3980 close(fd);
3981 talloc_free(tmp_ctx);
3982 return -1;
3985 close(fd);
3988 db_name = argv[0];
3990 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
3991 if (ctdb_db == NULL) {
3992 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3993 talloc_free(tmp_ctx);
3994 return -1;
3997 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3998 if (h == NULL) {
3999 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4000 talloc_free(tmp_ctx);
4001 return -1;
4004 key.dptr = discard_const(argv[1]);
4005 key.dsize = strlen(argv[1]);
4006 ret = ctdb_transaction_store(h, key, data);
4007 if (ret != 0) {
4008 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4009 talloc_free(tmp_ctx);
4010 return -1;
4013 ret = ctdb_transaction_commit(h);
4014 if (ret != 0) {
4015 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4016 talloc_free(tmp_ctx);
4017 return -1;
4021 talloc_free(tmp_ctx);
4022 return 0;
4026 * delete a record from a persistent database
4028 static int control_pdelete(struct ctdb_context *ctdb, int argc, const char **argv)
4030 const char *db_name;
4031 struct ctdb_db_context *ctdb_db;
4032 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4033 struct ctdb_transaction_handle *h;
4034 TDB_DATA key;
4035 int ret;
4036 bool persistent;
4037 uint8_t flags;
4039 if (argc < 2) {
4040 talloc_free(tmp_ctx);
4041 usage();
4044 db_name = argv[0];
4046 if (!db_exists(ctdb, db_name, NULL, &flags)) {
4047 talloc_free(tmp_ctx);
4048 return -1;
4051 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
4052 if (!persistent) {
4053 DEBUG(DEBUG_ERR, ("Database '%s' is not persistent\n", db_name));
4054 talloc_free(tmp_ctx);
4055 return -1;
4058 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4059 if (ctdb_db == NULL) {
4060 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n", db_name));
4061 talloc_free(tmp_ctx);
4062 return -1;
4065 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4066 if (h == NULL) {
4067 DEBUG(DEBUG_ERR, ("Failed to start transaction on database %s\n", db_name));
4068 talloc_free(tmp_ctx);
4069 return -1;
4072 key.dptr = discard_const(argv[1]);
4073 key.dsize = strlen(argv[1]);
4074 ret = ctdb_transaction_store(h, key, tdb_null);
4075 if (ret != 0) {
4076 DEBUG(DEBUG_ERR, ("Failed to delete record\n"));
4077 talloc_free(tmp_ctx);
4078 return -1;
4081 ret = ctdb_transaction_commit(h);
4082 if (ret != 0) {
4083 DEBUG(DEBUG_ERR, ("Failed to commit transaction\n"));
4084 talloc_free(tmp_ctx);
4085 return -1;
4088 talloc_free(tmp_ctx);
4089 return 0;
4093 check if a service is bound to a port or not
4095 static int control_chktcpport(struct ctdb_context *ctdb, int argc, const char **argv)
4097 int s, ret;
4098 unsigned v;
4099 int port;
4100 struct sockaddr_in sin;
4102 if (argc != 1) {
4103 printf("Use: ctdb chktcport <port>\n");
4104 return EINVAL;
4107 port = atoi(argv[0]);
4109 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
4110 if (s == -1) {
4111 printf("Failed to open local socket\n");
4112 return errno;
4115 v = fcntl(s, F_GETFL, 0);
4116 fcntl(s, F_SETFL, v | O_NONBLOCK);
4118 bzero(&sin, sizeof(sin));
4119 sin.sin_family = PF_INET;
4120 sin.sin_port = htons(port);
4121 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
4122 close(s);
4123 if (ret == -1) {
4124 printf("Failed to bind to local socket: %d %s\n", errno, strerror(errno));
4125 return errno;
4128 return 0;
4133 static void log_handler(struct ctdb_context *ctdb, uint64_t srvid,
4134 TDB_DATA data, void *private_data)
4136 DEBUG(DEBUG_ERR,("Log data received\n"));
4137 if (data.dsize > 0) {
4138 printf("%s", data.dptr);
4141 exit(0);
4145 display a list of log messages from the in memory ringbuffer
4147 static int control_getlog(struct ctdb_context *ctdb, int argc, const char **argv)
4149 int ret, i;
4150 bool main_daemon;
4151 struct ctdb_get_log_addr log_addr;
4152 TDB_DATA data;
4153 struct timeval tv;
4155 /* Process options */
4156 main_daemon = true;
4157 log_addr.pnn = ctdb_get_pnn(ctdb);
4158 log_addr.level = DEBUG_NOTICE;
4159 for (i = 0; i < argc; i++) {
4160 if (strcmp(argv[i], "recoverd") == 0) {
4161 main_daemon = false;
4162 } else {
4163 if (isalpha(argv[i][0]) || argv[i][0] == '-') {
4164 log_addr.level = get_debug_by_desc(argv[i]);
4165 } else {
4166 log_addr.level = strtol(argv[i], NULL, 0);
4171 /* Our message port is our PID */
4172 log_addr.srvid = getpid();
4174 data.dptr = (unsigned char *)&log_addr;
4175 data.dsize = sizeof(log_addr);
4177 DEBUG(DEBUG_ERR, ("Pulling logs from node %u\n", options.pnn));
4179 ctdb_client_set_message_handler(ctdb, log_addr.srvid, log_handler, NULL);
4180 sleep(1);
4182 DEBUG(DEBUG_ERR,("Listen for response on %d\n", (int)log_addr.srvid));
4184 if (main_daemon) {
4185 int32_t res;
4186 char *errmsg;
4187 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4189 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_GET_LOG,
4190 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
4191 if (ret != 0 || res != 0) {
4192 DEBUG(DEBUG_ERR,("Failed to get logs - %s\n", errmsg));
4193 talloc_free(tmp_ctx);
4194 return -1;
4196 talloc_free(tmp_ctx);
4197 } else {
4198 ret = ctdb_client_send_message(ctdb, options.pnn,
4199 CTDB_SRVID_GETLOG, data);
4200 if (ret != 0) {
4201 DEBUG(DEBUG_ERR,("Failed to send getlog request message to %u\n", options.pnn));
4202 return -1;
4206 tv = timeval_current();
4207 /* this loop will terminate when we have received the reply */
4208 while (timeval_elapsed(&tv) < (double)options.timelimit) {
4209 event_loop_once(ctdb->ev);
4212 DEBUG(DEBUG_INFO,("Timed out waiting for log data.\n"));
4214 return 0;
4218 clear the in memory log area
4220 static int control_clearlog(struct ctdb_context *ctdb, int argc, const char **argv)
4222 int ret;
4224 if (argc == 0 || (argc >= 1 && strcmp(argv[0], "recoverd") != 0)) {
4225 /* "recoverd" not given - get logs from main daemon */
4226 int32_t res;
4227 char *errmsg;
4228 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4230 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_CLEAR_LOG,
4231 0, tdb_null, tmp_ctx, NULL, &res, NULL, &errmsg);
4232 if (ret != 0 || res != 0) {
4233 DEBUG(DEBUG_ERR,("Failed to clear logs\n"));
4234 talloc_free(tmp_ctx);
4235 return -1;
4238 talloc_free(tmp_ctx);
4239 } else {
4240 TDB_DATA data; /* unused in recoverd... */
4241 data.dsize = 0;
4243 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_CLEARLOG, data);
4244 if (ret != 0) {
4245 DEBUG(DEBUG_ERR,("Failed to send clearlog request message to %u\n", options.pnn));
4246 return -1;
4250 return 0;
4254 static uint32_t reloadips_finished;
4256 static void reloadips_handler(struct ctdb_context *ctdb, uint64_t srvid,
4257 TDB_DATA data, void *private_data)
4259 reloadips_finished = 1;
4262 static int reloadips_all(struct ctdb_context *ctdb)
4264 struct reloadips_all_reply rips;
4265 struct ctdb_node_map *nodemap=NULL;
4266 TDB_DATA data;
4267 uint32_t recmaster;
4268 int ret, i;
4270 /* check that there are valid nodes available */
4271 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
4272 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
4273 return 1;
4275 for (i=0; i<nodemap->num;i++) {
4276 if (nodemap->nodes[i].flags != 0) {
4277 DEBUG(DEBUG_ERR,("reloadips -n all can only be used when all nodes are up and healthy. Aborting due to problem with node %d\n", i));
4278 return 1;
4282 rips.pnn = ctdb_get_pnn(ctdb);
4283 rips.srvid = getpid();
4285 /* register a message port for receiveing the reply so that we
4286 can receive the reply
4288 ctdb_client_set_message_handler(ctdb, rips.srvid, reloadips_handler, NULL);
4290 if (!ctdb_getrecmaster(ctdb_connection, CTDB_CURRENT_NODE, &recmaster)) {
4291 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node\n"));
4292 return -1;
4296 data.dptr = (uint8_t *)&rips;
4297 data.dsize = sizeof(rips);
4299 ret = ctdb_client_send_message(ctdb, recmaster, CTDB_SRVID_RELOAD_ALL_IPS, data);
4300 if (ret != 0) {
4301 DEBUG(DEBUG_ERR,("Failed to send reload all ips request message to %u\n", options.pnn));
4302 return 1;
4305 reloadips_finished = 0;
4306 while (reloadips_finished == 0) {
4307 event_loop_once(ctdb->ev);
4310 return 0;
4314 reload public ips on a specific node
4316 static int control_reloadips(struct ctdb_context *ctdb, int argc, const char **argv)
4318 int ret;
4319 int32_t res;
4320 char *errmsg;
4321 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4323 if (options.pnn == CTDB_BROADCAST_ALL) {
4324 return reloadips_all(ctdb);
4327 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RELOAD_PUBLIC_IPS,
4328 0, tdb_null, tmp_ctx, NULL, &res, NULL, &errmsg);
4329 if (ret != 0 || res != 0) {
4330 DEBUG(DEBUG_ERR,("Failed to reload ips\n"));
4331 talloc_free(tmp_ctx);
4332 return -1;
4335 talloc_free(tmp_ctx);
4336 return 0;
4340 display a list of the databases on a remote ctdb
4342 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
4344 int i, ret;
4345 struct ctdb_dbid_map *dbmap=NULL;
4347 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
4348 if (ret != 0) {
4349 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4350 return ret;
4353 if(options.machinereadable){
4354 printf(":ID:Name:Path:Persistent:Sticky:Unhealthy:ReadOnly:\n");
4355 for(i=0;i<dbmap->num;i++){
4356 const char *path;
4357 const char *name;
4358 const char *health;
4359 bool persistent;
4360 bool readonly;
4361 bool sticky;
4363 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
4364 dbmap->dbs[i].dbid, ctdb, &path);
4365 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
4366 dbmap->dbs[i].dbid, ctdb, &name);
4367 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
4368 dbmap->dbs[i].dbid, ctdb, &health);
4369 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4370 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4371 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4372 printf(":0x%08X:%s:%s:%d:%d:%d:%d:\n",
4373 dbmap->dbs[i].dbid, name, path,
4374 !!(persistent), !!(sticky),
4375 !!(health), !!(readonly));
4377 return 0;
4380 printf("Number of databases:%d\n", dbmap->num);
4381 for(i=0;i<dbmap->num;i++){
4382 const char *path;
4383 const char *name;
4384 const char *health;
4385 bool persistent;
4386 bool readonly;
4387 bool sticky;
4389 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
4390 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
4391 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
4392 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4393 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4394 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4395 printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
4396 dbmap->dbs[i].dbid, name, path,
4397 persistent?" PERSISTENT":"",
4398 sticky?" STICKY":"",
4399 readonly?" READONLY":"",
4400 health?" UNHEALTHY":"");
4403 return 0;
4407 display the status of a database on a remote ctdb
4409 static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
4411 const char *db_name;
4412 uint32_t db_id;
4413 uint8_t flags;
4414 const char *path;
4415 const char *health;
4417 if (argc < 1) {
4418 usage();
4421 db_name = argv[0];
4423 if (!db_exists(ctdb, db_name, &db_id, &flags)) {
4424 return -1;
4427 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &path);
4428 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &health);
4429 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
4430 db_id, db_name, path,
4431 (flags & CTDB_DB_FLAGS_PERSISTENT ? "yes" : "no"),
4432 (flags & CTDB_DB_FLAGS_STICKY ? "yes" : "no"),
4433 (flags & CTDB_DB_FLAGS_READONLY ? "yes" : "no"),
4434 (health ? health : "OK"));
4436 return 0;
4440 check if the local node is recmaster or not
4441 it will return 1 if this node is the recmaster and 0 if it is not
4442 or if the local ctdb daemon could not be contacted
4444 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
4446 uint32_t mypnn, recmaster;
4448 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
4449 if (mypnn == -1) {
4450 printf("Failed to get pnn of node\n");
4451 return 1;
4454 if (!ctdb_getrecmaster(ctdb_connection, options.pnn, &recmaster)) {
4455 printf("Failed to get the recmaster\n");
4456 return 1;
4459 if (recmaster != mypnn) {
4460 printf("this node is not the recmaster\n");
4461 return 1;
4464 printf("this node is the recmaster\n");
4465 return 0;
4469 ping a node
4471 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
4473 int ret;
4474 struct timeval tv = timeval_current();
4475 ret = ctdb_ctrl_ping(ctdb, options.pnn);
4476 if (ret == -1) {
4477 printf("Unable to get ping response from node %u\n", options.pnn);
4478 return -1;
4479 } else {
4480 printf("response from %u time=%.6f sec (%d clients)\n",
4481 options.pnn, timeval_elapsed(&tv), ret);
4483 return 0;
4488 get a node's runstate
4490 static int control_runstate(struct ctdb_context *ctdb, int argc, const char **argv)
4492 int ret;
4493 enum ctdb_runstate runstate;
4495 ret = ctdb_ctrl_get_runstate(ctdb, TIMELIMIT(), options.pnn, &runstate);
4496 if (ret == -1) {
4497 printf("Unable to get runstate response from node %u\n",
4498 options.pnn);
4499 return -1;
4500 } else {
4501 bool found = true;
4502 enum ctdb_runstate t;
4503 int i;
4504 for (i=0; i<argc; i++) {
4505 found = false;
4506 t = runstate_from_string(argv[i]);
4507 if (t == CTDB_RUNSTATE_UNKNOWN) {
4508 printf("Invalid run state (%s)\n", argv[i]);
4509 return -1;
4512 if (t == runstate) {
4513 found = true;
4514 break;
4518 if (!found) {
4519 printf("CTDB not in required run state (got %s)\n",
4520 runstate_to_string((enum ctdb_runstate)runstate));
4521 return -1;
4525 printf("%s\n", runstate_to_string(runstate));
4526 return 0;
4531 get a tunable
4533 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
4535 const char *name;
4536 uint32_t value;
4537 int ret;
4539 if (argc < 1) {
4540 usage();
4543 name = argv[0];
4544 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
4545 if (ret != 0) {
4546 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
4547 return -1;
4550 printf("%-23s = %u\n", name, value);
4551 return 0;
4555 set a tunable
4557 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
4559 const char *name;
4560 uint32_t value;
4561 int ret;
4563 if (argc < 2) {
4564 usage();
4567 name = argv[0];
4568 value = strtoul(argv[1], NULL, 0);
4570 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
4571 if (ret == -1) {
4572 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
4573 return -1;
4575 return 0;
4579 list all tunables
4581 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
4583 uint32_t count;
4584 const char **list;
4585 int ret, i;
4587 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
4588 if (ret == -1) {
4589 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
4590 return -1;
4593 for (i=0;i<count;i++) {
4594 control_getvar(ctdb, 1, &list[i]);
4597 talloc_free(list);
4599 return 0;
4603 display debug level on a node
4605 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4607 int ret;
4608 int32_t level;
4610 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
4611 if (ret != 0) {
4612 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
4613 return ret;
4614 } else {
4615 if (options.machinereadable){
4616 printf(":Name:Level:\n");
4617 printf(":%s:%d:\n",get_debug_by_level(level),level);
4618 } else {
4619 printf("Node %u is at debug level %s (%d)\n", options.pnn, get_debug_by_level(level), level);
4622 return 0;
4626 display reclock file of a node
4628 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4630 int ret;
4631 const char *reclock;
4633 ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
4634 if (ret != 0) {
4635 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4636 return ret;
4637 } else {
4638 if (options.machinereadable){
4639 if (reclock != NULL) {
4640 printf("%s", reclock);
4642 } else {
4643 if (reclock == NULL) {
4644 printf("No reclock file used.\n");
4645 } else {
4646 printf("Reclock file:%s\n", reclock);
4650 return 0;
4654 set the reclock file of a node
4656 static int control_setreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4658 int ret;
4659 const char *reclock;
4661 if (argc == 0) {
4662 reclock = NULL;
4663 } else if (argc == 1) {
4664 reclock = argv[0];
4665 } else {
4666 usage();
4669 ret = ctdb_ctrl_setreclock(ctdb, TIMELIMIT(), options.pnn, reclock);
4670 if (ret != 0) {
4671 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4672 return ret;
4674 return 0;
4678 set the natgw state on/off
4680 static int control_setnatgwstate(struct ctdb_context *ctdb, int argc, const char **argv)
4682 int ret;
4683 uint32_t natgwstate;
4685 if (argc == 0) {
4686 usage();
4689 if (!strcmp(argv[0], "on")) {
4690 natgwstate = 1;
4691 } else if (!strcmp(argv[0], "off")) {
4692 natgwstate = 0;
4693 } else {
4694 usage();
4697 ret = ctdb_ctrl_setnatgwstate(ctdb, TIMELIMIT(), options.pnn, natgwstate);
4698 if (ret != 0) {
4699 DEBUG(DEBUG_ERR, ("Unable to set the natgw state for node %u\n", options.pnn));
4700 return ret;
4703 return 0;
4707 set the lmaster role on/off
4709 static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4711 int ret;
4712 uint32_t lmasterrole;
4714 if (argc == 0) {
4715 usage();
4718 if (!strcmp(argv[0], "on")) {
4719 lmasterrole = 1;
4720 } else if (!strcmp(argv[0], "off")) {
4721 lmasterrole = 0;
4722 } else {
4723 usage();
4726 ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
4727 if (ret != 0) {
4728 DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
4729 return ret;
4732 return 0;
4736 set the recmaster role on/off
4738 static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4740 int ret;
4741 uint32_t recmasterrole;
4743 if (argc == 0) {
4744 usage();
4747 if (!strcmp(argv[0], "on")) {
4748 recmasterrole = 1;
4749 } else if (!strcmp(argv[0], "off")) {
4750 recmasterrole = 0;
4751 } else {
4752 usage();
4755 ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
4756 if (ret != 0) {
4757 DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
4758 return ret;
4761 return 0;
4765 set debug level on a node or all nodes
4767 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4769 int i, ret;
4770 int32_t level;
4772 if (argc == 0) {
4773 printf("You must specify the debug level. Valid levels are:\n");
4774 for (i=0; debug_levels[i].description != NULL; i++) {
4775 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
4778 return 0;
4781 if (isalpha(argv[0][0]) || argv[0][0] == '-') {
4782 level = get_debug_by_desc(argv[0]);
4783 } else {
4784 level = strtol(argv[0], NULL, 0);
4787 for (i=0; debug_levels[i].description != NULL; i++) {
4788 if (level == debug_levels[i].level) {
4789 break;
4792 if (debug_levels[i].description == NULL) {
4793 printf("Invalid debug level, must be one of\n");
4794 for (i=0; debug_levels[i].description != NULL; i++) {
4795 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
4797 return -1;
4800 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
4801 if (ret != 0) {
4802 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
4804 return 0;
4809 thaw a node
4811 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
4813 int ret;
4814 uint32_t priority;
4816 if (argc == 1) {
4817 priority = strtol(argv[0], NULL, 0);
4818 } else {
4819 priority = 0;
4821 DEBUG(DEBUG_ERR,("Thaw by priority %u\n", priority));
4823 ret = ctdb_ctrl_thaw_priority(ctdb, TIMELIMIT(), options.pnn, priority);
4824 if (ret != 0) {
4825 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
4827 return 0;
4832 attach to a database
4834 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
4836 const char *db_name;
4837 struct ctdb_db_context *ctdb_db;
4838 bool persistent = false;
4840 if (argc < 1) {
4841 usage();
4843 db_name = argv[0];
4844 if (argc > 2) {
4845 usage();
4847 if (argc == 2) {
4848 if (strcmp(argv[1], "persistent") != 0) {
4849 usage();
4851 persistent = true;
4854 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4855 if (ctdb_db == NULL) {
4856 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4857 return -1;
4860 return 0;
4864 set db priority
4866 static int control_setdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
4868 struct ctdb_db_priority db_prio;
4869 int ret;
4871 if (argc < 2) {
4872 usage();
4875 db_prio.db_id = strtoul(argv[0], NULL, 0);
4876 db_prio.priority = strtoul(argv[1], NULL, 0);
4878 ret = ctdb_ctrl_set_db_priority(ctdb, TIMELIMIT(), options.pnn, &db_prio);
4879 if (ret != 0) {
4880 DEBUG(DEBUG_ERR,("Unable to set db prio\n"));
4881 return -1;
4884 return 0;
4888 get db priority
4890 static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
4892 uint32_t db_id, priority;
4893 int ret;
4895 if (argc < 1) {
4896 usage();
4899 if (!db_exists(ctdb, argv[0], &db_id, NULL)) {
4900 return -1;
4903 ret = ctdb_ctrl_get_db_priority(ctdb, TIMELIMIT(), options.pnn, db_id, &priority);
4904 if (ret != 0) {
4905 DEBUG(DEBUG_ERR,("Unable to get db prio\n"));
4906 return -1;
4909 DEBUG(DEBUG_ERR,("Priority:%u\n", priority));
4911 return 0;
4915 set the sticky records capability for a database
4917 static int control_setdbsticky(struct ctdb_context *ctdb, int argc, const char **argv)
4919 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4920 uint32_t db_id;
4921 int ret;
4923 if (argc < 1) {
4924 usage();
4927 if (!db_exists(ctdb, argv[0], &db_id, NULL)) {
4928 return -1;
4931 ret = ctdb_ctrl_set_db_sticky(ctdb, options.pnn, db_id);
4932 if (ret != 0) {
4933 DEBUG(DEBUG_ERR,("Unable to set db to support sticky records\n"));
4934 talloc_free(tmp_ctx);
4935 return -1;
4938 talloc_free(tmp_ctx);
4939 return 0;
4943 set the readonly capability for a database
4945 static int control_setdbreadonly(struct ctdb_context *ctdb, int argc, const char **argv)
4947 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4948 uint32_t db_id;
4949 int ret;
4951 if (argc < 1) {
4952 usage();
4955 if (!db_exists(ctdb, argv[0], &db_id, NULL)) {
4956 return -1;
4959 ret = ctdb_ctrl_set_db_readonly(ctdb, options.pnn, db_id);
4960 if (ret != 0) {
4961 DEBUG(DEBUG_ERR,("Unable to set db to support readonly\n"));
4962 talloc_free(tmp_ctx);
4963 return -1;
4966 talloc_free(tmp_ctx);
4967 return 0;
4971 get db seqnum
4973 static int control_getdbseqnum(struct ctdb_context *ctdb, int argc, const char **argv)
4975 bool ret;
4976 uint32_t db_id;
4977 uint64_t seqnum;
4979 if (argc < 1) {
4980 usage();
4983 if (!db_exists(ctdb, argv[0], &db_id, NULL)) {
4984 return -1;
4987 ret = ctdb_getdbseqnum(ctdb_connection, options.pnn, db_id, &seqnum);
4988 if (!ret) {
4989 DEBUG(DEBUG_ERR, ("Unable to get seqnum from node."));
4990 return -1;
4993 printf("Sequence number:%lld\n", (long long)seqnum);
4995 return 0;
4999 * set db seqnum
5001 static int control_setdbseqnum(struct ctdb_context *ctdb, int argc, const char **argv)
5003 bool ret;
5004 struct ctdb_db_context *ctdb_db;
5005 uint32_t db_id;
5006 uint8_t flags;
5007 uint64_t old_seqnum, new_seqnum;
5008 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5009 struct ctdb_transaction_handle *h;
5010 TDB_DATA key, data;
5011 bool persistent;
5013 if (argc != 2) {
5014 talloc_free(tmp_ctx);
5015 usage();
5018 if (!db_exists(ctdb, argv[0], &db_id, &flags)) {
5019 talloc_free(tmp_ctx);
5020 return -1;
5023 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
5024 if (!persistent) {
5025 DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", argv[0]));
5026 talloc_free(tmp_ctx);
5027 return -1;
5030 ret = ctdb_getdbseqnum(ctdb_connection, options.pnn, db_id, &old_seqnum);
5031 if (!ret) {
5032 DEBUG(DEBUG_ERR, ("Unable to get seqnum from node."));
5033 talloc_free(tmp_ctx);
5034 return -1;
5037 new_seqnum = strtoull(argv[1], NULL, 0);
5038 if (new_seqnum <= old_seqnum) {
5039 DEBUG(DEBUG_ERR, ("New sequence number is less than current sequence number\n"));
5040 talloc_free(tmp_ctx);
5041 return -1;
5044 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), argv[0], persistent, 0);
5045 if (ctdb_db == NULL) {
5046 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
5047 talloc_free(tmp_ctx);
5048 return -1;
5051 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
5052 if (h == NULL) {
5053 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", argv[0]));
5054 talloc_free(tmp_ctx);
5055 return -1;
5058 key.dptr = (uint8_t *)discard_const(CTDB_DB_SEQNUM_KEY);
5059 key.dsize = strlen(CTDB_DB_SEQNUM_KEY) + 1;
5061 data.dsize = sizeof(new_seqnum);
5062 data.dptr = talloc_size(tmp_ctx, data.dsize);
5063 *data.dptr = new_seqnum;
5065 ret = ctdb_transaction_store(h, key, data);
5066 if (ret != 0) {
5067 DEBUG(DEBUG_ERR,("Failed to store record\n"));
5068 talloc_free(tmp_ctx);
5069 return -1;
5072 ret = ctdb_transaction_commit(h);
5073 if (ret != 0) {
5074 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
5075 talloc_free(tmp_ctx);
5076 return -1;
5079 talloc_free(tmp_ctx);
5080 return 0;
5084 run an eventscript on a node
5086 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
5088 TDB_DATA data;
5089 int ret;
5090 int32_t res;
5091 char *errmsg;
5092 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5094 if (argc != 1) {
5095 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5096 return -1;
5099 data.dptr = (unsigned char *)discard_const(argv[0]);
5100 data.dsize = strlen((char *)data.dptr) + 1;
5102 DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
5104 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
5105 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
5106 if (ret != 0 || res != 0) {
5107 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
5108 talloc_free(tmp_ctx);
5109 return -1;
5111 talloc_free(tmp_ctx);
5112 return 0;
5115 #define DB_VERSION 1
5116 #define MAX_DB_NAME 64
5117 struct db_file_header {
5118 unsigned long version;
5119 time_t timestamp;
5120 unsigned long persistent;
5121 unsigned long size;
5122 const char name[MAX_DB_NAME];
5125 struct backup_data {
5126 struct ctdb_marshall_buffer *records;
5127 uint32_t len;
5128 uint32_t total;
5129 bool traverse_error;
5132 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
5134 struct backup_data *bd = talloc_get_type(private, struct backup_data);
5135 struct ctdb_rec_data *rec;
5137 /* add the record */
5138 rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
5139 if (rec == NULL) {
5140 bd->traverse_error = true;
5141 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
5142 return -1;
5144 bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
5145 if (bd->records == NULL) {
5146 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
5147 bd->traverse_error = true;
5148 return -1;
5150 bd->records->count++;
5151 memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
5152 bd->len += rec->length;
5153 talloc_free(rec);
5155 bd->total++;
5156 return 0;
5160 * backup a database to a file
5162 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
5164 int ret;
5165 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5166 struct db_file_header dbhdr;
5167 struct ctdb_db_context *ctdb_db;
5168 struct backup_data *bd;
5169 int fh = -1;
5170 int status = -1;
5171 const char *reason = NULL;
5172 uint32_t db_id;
5173 uint8_t flags;
5175 if (argc != 2) {
5176 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5177 return -1;
5180 if (!db_exists(ctdb, argv[0], &db_id, &flags)) {
5181 return -1;
5184 ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
5185 db_id, tmp_ctx, &reason);
5186 if (ret != 0) {
5187 DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
5188 argv[0]));
5189 talloc_free(tmp_ctx);
5190 return -1;
5192 if (reason) {
5193 uint32_t allow_unhealthy = 0;
5195 ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
5196 "AllowUnhealthyDBRead",
5197 &allow_unhealthy);
5199 if (allow_unhealthy != 1) {
5200 DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
5201 argv[0], reason));
5203 DEBUG(DEBUG_ERR,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
5204 allow_unhealthy));
5205 talloc_free(tmp_ctx);
5206 return -1;
5209 DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
5210 argv[0], argv[0]));
5211 DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
5212 "tunnable AllowUnhealthyDBRead = %u\n",
5213 allow_unhealthy));
5216 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), argv[0], flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5217 if (ctdb_db == NULL) {
5218 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
5219 talloc_free(tmp_ctx);
5220 return -1;
5224 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
5225 if (ret == -1) {
5226 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
5227 talloc_free(tmp_ctx);
5228 return -1;
5232 bd = talloc_zero(tmp_ctx, struct backup_data);
5233 if (bd == NULL) {
5234 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
5235 talloc_free(tmp_ctx);
5236 return -1;
5239 bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
5240 if (bd->records == NULL) {
5241 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
5242 talloc_free(tmp_ctx);
5243 return -1;
5246 bd->len = offsetof(struct ctdb_marshall_buffer, data);
5247 bd->records->db_id = ctdb_db->db_id;
5248 /* traverse the database collecting all records */
5249 if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
5250 bd->traverse_error) {
5251 DEBUG(DEBUG_ERR,("Traverse error\n"));
5252 talloc_free(tmp_ctx);
5253 return -1;
5256 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
5259 fh = open(argv[1], O_RDWR|O_CREAT, 0600);
5260 if (fh == -1) {
5261 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
5262 talloc_free(tmp_ctx);
5263 return -1;
5266 dbhdr.version = DB_VERSION;
5267 dbhdr.timestamp = time(NULL);
5268 dbhdr.persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
5269 dbhdr.size = bd->len;
5270 if (strlen(argv[0]) >= MAX_DB_NAME) {
5271 DEBUG(DEBUG_ERR,("Too long dbname\n"));
5272 goto done;
5274 strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME);
5275 ret = write(fh, &dbhdr, sizeof(dbhdr));
5276 if (ret == -1) {
5277 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5278 goto done;
5280 ret = write(fh, bd->records, bd->len);
5281 if (ret == -1) {
5282 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5283 goto done;
5286 status = 0;
5287 done:
5288 if (fh != -1) {
5289 ret = close(fh);
5290 if (ret == -1) {
5291 DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
5295 DEBUG(DEBUG_ERR,("Database backed up to %s\n", argv[1]));
5297 talloc_free(tmp_ctx);
5298 return status;
5302 * restore a database from a file
5304 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
5306 int ret;
5307 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5308 TDB_DATA outdata;
5309 TDB_DATA data;
5310 struct db_file_header dbhdr;
5311 struct ctdb_db_context *ctdb_db;
5312 struct ctdb_node_map *nodemap=NULL;
5313 struct ctdb_vnn_map *vnnmap=NULL;
5314 int i, fh;
5315 struct ctdb_control_wipe_database w;
5316 uint32_t *nodes;
5317 uint32_t generation;
5318 struct tm *tm;
5319 char tbuf[100];
5320 char *dbname;
5322 if (argc < 1 || argc > 2) {
5323 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5324 return -1;
5327 fh = open(argv[0], O_RDONLY);
5328 if (fh == -1) {
5329 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5330 talloc_free(tmp_ctx);
5331 return -1;
5334 read(fh, &dbhdr, sizeof(dbhdr));
5335 if (dbhdr.version != DB_VERSION) {
5336 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5337 talloc_free(tmp_ctx);
5338 return -1;
5341 dbname = discard_const(dbhdr.name);
5342 if (argc == 2) {
5343 dbname = discard_const(argv[1]);
5346 outdata.dsize = dbhdr.size;
5347 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5348 if (outdata.dptr == NULL) {
5349 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5350 close(fh);
5351 talloc_free(tmp_ctx);
5352 return -1;
5354 read(fh, outdata.dptr, outdata.dsize);
5355 close(fh);
5357 tm = localtime(&dbhdr.timestamp);
5358 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5359 printf("Restoring database '%s' from backup @ %s\n",
5360 dbname, tbuf);
5363 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), dbname, dbhdr.persistent, 0);
5364 if (ctdb_db == NULL) {
5365 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbname));
5366 talloc_free(tmp_ctx);
5367 return -1;
5370 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
5371 if (ret != 0) {
5372 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
5373 talloc_free(tmp_ctx);
5374 return ret;
5378 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
5379 if (ret != 0) {
5380 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
5381 talloc_free(tmp_ctx);
5382 return ret;
5385 /* freeze all nodes */
5386 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5387 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5388 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5389 nodes, i,
5390 TIMELIMIT(),
5391 false, tdb_null,
5392 NULL, NULL,
5393 NULL) != 0) {
5394 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5395 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5396 talloc_free(tmp_ctx);
5397 return -1;
5401 generation = vnnmap->generation;
5402 data.dptr = (void *)&generation;
5403 data.dsize = sizeof(generation);
5405 /* start a cluster wide transaction */
5406 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5407 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5408 nodes, 0,
5409 TIMELIMIT(), false, data,
5410 NULL, NULL,
5411 NULL) != 0) {
5412 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
5413 return -1;
5417 w.db_id = ctdb_db->db_id;
5418 w.transaction_id = generation;
5420 data.dptr = (void *)&w;
5421 data.dsize = sizeof(w);
5423 /* wipe all the remote databases. */
5424 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5425 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5426 nodes, 0,
5427 TIMELIMIT(), false, data,
5428 NULL, NULL,
5429 NULL) != 0) {
5430 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5431 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5432 talloc_free(tmp_ctx);
5433 return -1;
5436 /* push the database */
5437 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5438 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
5439 nodes, 0,
5440 TIMELIMIT(), false, outdata,
5441 NULL, NULL,
5442 NULL) != 0) {
5443 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
5444 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5445 talloc_free(tmp_ctx);
5446 return -1;
5449 data.dptr = (void *)&ctdb_db->db_id;
5450 data.dsize = sizeof(ctdb_db->db_id);
5452 /* mark the database as healthy */
5453 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5454 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5455 nodes, 0,
5456 TIMELIMIT(), false, data,
5457 NULL, NULL,
5458 NULL) != 0) {
5459 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5460 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5461 talloc_free(tmp_ctx);
5462 return -1;
5465 data.dptr = (void *)&generation;
5466 data.dsize = sizeof(generation);
5468 /* commit all the changes */
5469 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5470 nodes, 0,
5471 TIMELIMIT(), false, data,
5472 NULL, NULL,
5473 NULL) != 0) {
5474 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5475 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5476 talloc_free(tmp_ctx);
5477 return -1;
5481 /* thaw all nodes */
5482 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5483 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5484 nodes, 0,
5485 TIMELIMIT(),
5486 false, tdb_null,
5487 NULL, NULL,
5488 NULL) != 0) {
5489 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5490 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5491 talloc_free(tmp_ctx);
5492 return -1;
5496 talloc_free(tmp_ctx);
5497 return 0;
5501 * dump a database backup from a file
5503 static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
5505 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5506 TDB_DATA outdata;
5507 struct db_file_header dbhdr;
5508 int i, fh;
5509 struct tm *tm;
5510 char tbuf[100];
5511 struct ctdb_rec_data *rec = NULL;
5512 struct ctdb_marshall_buffer *m;
5513 struct ctdb_dump_db_context c;
5515 if (argc != 1) {
5516 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5517 return -1;
5520 fh = open(argv[0], O_RDONLY);
5521 if (fh == -1) {
5522 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5523 talloc_free(tmp_ctx);
5524 return -1;
5527 read(fh, &dbhdr, sizeof(dbhdr));
5528 if (dbhdr.version != DB_VERSION) {
5529 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5530 talloc_free(tmp_ctx);
5531 return -1;
5534 outdata.dsize = dbhdr.size;
5535 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5536 if (outdata.dptr == NULL) {
5537 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5538 close(fh);
5539 talloc_free(tmp_ctx);
5540 return -1;
5542 read(fh, outdata.dptr, outdata.dsize);
5543 close(fh);
5544 m = (struct ctdb_marshall_buffer *)outdata.dptr;
5546 tm = localtime(&dbhdr.timestamp);
5547 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5548 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
5549 dbhdr.name, m->db_id, tbuf);
5551 ZERO_STRUCT(c);
5552 c.f = stdout;
5553 c.printemptyrecords = (bool)options.printemptyrecords;
5554 c.printdatasize = (bool)options.printdatasize;
5555 c.printlmaster = false;
5556 c.printhash = (bool)options.printhash;
5557 c.printrecordflags = (bool)options.printrecordflags;
5559 for (i=0; i < m->count; i++) {
5560 uint32_t reqid = 0;
5561 TDB_DATA key, data;
5563 /* we do not want the header splitted, so we pass NULL*/
5564 rec = ctdb_marshall_loop_next(m, rec, &reqid,
5565 NULL, &key, &data);
5567 ctdb_dumpdb_record(ctdb, key, data, &c);
5570 printf("Dumped %d records\n", i);
5571 talloc_free(tmp_ctx);
5572 return 0;
5576 * wipe a database from a file
5578 static int control_wipedb(struct ctdb_context *ctdb, int argc,
5579 const char **argv)
5581 int ret;
5582 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5583 TDB_DATA data;
5584 struct ctdb_db_context *ctdb_db;
5585 struct ctdb_node_map *nodemap = NULL;
5586 struct ctdb_vnn_map *vnnmap = NULL;
5587 int i;
5588 struct ctdb_control_wipe_database w;
5589 uint32_t *nodes;
5590 uint32_t generation;
5591 uint8_t flags;
5593 if (argc != 1) {
5594 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5595 return -1;
5598 if (!db_exists(ctdb, argv[0], NULL, &flags)) {
5599 return -1;
5602 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), argv[0], flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5603 if (ctdb_db == NULL) {
5604 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
5605 argv[0]));
5606 talloc_free(tmp_ctx);
5607 return -1;
5610 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
5611 &nodemap);
5612 if (ret != 0) {
5613 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5614 options.pnn));
5615 talloc_free(tmp_ctx);
5616 return ret;
5619 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5620 &vnnmap);
5621 if (ret != 0) {
5622 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
5623 options.pnn));
5624 talloc_free(tmp_ctx);
5625 return ret;
5628 /* freeze all nodes */
5629 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5630 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5631 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5632 nodes, i,
5633 TIMELIMIT(),
5634 false, tdb_null,
5635 NULL, NULL,
5636 NULL);
5637 if (ret != 0) {
5638 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5639 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
5640 CTDB_RECOVERY_ACTIVE);
5641 talloc_free(tmp_ctx);
5642 return -1;
5646 generation = vnnmap->generation;
5647 data.dptr = (void *)&generation;
5648 data.dsize = sizeof(generation);
5650 /* start a cluster wide transaction */
5651 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5652 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5653 nodes, 0,
5654 TIMELIMIT(), false, data,
5655 NULL, NULL,
5656 NULL);
5657 if (ret!= 0) {
5658 DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
5659 "transactions.\n"));
5660 return -1;
5663 w.db_id = ctdb_db->db_id;
5664 w.transaction_id = generation;
5666 data.dptr = (void *)&w;
5667 data.dsize = sizeof(w);
5669 /* wipe all the remote databases. */
5670 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5671 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5672 nodes, 0,
5673 TIMELIMIT(), false, data,
5674 NULL, NULL,
5675 NULL) != 0) {
5676 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5677 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5678 talloc_free(tmp_ctx);
5679 return -1;
5682 data.dptr = (void *)&ctdb_db->db_id;
5683 data.dsize = sizeof(ctdb_db->db_id);
5685 /* mark the database as healthy */
5686 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5687 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5688 nodes, 0,
5689 TIMELIMIT(), false, data,
5690 NULL, NULL,
5691 NULL) != 0) {
5692 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5693 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5694 talloc_free(tmp_ctx);
5695 return -1;
5698 data.dptr = (void *)&generation;
5699 data.dsize = sizeof(generation);
5701 /* commit all the changes */
5702 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5703 nodes, 0,
5704 TIMELIMIT(), false, data,
5705 NULL, NULL,
5706 NULL) != 0) {
5707 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5708 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5709 talloc_free(tmp_ctx);
5710 return -1;
5713 /* thaw all nodes */
5714 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5715 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5716 nodes, 0,
5717 TIMELIMIT(),
5718 false, tdb_null,
5719 NULL, NULL,
5720 NULL) != 0) {
5721 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5722 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5723 talloc_free(tmp_ctx);
5724 return -1;
5727 DEBUG(DEBUG_ERR, ("Database wiped.\n"));
5729 talloc_free(tmp_ctx);
5730 return 0;
5734 dump memory usage
5736 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
5738 TDB_DATA data;
5739 int ret;
5740 int32_t res;
5741 char *errmsg;
5742 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5743 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
5744 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
5745 if (ret != 0 || res != 0) {
5746 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
5747 talloc_free(tmp_ctx);
5748 return -1;
5750 write(1, data.dptr, data.dsize);
5751 talloc_free(tmp_ctx);
5752 return 0;
5756 handler for memory dumps
5758 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid,
5759 TDB_DATA data, void *private_data)
5761 write(1, data.dptr, data.dsize);
5762 exit(0);
5766 dump memory usage on the recovery daemon
5768 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
5770 int ret;
5771 TDB_DATA data;
5772 struct rd_memdump_reply rd;
5774 rd.pnn = ctdb_get_pnn(ctdb);
5775 rd.srvid = getpid();
5777 /* register a message port for receiveing the reply so that we
5778 can receive the reply
5780 ctdb_client_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
5783 data.dptr = (uint8_t *)&rd;
5784 data.dsize = sizeof(rd);
5786 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
5787 if (ret != 0) {
5788 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
5789 return -1;
5792 /* this loop will terminate when we have received the reply */
5793 while (1) {
5794 event_loop_once(ctdb->ev);
5797 return 0;
5801 send a message to a srvid
5803 static int control_msgsend(struct ctdb_context *ctdb, int argc, const char **argv)
5805 unsigned long srvid;
5806 int ret;
5807 TDB_DATA data;
5809 if (argc < 2) {
5810 usage();
5813 srvid = strtoul(argv[0], NULL, 0);
5815 data.dptr = (uint8_t *)discard_const(argv[1]);
5816 data.dsize= strlen(argv[1]);
5818 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, srvid, data);
5819 if (ret != 0) {
5820 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
5821 return -1;
5824 return 0;
5828 handler for msglisten
5830 static void msglisten_handler(struct ctdb_context *ctdb, uint64_t srvid,
5831 TDB_DATA data, void *private_data)
5833 int i;
5835 printf("Message received: ");
5836 for (i=0;i<data.dsize;i++) {
5837 printf("%c", data.dptr[i]);
5839 printf("\n");
5843 listen for messages on a messageport
5845 static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
5847 uint64_t srvid;
5849 srvid = getpid();
5851 /* register a message port and listen for messages
5853 ctdb_client_set_message_handler(ctdb, srvid, msglisten_handler, NULL);
5854 printf("Listening for messages on srvid:%d\n", (int)srvid);
5856 while (1) {
5857 event_loop_once(ctdb->ev);
5860 return 0;
5864 list all nodes in the cluster
5865 we parse the nodes file directly
5867 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
5869 TALLOC_CTX *mem_ctx = talloc_new(NULL);
5870 struct pnn_node *pnn_nodes;
5871 struct pnn_node *pnn_node;
5873 pnn_nodes = read_nodes_file(mem_ctx);
5874 if (pnn_nodes == NULL) {
5875 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
5876 talloc_free(mem_ctx);
5877 return -1;
5880 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
5881 ctdb_sock_addr addr;
5882 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
5883 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
5884 talloc_free(mem_ctx);
5885 return -1;
5887 if (options.machinereadable){
5888 printf(":%d:%s:\n", pnn_node->pnn, pnn_node->addr);
5889 } else {
5890 printf("%s\n", pnn_node->addr);
5893 talloc_free(mem_ctx);
5895 return 0;
5899 reload the nodes file on the local node
5901 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
5903 int i, ret;
5904 int mypnn;
5905 struct ctdb_node_map *nodemap=NULL;
5907 mypnn = ctdb_get_pnn(ctdb);
5909 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
5910 if (ret != 0) {
5911 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
5912 return ret;
5915 /* reload the nodes file on all remote nodes */
5916 for (i=0;i<nodemap->num;i++) {
5917 if (nodemap->nodes[i].pnn == mypnn) {
5918 continue;
5920 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
5921 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
5922 nodemap->nodes[i].pnn);
5923 if (ret != 0) {
5924 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
5928 /* reload the nodes file on the local node */
5929 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
5930 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
5931 if (ret != 0) {
5932 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
5935 /* initiate a recovery */
5936 control_recover(ctdb, argc, argv);
5938 return 0;
5942 static const struct {
5943 const char *name;
5944 int (*fn)(struct ctdb_context *, int, const char **);
5945 bool auto_all;
5946 bool without_daemon; /* can be run without daemon running ? */
5947 const char *msg;
5948 const char *args;
5949 } ctdb_commands[] = {
5950 { "version", control_version, true, true, "show version of ctdb" },
5951 { "status", control_status, true, false, "show node status" },
5952 { "uptime", control_uptime, true, false, "show node uptime" },
5953 { "ping", control_ping, true, false, "ping all nodes" },
5954 { "runstate", control_runstate, true, false, "get/check runstate of a node", "[setup|first_recovery|startup|running]" },
5955 { "getvar", control_getvar, true, false, "get a tunable variable", "<name>"},
5956 { "setvar", control_setvar, true, false, "set a tunable variable", "<name> <value>"},
5957 { "listvars", control_listvars, true, false, "list tunable variables"},
5958 { "statistics", control_statistics, false, false, "show statistics" },
5959 { "statisticsreset", control_statistics_reset, true, false, "reset statistics"},
5960 { "stats", control_stats, false, false, "show rolling statistics", "[number of history records]" },
5961 { "ip", control_ip, false, false, "show which public ip's that ctdb manages" },
5962 { "ipinfo", control_ipinfo, true, false, "show details about a public ip that ctdb manages", "<ip>" },
5963 { "ifaces", control_ifaces, true, false, "show which interfaces that ctdb manages" },
5964 { "setifacelink", control_setifacelink, true, false, "set interface link status", "<iface> <status>" },
5965 { "process-exists", control_process_exists, true, false, "check if a process exists on a node", "<pid>"},
5966 { "getdbmap", control_getdbmap, true, false, "show the database map" },
5967 { "getdbstatus", control_getdbstatus, true, false, "show the status of a database", "<dbname|dbid>" },
5968 { "catdb", control_catdb, true, false, "dump a ctdb database" , "<dbname|dbid>"},
5969 { "cattdb", control_cattdb, true, false, "dump a local tdb database" , "<dbname|dbid>"},
5970 { "getmonmode", control_getmonmode, true, false, "show monitoring mode" },
5971 { "getcapabilities", control_getcapabilities, true, false, "show node capabilities" },
5972 { "pnn", control_pnn, true, false, "show the pnn of the currnet node" },
5973 { "lvs", control_lvs, true, false, "show lvs configuration" },
5974 { "lvsmaster", control_lvsmaster, true, false, "show which node is the lvs master" },
5975 { "disablemonitor", control_disable_monmode,true, false, "set monitoring mode to DISABLE" },
5976 { "enablemonitor", control_enable_monmode, true, false, "set monitoring mode to ACTIVE" },
5977 { "setdebug", control_setdebug, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
5978 { "getdebug", control_getdebug, true, false, "get debug level" },
5979 { "getlog", control_getlog, true, false, "get the log data from the in memory ringbuffer", "[<level>] [recoverd]" },
5980 { "clearlog", control_clearlog, true, false, "clear the log data from the in memory ringbuffer", "[recoverd]" },
5981 { "attach", control_attach, true, false, "attach to a database", "<dbname> [persistent]" },
5982 { "dumpmemory", control_dumpmemory, true, false, "dump memory map to stdout" },
5983 { "rddumpmemory", control_rddumpmemory, true, false, "dump memory map from the recovery daemon to stdout" },
5984 { "getpid", control_getpid, true, false, "get ctdbd process ID" },
5985 { "disable", control_disable, true, false, "disable a nodes public IP" },
5986 { "enable", control_enable, true, false, "enable a nodes public IP" },
5987 { "stop", control_stop, true, false, "stop a node" },
5988 { "continue", control_continue, true, false, "re-start a stopped node" },
5989 { "ban", control_ban, true, false, "ban a node from the cluster", "<bantime|0>"},
5990 { "unban", control_unban, true, false, "unban a node" },
5991 { "showban", control_showban, true, false, "show ban information"},
5992 { "shutdown", control_shutdown, true, false, "shutdown ctdbd" },
5993 { "recover", control_recover, true, false, "force recovery" },
5994 { "sync", control_ipreallocate, false, false, "wait until ctdbd has synced all state changes" },
5995 { "ipreallocate", control_ipreallocate, true, false, "force the recovery daemon to perform a ip reallocation procedure" },
5996 { "thaw", control_thaw, true, false, "thaw databases", "[priority:1-3]" },
5997 { "isnotrecmaster", control_isnotrecmaster, false, false, "check if the local node is recmaster or not" },
5998 { "killtcp", kill_tcp, false, false, "kill a tcp connection.", "[<srcip:port> <dstip:port>]" },
5999 { "gratiousarp", control_gratious_arp, false, false, "send a gratious arp", "<ip> <interface>" },
6000 { "tickle", tickle_tcp, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
6001 { "gettickles", control_get_tickles, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
6002 { "addtickle", control_add_tickle, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
6004 { "deltickle", control_del_tickle, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
6006 { "regsrvid", regsrvid, false, false, "register a server id", "<pnn> <type> <id>" },
6007 { "unregsrvid", unregsrvid, false, false, "unregister a server id", "<pnn> <type> <id>" },
6008 { "chksrvid", chksrvid, false, false, "check if a server id exists", "<pnn> <type> <id>" },
6009 { "getsrvids", getsrvids, false, false, "get a list of all server ids"},
6010 { "check_srvids", check_srvids, false, false, "check if a srvid exists", "<id>+" },
6011 { "vacuum", ctdb_vacuum, false, true, "vacuum the databases of empty records", "[max_records]"},
6012 { "repack", ctdb_repack, false, false, "repack all databases", "[max_freelist]"},
6013 { "listnodes", control_listnodes, false, true, "list all nodes in the cluster"},
6014 { "reloadnodes", control_reload_nodes_file, false, false, "reload the nodes file and restart the transport on all nodes"},
6015 { "moveip", control_moveip, false, false, "move/failover an ip address to another node", "<ip> <node>"},
6016 { "rebalanceip", control_rebalanceip, false, false, "release an ip from the node and let recd rebalance it", "<ip>"},
6017 { "addip", control_addip, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
6018 { "delip", control_delip, false, false, "delete an ip address from a node", "<ip>"},
6019 { "eventscript", control_eventscript, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
6020 { "backupdb", control_backupdb, false, false, "backup the database into a file.", "<dbname|dbid> <file>"},
6021 { "restoredb", control_restoredb, false, false, "restore the database from a file.", "<file> [dbname]"},
6022 { "dumpdbbackup", control_dumpdbbackup, false, true, "dump database backup from a file.", "<file>"},
6023 { "wipedb", control_wipedb, false, false, "wipe the contents of a database.", "<dbname|dbid>"},
6024 { "recmaster", control_recmaster, true, false, "show the pnn for the recovery master."},
6025 { "scriptstatus", control_scriptstatus, true, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
6026 { "enablescript", control_enablescript, false, false, "enable an eventscript", "<script>"},
6027 { "disablescript", control_disablescript, false, false, "disable an eventscript", "<script>"},
6028 { "natgwlist", control_natgwlist, false, false, "show the nodes belonging to this natgw configuration"},
6029 { "xpnn", control_xpnn, true, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
6030 { "getreclock", control_getreclock, false, false, "Show the reclock file of a node"},
6031 { "setreclock", control_setreclock, false, false, "Set/clear the reclock file of a node", "[filename]"},
6032 { "setnatgwstate", control_setnatgwstate, false, false, "Set NATGW state to on/off", "{on|off}"},
6033 { "setlmasterrole", control_setlmasterrole, false, false, "Set LMASTER role to on/off", "{on|off}"},
6034 { "setrecmasterrole", control_setrecmasterrole, false, false, "Set RECMASTER role to on/off", "{on|off}"},
6035 { "setdbprio", control_setdbprio, false, false, "Set DB priority", "<dbname|dbid> <prio:1-3>"},
6036 { "getdbprio", control_getdbprio, false, false, "Get DB priority", "<dbname|dbid>"},
6037 { "setdbreadonly", control_setdbreadonly, false, false, "Set DB readonly capable", "<dbname|dbid>"},
6038 { "setdbsticky", control_setdbsticky, false, false, "Set DB sticky-records capable", "<dbname|dbid>"},
6039 { "msglisten", control_msglisten, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
6040 { "msgsend", control_msgsend, false, false, "Send a message to srvid", "<srvid> <message>"},
6041 { "pfetch", control_pfetch, false, false, "fetch a record from a persistent database", "<dbname|dbid> <key> [<file>]" },
6042 { "pstore", control_pstore, false, false, "write a record to a persistent database", "<dbname|dbid> <key> <file containing record>" },
6043 { "pdelete", control_pdelete, false, false, "delete a record from a persistent database", "<dbname|dbid> <key>" },
6044 { "tfetch", control_tfetch, false, true, "fetch a record from a [c]tdb-file [-v]", "<tdb-file> <key> [<file>]" },
6045 { "tstore", control_tstore, false, true, "store a record (including ltdb header)", "<tdb-file> <key> <data+header>" },
6046 { "readkey", control_readkey, true, false, "read the content off a database key", "<tdb-file> <key>" },
6047 { "writekey", control_writekey, true, false, "write to a database key", "<tdb-file> <key> <value>" },
6048 { "checktcpport", control_chktcpport, false, true, "check if a service is bound to a specific tcp port or not", "<port>" },
6049 { "rebalancenode", control_rebalancenode, false, false, "release a node by allowing it to takeover ips", "<pnn>"},
6050 { "getdbseqnum", control_getdbseqnum, false, false, "get the sequence number off a database", "<dbname|dbid>" },
6051 { "setdbseqnum", control_setdbseqnum, false, false, "set the sequence number for a database", "<dbname|dbid> <seqnum>" },
6052 { "nodestatus", control_nodestatus, true, false, "show and return node status" },
6053 { "dbstatistics", control_dbstatistics, false, false, "show db statistics", "<dbname|dbid>" },
6054 { "reloadips", control_reloadips, false, false, "reload the public addresses file on a node" },
6055 { "ipiface", control_ipiface, true, true, "Find which interface an ip address is hsoted on", "<ip>" },
6059 show usage message
6061 static void usage(void)
6063 int i;
6064 printf(
6065 "Usage: ctdb [options] <control>\n" \
6066 "Options:\n" \
6067 " -n <node> choose node number, or 'all' (defaults to local node)\n"
6068 " -Y generate machinereadable output\n"
6069 " -v generate verbose output\n"
6070 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
6071 printf("Controls:\n");
6072 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6073 printf(" %-15s %-27s %s\n",
6074 ctdb_commands[i].name,
6075 ctdb_commands[i].args?ctdb_commands[i].args:"",
6076 ctdb_commands[i].msg);
6078 exit(1);
6082 static void ctdb_alarm(int sig)
6084 printf("Maximum runtime exceeded - exiting\n");
6085 _exit(ERR_TIMEOUT);
6089 main program
6091 int main(int argc, const char *argv[])
6093 struct ctdb_context *ctdb;
6094 char *nodestring = NULL;
6095 struct poptOption popt_options[] = {
6096 POPT_AUTOHELP
6097 POPT_CTDB_CMDLINE
6098 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
6099 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
6100 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
6101 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
6102 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
6103 { "print-emptyrecords", 0, POPT_ARG_NONE, &options.printemptyrecords, 0, "print the empty records when dumping databases (catdb, cattdb, dumpdbbackup)", NULL },
6104 { "print-datasize", 0, POPT_ARG_NONE, &options.printdatasize, 0, "do not print record data when dumping databases, only the data size", NULL },
6105 { "print-lmaster", 0, POPT_ARG_NONE, &options.printlmaster, 0, "print the record's lmaster in catdb", NULL },
6106 { "print-hash", 0, POPT_ARG_NONE, &options.printhash, 0, "print the record's hash when dumping databases", NULL },
6107 { "print-recordflags", 0, POPT_ARG_NONE, &options.printrecordflags, 0, "print the record flags in catdb and dumpdbbackup", NULL },
6108 POPT_TABLEEND
6110 int opt;
6111 const char **extra_argv;
6112 int extra_argc = 0;
6113 int ret=-1, i;
6114 poptContext pc;
6115 struct event_context *ev;
6116 const char *control;
6117 const char *socket_name;
6119 setlinebuf(stdout);
6121 /* set some defaults */
6122 options.maxruntime = 0;
6123 options.timelimit = 10;
6124 options.pnn = CTDB_CURRENT_NODE;
6126 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
6128 while ((opt = poptGetNextOpt(pc)) != -1) {
6129 switch (opt) {
6130 default:
6131 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
6132 poptBadOption(pc, 0), poptStrerror(opt)));
6133 exit(1);
6137 /* setup the remaining options for the main program to use */
6138 extra_argv = poptGetArgs(pc);
6139 if (extra_argv) {
6140 extra_argv++;
6141 while (extra_argv[extra_argc]) extra_argc++;
6144 if (extra_argc < 1) {
6145 usage();
6148 if (options.maxruntime == 0) {
6149 const char *ctdb_timeout;
6150 ctdb_timeout = getenv("CTDB_TIMEOUT");
6151 if (ctdb_timeout != NULL) {
6152 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
6153 } else {
6154 /* default timeout is 120 seconds */
6155 options.maxruntime = 120;
6159 signal(SIGALRM, ctdb_alarm);
6160 alarm(options.maxruntime);
6162 control = extra_argv[0];
6164 ev = event_context_init(NULL);
6165 if (!ev) {
6166 DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
6167 exit(1);
6170 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6171 if (strcmp(control, ctdb_commands[i].name) == 0) {
6172 break;
6176 if (i == ARRAY_SIZE(ctdb_commands)) {
6177 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
6178 exit(1);
6181 if (ctdb_commands[i].without_daemon == true) {
6182 if (nodestring != NULL) {
6183 DEBUG(DEBUG_ERR, ("Can't specify node(s) with \"ctdb %s\"\n", control));
6184 exit(1);
6186 close(2);
6187 return ctdb_commands[i].fn(NULL, extra_argc-1, extra_argv+1);
6190 /* initialise ctdb */
6191 ctdb = ctdb_cmdline_client(ev, TIMELIMIT());
6193 if (ctdb == NULL) {
6194 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
6195 exit(1);
6198 /* initialize a libctdb connection as well */
6199 socket_name = ctdb_get_socketname(ctdb);
6200 ctdb_connection = ctdb_connect(socket_name,
6201 ctdb_log_file, stderr);
6202 if (ctdb_connection == NULL) {
6203 DEBUG(DEBUG_ERR, ("Failed to connect to daemon from libctdb\n"));
6204 exit(1);
6207 /* setup the node number(s) to contact */
6208 if (!parse_nodestring(ctdb, nodestring, CTDB_CURRENT_NODE, false,
6209 &options.nodes, &options.pnn)) {
6210 usage();
6213 if (options.pnn == CTDB_CURRENT_NODE) {
6214 options.pnn = options.nodes[0];
6217 if (ctdb_commands[i].auto_all &&
6218 ((options.pnn == CTDB_BROADCAST_ALL) ||
6219 (options.pnn == CTDB_MULTICAST))) {
6220 int j;
6222 ret = 0;
6223 for (j = 0; j < talloc_array_length(options.nodes); j++) {
6224 options.pnn = options.nodes[j];
6225 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6227 } else {
6228 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6231 ctdb_disconnect(ctdb_connection);
6232 talloc_free(ctdb);
6233 talloc_free(ev);
6234 (void)poptFreeContext(pc);
6236 return ret;