4 Copyright (C) Andrew Tridgell 2007
5 Copyright (C) Ronnie Sahlberg 2007
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "lib/tevent/tevent.h"
23 #include "system/time.h"
24 #include "system/filesys.h"
25 #include "system/network.h"
26 #include "system/locale.h"
29 #include "../include/ctdb.h"
30 #include "../include/ctdb_client.h"
31 #include "../include/ctdb_private.h"
32 #include "../common/rb_tree.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);
50 int printemptyrecords;
57 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
58 #define LONGTIMELIMIT() timeval_current_ofs(options.timelimit*10, 0)
61 static int control_version(struct ctdb_context *ctdb, int argc, const char **argv)
64 #define XSTR(x) STR(x)
65 printf("CTDB version: %s\n", XSTR(CTDB_VERS));
70 #define CTDB_NOMEM_ABORT(p) do { if (!(p)) { \
71 DEBUG(DEBUG_ALERT,("ctdb fatal error: %s\n", \
72 "Out of memory in " __location__ )); \
76 /* Pretty print the flags to a static buffer in human-readable format.
77 * This never returns NULL!
79 static const char *pretty_print_flags(uint32_t flags)
86 { NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
87 { NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
88 { NODE_FLAGS_BANNED, "BANNED" },
89 { NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
90 { NODE_FLAGS_DELETED, "DELETED" },
91 { NODE_FLAGS_STOPPED, "STOPPED" },
92 { NODE_FLAGS_INACTIVE, "INACTIVE" },
94 static char flags_str[512]; /* Big enough to contain all flag names */
97 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
98 if (flags & flag_names[j].flag) {
99 if (flags_str[0] == '\0') {
100 (void) strcpy(flags_str, flag_names[j].name);
102 (void) strcat(flags_str, "|");
103 (void) strcat(flags_str, flag_names[j].name);
107 if (flags_str[0] == '\0') {
108 (void) strcpy(flags_str, "OK");
114 static int h2i(char h)
116 if (h >= 'a' && h <= 'f') return h - 'a' + 10;
117 if (h >= 'A' && h <= 'F') return h - 'f' + 10;
121 static TDB_DATA hextodata(TALLOC_CTX *mem_ctx, const char *str)
124 TDB_DATA key = {NULL, 0};
128 DEBUG(DEBUG_ERR,("Key specified with odd number of hexadecimal digits\n"));
133 key.dptr = talloc_size(mem_ctx, key.dsize);
135 for (i=0; i < len/2; i++) {
136 key.dptr[i] = h2i(str[i*2]) << 4 | h2i(str[i*2+1]);
141 /* Parse a nodestring. Parameter dd_ok controls what happens to nodes
142 * that are disconnected or deleted. If dd_ok is true those nodes are
143 * included in the output list of nodes. If dd_ok is false, those
144 * nodes are filtered from the "all" case and cause an error if
145 * explicitly specified.
147 static bool parse_nodestring(struct ctdb_context *ctdb,
148 const char * nodestring,
149 uint32_t current_pnn,
156 struct ctdb_node_map *nodemap;
160 if (!ctdb_getnodemap(ctdb_connection, CTDB_CURRENT_NODE, &nodemap)) {
161 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
165 if (nodestring != NULL) {
166 *nodes = talloc_array(ctdb, uint32_t, 0);
167 CTDB_NOMEM_ABORT(*nodes);
171 if (strcmp(nodestring, "all") == 0) {
172 *pnn_mode = CTDB_BROADCAST_ALL;
175 for (i = 0; i < nodemap->num; i++) {
176 if ((nodemap->nodes[i].flags &
177 (NODE_FLAGS_DISCONNECTED |
178 NODE_FLAGS_DELETED)) && !dd_ok) {
181 *nodes = talloc_realloc(ctdb, *nodes,
183 CTDB_NOMEM_ABORT(*nodes);
191 ns = talloc_strdup(ctdb, nodestring);
192 tok = strtok(ns, ",");
193 while (tok != NULL) {
195 i = (uint32_t)strtoul(tok, NULL, 0);
196 if (i >= nodemap->num) {
197 DEBUG(DEBUG_ERR, ("Node %u does not exist\n", i));
200 if ((nodemap->nodes[i].flags &
201 (NODE_FLAGS_DISCONNECTED |
202 NODE_FLAGS_DELETED)) && !dd_ok) {
203 DEBUG(DEBUG_ERR, ("Node %u has status %s\n", i, pretty_print_flags(nodemap->nodes[i].flags)));
206 if (!ctdb_getpnn(ctdb_connection, i, &pnn)) {
207 DEBUG(DEBUG_ERR, ("Can not access node %u. Node is not operational.\n", i));
211 *nodes = talloc_realloc(ctdb, *nodes,
213 CTDB_NOMEM_ABORT(*nodes);
218 tok = strtok(NULL, ",");
223 *pnn_mode = (*nodes)[0];
225 *pnn_mode = CTDB_MULTICAST;
229 /* default - no nodes specified */
230 *nodes = talloc_array(ctdb, uint32_t, 1);
231 CTDB_NOMEM_ABORT(*nodes);
232 *pnn_mode = CTDB_CURRENT_NODE;
234 if (!ctdb_getpnn(ctdb_connection, current_pnn,
240 ctdb_free_nodemap(nodemap);
246 check if a database exists
248 static int db_exists(struct ctdb_context *ctdb, const char *db_name, bool *persistent)
251 struct ctdb_dbid_map *dbmap=NULL;
253 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
255 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
259 for(i=0;i<dbmap->num;i++){
262 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
263 if (!strcmp(name, db_name)) {
265 *persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
275 see if a process exists
277 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
285 if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
286 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
290 ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
292 printf("%u:%u exists\n", pnn, pid);
294 printf("%u:%u does not exist\n", pnn, pid);
300 display statistics structure
302 static void show_statistics(struct ctdb_statistics *s, int show_header)
304 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
306 const char *prefix=NULL;
308 int tmp, days, hours, minutes, seconds;
313 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
314 STATISTICS_FIELD(num_clients),
315 STATISTICS_FIELD(frozen),
316 STATISTICS_FIELD(recovering),
317 STATISTICS_FIELD(num_recoveries),
318 STATISTICS_FIELD(client_packets_sent),
319 STATISTICS_FIELD(client_packets_recv),
320 STATISTICS_FIELD(node_packets_sent),
321 STATISTICS_FIELD(node_packets_recv),
322 STATISTICS_FIELD(keepalive_packets_sent),
323 STATISTICS_FIELD(keepalive_packets_recv),
324 STATISTICS_FIELD(node.req_call),
325 STATISTICS_FIELD(node.reply_call),
326 STATISTICS_FIELD(node.req_dmaster),
327 STATISTICS_FIELD(node.reply_dmaster),
328 STATISTICS_FIELD(node.reply_error),
329 STATISTICS_FIELD(node.req_message),
330 STATISTICS_FIELD(node.req_control),
331 STATISTICS_FIELD(node.reply_control),
332 STATISTICS_FIELD(client.req_call),
333 STATISTICS_FIELD(client.req_message),
334 STATISTICS_FIELD(client.req_control),
335 STATISTICS_FIELD(timeouts.call),
336 STATISTICS_FIELD(timeouts.control),
337 STATISTICS_FIELD(timeouts.traverse),
338 STATISTICS_FIELD(total_calls),
339 STATISTICS_FIELD(pending_calls),
340 STATISTICS_FIELD(lockwait_calls),
341 STATISTICS_FIELD(pending_lockwait_calls),
342 STATISTICS_FIELD(childwrite_calls),
343 STATISTICS_FIELD(pending_childwrite_calls),
344 STATISTICS_FIELD(memory_used),
345 STATISTICS_FIELD(max_hop_count),
346 STATISTICS_FIELD(total_ro_delegations),
347 STATISTICS_FIELD(total_ro_revokes),
349 tmp = s->statistics_current_time.tv_sec - s->statistics_start_time.tv_sec;
358 if (options.machinereadable){
360 printf("CTDB version:");
361 printf("Current time of statistics:");
362 printf("Statistics collected since:");
363 for (i=0;i<ARRAY_SIZE(fields);i++) {
364 printf("%s:", fields[i].name);
366 printf("num_reclock_ctdbd_latency:");
367 printf("min_reclock_ctdbd_latency:");
368 printf("avg_reclock_ctdbd_latency:");
369 printf("max_reclock_ctdbd_latency:");
371 printf("num_reclock_recd_latency:");
372 printf("min_reclock_recd_latency:");
373 printf("avg_reclock_recd_latency:");
374 printf("max_reclock_recd_latency:");
376 printf("num_call_latency:");
377 printf("min_call_latency:");
378 printf("avg_call_latency:");
379 printf("max_call_latency:");
381 printf("num_lockwait_latency:");
382 printf("min_lockwait_latency:");
383 printf("avg_lockwait_latency:");
384 printf("max_lockwait_latency:");
386 printf("num_childwrite_latency:");
387 printf("min_childwrite_latency:");
388 printf("avg_childwrite_latency:");
389 printf("max_childwrite_latency:");
392 printf("%d:", CTDB_VERSION);
393 printf("%d:", (int)s->statistics_current_time.tv_sec);
394 printf("%d:", (int)s->statistics_start_time.tv_sec);
395 for (i=0;i<ARRAY_SIZE(fields);i++) {
396 printf("%d:", *(uint32_t *)(fields[i].offset+(uint8_t *)s));
398 printf("%d:", s->reclock.ctdbd.num);
399 printf("%.6f:", s->reclock.ctdbd.min);
400 printf("%.6f:", s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0);
401 printf("%.6f:", s->reclock.ctdbd.max);
403 printf("%d:", s->reclock.recd.num);
404 printf("%.6f:", s->reclock.recd.min);
405 printf("%.6f:", s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0);
406 printf("%.6f:", s->reclock.recd.max);
408 printf("%d:", s->call_latency.num);
409 printf("%.6f:", s->call_latency.min);
410 printf("%.6f:", s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0);
411 printf("%.6f:", s->call_latency.max);
413 printf("%d:", s->lockwait_latency.num);
414 printf("%.6f:", s->lockwait_latency.min);
415 printf("%.6f:", s->lockwait_latency.num?s->lockwait_latency.total/s->lockwait_latency.num:0.0);
416 printf("%.6f:", s->lockwait_latency.max);
418 printf("%d:", s->childwrite_latency.num);
419 printf("%.6f:", s->childwrite_latency.min);
420 printf("%.6f:", s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0);
421 printf("%.6f:", s->childwrite_latency.max);
424 printf("CTDB version %u\n", CTDB_VERSION);
425 printf("Current time of statistics : %s", ctime(&s->statistics_current_time.tv_sec));
426 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&s->statistics_start_time.tv_sec));
428 for (i=0;i<ARRAY_SIZE(fields);i++) {
429 if (strchr(fields[i].name, '.')) {
430 preflen = strcspn(fields[i].name, ".")+1;
431 if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
432 prefix = fields[i].name;
433 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
438 printf(" %*s%-22s%*s%10u\n",
440 fields[i].name+preflen,
442 *(uint32_t *)(fields[i].offset+(uint8_t *)s));
444 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);
446 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);
448 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);
449 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n", "lockwait_latency MIN/AVG/MAX", s->lockwait_latency.min, s->lockwait_latency.num?s->lockwait_latency.total/s->lockwait_latency.num:0.0, s->lockwait_latency.max, s->lockwait_latency.num);
450 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);
453 talloc_free(tmp_ctx);
457 display remote ctdb statistics combined from all nodes
459 static int control_statistics_all(struct ctdb_context *ctdb)
462 struct ctdb_statistics statistics;
466 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
467 CTDB_NO_MEMORY(ctdb, nodes);
469 ZERO_STRUCT(statistics);
471 for (i=0;i<num_nodes;i++) {
472 struct ctdb_statistics s1;
474 uint32_t *v1 = (uint32_t *)&s1;
475 uint32_t *v2 = (uint32_t *)&statistics;
477 offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
478 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
480 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
483 for (j=0;j<num_ints;j++) {
486 statistics.max_hop_count =
487 MAX(statistics.max_hop_count, s1.max_hop_count);
488 statistics.call_latency.max =
489 MAX(statistics.call_latency.max, s1.call_latency.max);
490 statistics.lockwait_latency.max =
491 MAX(statistics.lockwait_latency.max, s1.lockwait_latency.max);
494 printf("Gathered statistics for %u nodes\n", num_nodes);
495 show_statistics(&statistics, 1);
500 display remote ctdb statistics
502 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
505 struct ctdb_statistics statistics;
507 if (options.pnn == CTDB_BROADCAST_ALL) {
508 return control_statistics_all(ctdb);
511 ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
513 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
516 show_statistics(&statistics, 1);
522 reset remote ctdb statistics
524 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
528 ret = ctdb_statistics_reset(ctdb, options.pnn);
530 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
538 display remote ctdb rolling statistics
540 static int control_stats(struct ctdb_context *ctdb, int argc, const char **argv)
543 struct ctdb_statistics_wire *stats;
544 int i, num_records = -1;
547 num_records = atoi(argv[0]) - 1;
550 ret = ctdb_ctrl_getstathistory(ctdb, TIMELIMIT(), options.pnn, ctdb, &stats);
552 DEBUG(DEBUG_ERR, ("Unable to get rolling statistics from node %u\n", options.pnn));
555 for (i=0;i<stats->num;i++) {
556 if (stats->stats[i].statistics_start_time.tv_sec == 0) {
559 show_statistics(&stats->stats[i], i==0);
560 if (i == num_records) {
569 display remote ctdb db statistics
571 static int control_dbstatistics(struct ctdb_context *ctdb, int argc, const char **argv)
573 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
574 struct ctdb_db_statistics *dbstatistics;
575 struct ctdb_dbid_map *dbmap=NULL;
582 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
584 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
587 for(i=0;i<dbmap->num;i++){
590 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
591 if(!strcmp(argv[0], name)){
592 talloc_free(discard_const(name));
595 talloc_free(discard_const(name));
597 if (i == dbmap->num) {
598 DEBUG(DEBUG_ERR,("No database with name '%s' found\n", argv[0]));
599 talloc_free(tmp_ctx);
603 if (!ctdb_getdbstat(ctdb_connection, options.pnn, dbmap->dbs[i].dbid, &dbstatistics)) {
604 DEBUG(DEBUG_ERR,("Failed to read db statistics from node\n"));
605 talloc_free(tmp_ctx);
609 printf("DB Statistics:\n");
610 printf("RO Delegations: %d\n", dbstatistics->db_ro_delegations);
611 printf("RO Revokes: %d\n", dbstatistics->db_ro_revokes);
613 ctdb_free_dbstat(dbstatistics);
618 display uptime of remote node
620 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
623 struct ctdb_uptime *uptime = NULL;
624 int tmp, days, hours, minutes, seconds;
626 ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
628 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
632 if (options.machinereadable){
633 printf(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
634 printf(":%u:%u:%u:%lf\n",
635 (unsigned int)uptime->current_time.tv_sec,
636 (unsigned int)uptime->ctdbd_start_time.tv_sec,
637 (unsigned int)uptime->last_recovery_finished.tv_sec,
638 timeval_delta(&uptime->last_recovery_finished,
639 &uptime->last_recovery_started)
644 printf("Current time of node : %s", ctime(&uptime->current_time.tv_sec));
646 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
654 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
656 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
664 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
666 printf("Duration of last recovery/failover: %lf seconds\n",
667 timeval_delta(&uptime->last_recovery_finished,
668 &uptime->last_recovery_started));
674 show the PNN of the current node
676 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
681 ret = ctdb_getpnn(ctdb_connection, options.pnn, &mypnn);
683 DEBUG(DEBUG_ERR, ("Unable to get pnn from node."));
687 printf("PNN:%d\n", mypnn);
693 struct pnn_node *next;
698 static struct pnn_node *read_nodes_file(TALLOC_CTX *mem_ctx)
700 const char *nodes_list;
704 struct pnn_node *pnn_nodes = NULL;
705 struct pnn_node *pnn_node;
706 struct pnn_node *tmp_node;
708 /* read the nodes file */
709 nodes_list = getenv("CTDB_NODES");
710 if (nodes_list == NULL) {
711 nodes_list = "/etc/ctdb/nodes";
713 lines = file_lines_load(nodes_list, &nlines, mem_ctx);
717 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
720 for (i=0, pnn=0; i<nlines; i++) {
724 /* strip leading spaces */
725 while((*node == ' ') || (*node == '\t')) {
732 if (strcmp(node, "") == 0) {
735 pnn_node = talloc(mem_ctx, struct pnn_node);
736 pnn_node->pnn = pnn++;
737 pnn_node->addr = talloc_strdup(pnn_node, node);
738 pnn_node->next = pnn_nodes;
739 pnn_nodes = pnn_node;
742 /* swap them around so we return them in incrementing order */
743 pnn_node = pnn_nodes;
747 pnn_node = pnn_node->next;
749 tmp_node->next = pnn_nodes;
750 pnn_nodes = tmp_node;
757 show the PNN of the current node
758 discover the pnn by loading the nodes file and try to bind to all
759 addresses one at a time until the ip address is found.
761 static int control_xpnn(struct ctdb_context *ctdb, int argc, const char **argv)
763 TALLOC_CTX *mem_ctx = talloc_new(NULL);
764 struct pnn_node *pnn_nodes;
765 struct pnn_node *pnn_node;
767 pnn_nodes = read_nodes_file(mem_ctx);
768 if (pnn_nodes == NULL) {
769 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
770 talloc_free(mem_ctx);
774 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
777 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
778 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
779 talloc_free(mem_ctx);
783 if (ctdb_sys_have_ip(&addr)) {
784 printf("PNN:%d\n", pnn_node->pnn);
785 talloc_free(mem_ctx);
790 printf("Failed to detect which PNN this node is\n");
791 talloc_free(mem_ctx);
795 /* Helpers for ctdb status
797 static bool is_partially_online(struct ctdb_node_and_flags *node)
802 if (node->flags == 0) {
803 struct ctdb_ifaces_list *ifaces;
805 if (ctdb_getifaces(ctdb_connection, node->pnn, &ifaces)) {
806 for (j=0; j < ifaces->num; j++) {
807 if (ifaces->ifaces[j].link_state != 0) {
813 ctdb_free_ifaces(ifaces);
820 static int control_status_1_machine(int mypnn, struct ctdb_node_and_flags *node)
822 printf(":%d:%s:%d:%d:%d:%d:%d:%d:%d:%c:\n", node->pnn,
823 ctdb_addr_to_str(&node->addr),
824 !!(node->flags&NODE_FLAGS_DISCONNECTED),
825 !!(node->flags&NODE_FLAGS_BANNED),
826 !!(node->flags&NODE_FLAGS_PERMANENTLY_DISABLED),
827 !!(node->flags&NODE_FLAGS_UNHEALTHY),
828 !!(node->flags&NODE_FLAGS_STOPPED),
829 !!(node->flags&NODE_FLAGS_INACTIVE),
830 is_partially_online(node) ? 1 : 0,
831 (node->pnn == mypnn)?'Y':'N');
836 static int control_status_1_human(int mypnn, struct ctdb_node_and_flags *node)
838 printf("pnn:%d %-16s %s%s\n", node->pnn,
839 ctdb_addr_to_str(&node->addr),
840 is_partially_online(node) ? "PARTIALLYONLINE" : pretty_print_flags(node->flags),
841 node->pnn == mypnn?" (THIS NODE)":"");
847 display remote ctdb status
849 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
852 struct ctdb_vnn_map *vnnmap=NULL;
853 struct ctdb_node_map *nodemap=NULL;
854 uint32_t recmode, recmaster, mypnn;
856 if (!ctdb_getpnn(ctdb_connection, options.pnn, &mypnn)) {
860 if (!ctdb_getnodemap(ctdb_connection, options.pnn, &nodemap)) {
861 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
865 if (options.machinereadable) {
866 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
867 ":Inactive:PartiallyOnline:ThisNode:\n");
868 for (i=0;i<nodemap->num;i++) {
869 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
872 (void) control_status_1_machine(mypnn,
878 printf("Number of nodes:%d\n", nodemap->num);
879 for(i=0;i<nodemap->num;i++){
880 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
883 (void) control_status_1_human(mypnn, &nodemap->nodes[i]);
886 if (!ctdb_getvnnmap(ctdb_connection, options.pnn, &vnnmap)) {
887 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
890 if (vnnmap->generation == INVALID_GENERATION) {
891 printf("Generation:INVALID\n");
893 printf("Generation:%d\n",vnnmap->generation);
895 printf("Size:%d\n",vnnmap->size);
896 for(i=0;i<vnnmap->size;i++){
897 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
899 ctdb_free_vnnmap(vnnmap);
901 if (!ctdb_getrecmode(ctdb_connection, options.pnn, &recmode)) {
902 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
905 printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
907 if (!ctdb_getrecmaster(ctdb_connection, options.pnn, &recmaster)) {
908 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
911 printf("Recovery master:%d\n",recmaster);
916 static int control_nodestatus(struct ctdb_context *ctdb, int argc, const char **argv)
919 struct ctdb_node_map *nodemap=NULL;
921 uint32_t pnn_mode, mypnn;
927 if (!parse_nodestring(ctdb, argc == 1 ? argv[0] : NULL,
928 options.pnn, true, &nodes, &pnn_mode)) {
932 if (options.machinereadable) {
933 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
934 ":Inactive:PartiallyOnline:ThisNode:\n");
935 } else if (pnn_mode == CTDB_BROADCAST_ALL) {
936 printf("Number of nodes:%d\n", (int) talloc_array_length(nodes));
939 if (!ctdb_getpnn(ctdb_connection, CTDB_CURRENT_NODE, &mypnn)) {
940 DEBUG(DEBUG_ERR, ("Unable to get PNN from local node\n"));
944 if (!ctdb_getnodemap(ctdb_connection, options.pnn, &nodemap)) {
945 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
951 for (i = 0; i < talloc_array_length(nodes); i++) {
952 if (options.machinereadable) {
953 ret |= control_status_1_machine(mypnn,
954 &nodemap->nodes[nodes[i]]);
956 ret |= control_status_1_human(mypnn,
957 &nodemap->nodes[nodes[i]]);
964 struct natgw_node *next;
969 display the list of nodes belonging to this natgw configuration
971 static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **argv)
974 uint32_t capabilities;
975 const char *natgw_list;
978 struct natgw_node *natgw_nodes = NULL;
979 struct natgw_node *natgw_node;
980 struct ctdb_node_map *nodemap=NULL;
983 /* read the natgw nodes file into a linked list */
984 natgw_list = getenv("NATGW_NODES");
985 if (natgw_list == NULL) {
986 natgw_list = "/etc/ctdb/natgw_nodes";
988 lines = file_lines_load(natgw_list, &nlines, ctdb);
990 ctdb_set_error(ctdb, "Failed to load natgw node list '%s'\n", natgw_list);
993 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
996 for (i=0;i<nlines;i++) {
1000 /* strip leading spaces */
1001 while((*node == ' ') || (*node == '\t')) {
1007 if (strcmp(node, "") == 0) {
1010 natgw_node = talloc(ctdb, struct natgw_node);
1011 natgw_node->addr = talloc_strdup(natgw_node, node);
1012 CTDB_NO_MEMORY(ctdb, natgw_node->addr);
1013 natgw_node->next = natgw_nodes;
1014 natgw_nodes = natgw_node;
1017 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
1019 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node.\n"));
1024 while(i<nodemap->num) {
1025 for(natgw_node=natgw_nodes;natgw_node;natgw_node=natgw_node->next) {
1026 if (!strcmp(natgw_node->addr, ctdb_addr_to_str(&nodemap->nodes[i].addr))) {
1031 /* this node was not in the natgw so we just remove it from
1034 if ((natgw_node == NULL)
1035 || (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) ) {
1038 for (j=i+1; j<nodemap->num; j++) {
1039 nodemap->nodes[j-1] = nodemap->nodes[j];
1048 /* pick a node to be natgwmaster
1049 * we dont allow STOPPED, DELETED, BANNED or UNHEALTHY nodes to become the natgwmaster
1051 for(i=0;i<nodemap->num;i++){
1052 if (!(nodemap->nodes[i].flags & (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_UNHEALTHY))) {
1053 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, &capabilities);
1055 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", nodemap->nodes[i].pnn));
1058 if (!(capabilities&CTDB_CAP_NATGW)) {
1061 printf("%d %s\n", nodemap->nodes[i].pnn,ctdb_addr_to_str(&nodemap->nodes[i].addr));
1065 /* we couldnt find any healthy node, try unhealthy ones */
1066 if (i == nodemap->num) {
1067 for(i=0;i<nodemap->num;i++){
1068 if (!(nodemap->nodes[i].flags & (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED))) {
1069 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, &capabilities);
1071 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", nodemap->nodes[i].pnn));
1074 if (!(capabilities&CTDB_CAP_NATGW)) {
1077 printf("%d %s\n", nodemap->nodes[i].pnn,ctdb_addr_to_str(&nodemap->nodes[i].addr));
1082 /* unless all nodes are STOPPED, when we pick one anyway */
1083 if (i == nodemap->num) {
1084 for(i=0;i<nodemap->num;i++){
1085 if (!(nodemap->nodes[i].flags & (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED))) {
1086 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, &capabilities);
1088 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", nodemap->nodes[i].pnn));
1091 if (!(capabilities&CTDB_CAP_NATGW)) {
1094 printf("%d %s\n", nodemap->nodes[i].pnn, ctdb_addr_to_str(&nodemap->nodes[i].addr));
1098 /* or if we still can not find any */
1099 if (i == nodemap->num) {
1100 printf("-1 0.0.0.0\n");
1101 ret = 2; /* matches ENOENT */
1105 /* print the pruned list of nodes belonging to this natgw list */
1106 for(i=0;i<nodemap->num;i++){
1107 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1110 printf(":%d:%s:%d:%d:%d:%d:%d\n", nodemap->nodes[i].pnn,
1111 ctdb_addr_to_str(&nodemap->nodes[i].addr),
1112 !!(nodemap->nodes[i].flags&NODE_FLAGS_DISCONNECTED),
1113 !!(nodemap->nodes[i].flags&NODE_FLAGS_BANNED),
1114 !!(nodemap->nodes[i].flags&NODE_FLAGS_PERMANENTLY_DISABLED),
1115 !!(nodemap->nodes[i].flags&NODE_FLAGS_UNHEALTHY),
1116 !!(nodemap->nodes[i].flags&NODE_FLAGS_STOPPED));
1123 display the status of the scripts for monitoring (or other events)
1125 static int control_one_scriptstatus(struct ctdb_context *ctdb,
1126 enum ctdb_eventscript_call type)
1128 struct ctdb_scripts_wire *script_status;
1131 ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
1133 DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
1137 if (script_status == NULL) {
1138 if (!options.machinereadable) {
1139 printf("%s cycle never run\n",
1140 ctdb_eventscript_call_names[type]);
1145 if (!options.machinereadable) {
1146 printf("%d scripts were executed last %s cycle\n",
1147 script_status->num_scripts,
1148 ctdb_eventscript_call_names[type]);
1150 for (i=0; i<script_status->num_scripts; i++) {
1151 const char *status = NULL;
1153 switch (script_status->scripts[i].status) {
1155 status = "TIMEDOUT";
1158 status = "DISABLED";
1164 if (script_status->scripts[i].status > 0)
1168 if (options.machinereadable) {
1169 printf(":%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
1170 ctdb_eventscript_call_names[type],
1171 script_status->scripts[i].name,
1172 script_status->scripts[i].status,
1174 (long)script_status->scripts[i].start.tv_sec,
1175 (long)script_status->scripts[i].start.tv_usec,
1176 (long)script_status->scripts[i].finished.tv_sec,
1177 (long)script_status->scripts[i].finished.tv_usec,
1178 script_status->scripts[i].output);
1182 printf("%-20s Status:%s ",
1183 script_status->scripts[i].name, status);
1185 /* Some other error, eg from stat. */
1186 printf("%-20s Status:CANNOT RUN (%s)",
1187 script_status->scripts[i].name,
1188 strerror(-script_status->scripts[i].status));
1190 if (script_status->scripts[i].status >= 0) {
1191 printf("Duration:%.3lf ",
1192 timeval_delta(&script_status->scripts[i].finished,
1193 &script_status->scripts[i].start));
1195 if (script_status->scripts[i].status != -ENOEXEC) {
1197 ctime(&script_status->scripts[i].start.tv_sec));
1198 if (script_status->scripts[i].status != 0) {
1199 printf(" OUTPUT:%s\n",
1200 script_status->scripts[i].output);
1210 static int control_scriptstatus(struct ctdb_context *ctdb,
1211 int argc, const char **argv)
1214 enum ctdb_eventscript_call type, min, max;
1218 DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
1223 arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
1227 for (type = 0; type < CTDB_EVENT_MAX; type++) {
1228 if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
1234 if (type == CTDB_EVENT_MAX) {
1235 if (strcmp(arg, "all") == 0) {
1237 max = CTDB_EVENT_MAX;
1239 DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
1244 if (options.machinereadable) {
1245 printf(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1248 for (type = min; type < max; type++) {
1249 ret = control_one_scriptstatus(ctdb, type);
1259 enable an eventscript
1261 static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1269 ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1271 DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
1279 disable an eventscript
1281 static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1289 ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1291 DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
1299 display the pnn of the recovery master
1301 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1305 if (!ctdb_getrecmaster(ctdb_connection, options.pnn, &recmaster)) {
1306 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1309 printf("%d\n",recmaster);
1315 add a tickle to a public address
1317 static int control_add_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1319 struct ctdb_tcp_connection t;
1327 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1328 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1331 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1332 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1336 data.dptr = (uint8_t *)&t;
1337 data.dsize = sizeof(t);
1339 /* tell all nodes about this tcp connection */
1340 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE,
1341 0, data, ctdb, NULL, NULL, NULL, NULL);
1343 DEBUG(DEBUG_ERR,("Failed to add tickle\n"));
1352 delete a tickle from a node
1354 static int control_del_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1356 struct ctdb_tcp_connection t;
1364 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1365 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1368 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1369 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1373 data.dptr = (uint8_t *)&t;
1374 data.dsize = sizeof(t);
1376 /* tell all nodes about this tcp connection */
1377 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_REMOVE,
1378 0, data, ctdb, NULL, NULL, NULL, NULL);
1380 DEBUG(DEBUG_ERR,("Failed to remove tickle\n"));
1389 get a list of all tickles for this pnn
1391 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
1393 struct ctdb_control_tcp_tickle_list *list;
1394 ctdb_sock_addr addr;
1403 port = atoi(argv[1]);
1406 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1407 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1411 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
1413 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
1417 if (options.machinereadable){
1418 printf(":source ip:port:destination ip:port:\n");
1419 for (i=0;i<list->tickles.num;i++) {
1420 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1423 printf(":%s:%u", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1424 printf(":%s:%u:\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
1427 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
1428 printf("Num tickles:%u\n", list->tickles.num);
1429 for (i=0;i<list->tickles.num;i++) {
1430 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1433 printf("SRC: %s:%u ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1434 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));
1444 static int move_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1446 struct ctdb_all_public_ips *ips;
1447 struct ctdb_public_ip ip;
1450 uint32_t disable_time;
1452 struct ctdb_node_map *nodemap=NULL;
1453 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1456 data.dptr = (uint8_t*)&disable_time;
1457 data.dsize = sizeof(disable_time);
1458 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1460 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1466 /* read the public ip list from the node */
1467 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
1469 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
1470 talloc_free(tmp_ctx);
1474 for (i=0;i<ips->num;i++) {
1475 if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
1480 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
1481 pnn, ctdb_addr_to_str(addr)));
1482 talloc_free(tmp_ctx);
1489 data.dptr = (uint8_t *)&ip;
1490 data.dsize = sizeof(ip);
1492 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1494 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1495 talloc_free(tmp_ctx);
1499 nodes = list_of_active_nodes_except_pnn(ctdb, nodemap, tmp_ctx, pnn);
1500 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1507 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1508 talloc_free(tmp_ctx);
1512 ret = ctdb_ctrl_takeover_ip(ctdb, LONGTIMELIMIT(), pnn, &ip);
1514 DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
1515 talloc_free(tmp_ctx);
1519 /* update the recovery daemon so it now knows to expect the new
1520 node assignment for this ip.
1522 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_RECD_UPDATE_IP, data);
1524 DEBUG(DEBUG_ERR,("Failed to send message to update the ip on the recovery master.\n"));
1528 talloc_free(tmp_ctx);
1533 move/failover an ip address to a specific node
1535 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
1538 int ret, retries = 0;
1539 ctdb_sock_addr addr;
1546 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1547 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1552 if (sscanf(argv[1], "%u", &pnn) != 1) {
1553 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
1558 ret = move_ip(ctdb, &addr, pnn);
1560 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Wait 3 second and try again.\n", pnn));
1564 } while (retries < 5 && ret != 0);
1566 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Giving up.\n", pnn));
1573 static int rebalance_node(struct ctdb_context *ctdb, uint32_t pnn)
1578 if (ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), pnn, &recmaster) != 0) {
1579 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", pnn));
1583 data.dptr = (uint8_t *)&pnn;
1584 data.dsize = sizeof(uint32_t);
1585 if (ctdb_client_send_message(ctdb, recmaster, CTDB_SRVID_REBALANCE_NODE, data) != 0) {
1586 DEBUG(DEBUG_ERR,("Failed to send message to force node reallocation\n"));
1595 rebalance a node by setting it to allow failback and triggering a
1598 static int control_rebalancenode(struct ctdb_context *ctdb, int argc, const char **argv)
1600 switch (options.pnn) {
1601 case CTDB_BROADCAST_ALL:
1602 case CTDB_CURRENT_NODE:
1603 DEBUG(DEBUG_ERR,("You must specify a node number with -n <pnn> for the node to rebalance\n"));
1607 return rebalance_node(ctdb, options.pnn);
1611 static int getips_store_callback(void *param, void *data)
1613 struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
1614 struct ctdb_all_public_ips *ips = param;
1618 ips->ips[i].pnn = node_ip->pnn;
1619 ips->ips[i].addr = node_ip->addr;
1623 static int getips_count_callback(void *param, void *data)
1625 uint32_t *count = param;
1632 static uint32_t *ip_key(ctdb_sock_addr *ip)
1634 static uint32_t key[IP_KEYLEN];
1636 bzero(key, sizeof(key));
1638 switch (ip->sa.sa_family) {
1640 key[0] = ip->ip.sin_addr.s_addr;
1643 uint32_t *s6_a32 = (uint32_t *)&(ip->ip6.sin6_addr.s6_addr);
1651 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
1658 static void *add_ip_callback(void *parm, void *data)
1664 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
1666 struct ctdb_all_public_ips *tmp_ips;
1667 struct ctdb_node_map *nodemap=NULL;
1668 trbt_tree_t *ip_tree;
1672 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1674 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1678 ip_tree = trbt_create(tmp_ctx, 0);
1680 for(i=0;i<nodemap->num;i++){
1681 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1684 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
1688 /* read the public ip list from this node */
1689 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
1691 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1695 for (j=0; j<tmp_ips->num;j++) {
1696 struct ctdb_public_ip *node_ip;
1698 node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
1699 node_ip->pnn = tmp_ips->ips[j].pnn;
1700 node_ip->addr = tmp_ips->ips[j].addr;
1702 trbt_insertarray32_callback(ip_tree,
1703 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
1707 talloc_free(tmp_ips);
1712 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
1714 len = offsetof(struct ctdb_all_public_ips, ips) +
1715 count*sizeof(struct ctdb_public_ip);
1716 tmp_ips = talloc_zero_size(tmp_ctx, len);
1717 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
1726 * scans all other nodes and returns a pnn for another node that can host this
1730 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1732 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1733 struct ctdb_all_public_ips *ips;
1734 struct ctdb_node_map *nodemap=NULL;
1737 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1739 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1740 talloc_free(tmp_ctx);
1744 for(i=0;i<nodemap->num;i++){
1745 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1748 if (nodemap->nodes[i].pnn == options.pnn) {
1752 /* read the public ip list from this node */
1753 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
1755 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1759 for (j=0;j<ips->num;j++) {
1760 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1761 talloc_free(tmp_ctx);
1762 return nodemap->nodes[i].pnn;
1768 talloc_free(tmp_ctx);
1772 static uint32_t ipreallocate_finished;
1775 handler for receiving the response to ipreallocate
1777 static void ip_reallocate_handler(struct ctdb_context *ctdb, uint64_t srvid,
1778 TDB_DATA data, void *private_data)
1780 ipreallocate_finished = 1;
1783 static void ctdb_every_second(struct event_context *ev, struct timed_event *te, struct timeval t, void *p)
1785 struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
1787 event_add_timed(ctdb->ev, ctdb,
1788 timeval_current_ofs(1, 0),
1789 ctdb_every_second, ctdb);
1793 ask the recovery daemon on the recovery master to perform a ip reallocation
1795 static int control_ipreallocate(struct ctdb_context *ctdb, int argc, const char **argv)
1799 struct takeover_run_reply rd;
1801 struct ctdb_node_map *nodemap=NULL;
1803 struct timeval tv = timeval_current();
1805 /* we need some events to trigger so we can timeout and restart
1808 event_add_timed(ctdb->ev, ctdb,
1809 timeval_current_ofs(1, 0),
1810 ctdb_every_second, ctdb);
1812 rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
1814 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
1817 rd.srvid = getpid();
1819 /* register a message port for receiveing the reply so that we
1820 can receive the reply
1822 ctdb_client_set_message_handler(ctdb, rd.srvid, ip_reallocate_handler, NULL);
1824 data.dptr = (uint8_t *)&rd;
1825 data.dsize = sizeof(rd);
1828 /* check that there are valid nodes available */
1829 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap) != 0) {
1830 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
1833 for (i=0; i<nodemap->num;i++) {
1834 if ((nodemap->nodes[i].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) == 0) {
1838 if (i==nodemap->num) {
1839 DEBUG(DEBUG_ERR,("No recmaster available, no need to wait for cluster convergence\n"));
1844 if (!ctdb_getrecmaster(ctdb_connection, options.pnn, &recmaster)) {
1845 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1849 /* verify the node exists */
1850 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), recmaster, ctdb, &nodemap) != 0) {
1851 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
1856 /* check tha there are nodes available that can act as a recmaster */
1857 for (i=0; i<nodemap->num; i++) {
1858 if (nodemap->nodes[i].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
1863 if (i == nodemap->num) {
1864 DEBUG(DEBUG_ERR,("No possible nodes to host addresses.\n"));
1868 /* verify the recovery master is not STOPPED, nor BANNED */
1869 if (nodemap->nodes[recmaster].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
1870 DEBUG(DEBUG_ERR,("No suitable recmaster found. Try again\n"));
1876 /* verify the recovery master is not STOPPED, nor BANNED */
1877 if (nodemap->nodes[recmaster].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
1878 DEBUG(DEBUG_ERR,("No suitable recmaster found. Try again\n"));
1884 ipreallocate_finished = 0;
1885 ret = ctdb_client_send_message(ctdb, recmaster, CTDB_SRVID_TAKEOVER_RUN, data);
1887 DEBUG(DEBUG_ERR,("Failed to send ip takeover run request message to %u\n", options.pnn));
1891 tv = timeval_current();
1892 /* this loop will terminate when we have received the reply */
1893 while (timeval_elapsed(&tv) < 5.0 && ipreallocate_finished == 0) {
1894 event_loop_once(ctdb->ev);
1896 if (ipreallocate_finished == 1) {
1909 add a public ip address to a node
1911 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
1914 int len, retries = 0;
1916 ctdb_sock_addr addr;
1917 struct ctdb_control_ip_iface *pub;
1918 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1919 struct ctdb_all_public_ips *ips;
1923 talloc_free(tmp_ctx);
1927 if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
1928 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
1929 talloc_free(tmp_ctx);
1933 /* read the public ip list from the node */
1934 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
1936 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", options.pnn));
1937 talloc_free(tmp_ctx);
1940 for (i=0;i<ips->num;i++) {
1941 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
1942 DEBUG(DEBUG_ERR,("Can not add ip to node. Node already hosts this ip\n"));
1949 /* Dont timeout. This command waits for an ip reallocation
1950 which sometimes can take wuite a while if there has
1951 been a recent recovery
1955 len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
1956 pub = talloc_size(tmp_ctx, len);
1957 CTDB_NO_MEMORY(ctdb, pub);
1961 pub->len = strlen(argv[1])+1;
1962 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
1965 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
1967 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options.pnn));
1971 } while (retries < 5 && ret != 0);
1973 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Giving up.\n", options.pnn));
1974 talloc_free(tmp_ctx);
1978 if (rebalance_node(ctdb, options.pnn) != 0) {
1979 DEBUG(DEBUG_ERR,("Error when trying to rebalance node\n"));
1983 talloc_free(tmp_ctx);
1987 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
1989 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
1991 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1992 struct ctdb_node_map *nodemap=NULL;
1993 struct ctdb_all_public_ips *ips;
1996 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1998 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
2002 /* remove it from the nodes that are not hosting the ip currently */
2003 for(i=0;i<nodemap->num;i++){
2004 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2007 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
2008 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2012 for (j=0;j<ips->num;j++) {
2013 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2021 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
2025 options.pnn = nodemap->nodes[i].pnn;
2026 control_delip(ctdb, argc, argv);
2030 /* remove it from every node (also the one hosting it) */
2031 for(i=0;i<nodemap->num;i++){
2032 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2035 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
2036 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2040 for (j=0;j<ips->num;j++) {
2041 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2049 options.pnn = nodemap->nodes[i].pnn;
2050 control_delip(ctdb, argc, argv);
2053 talloc_free(tmp_ctx);
2058 delete a public ip address from a node
2060 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
2064 ctdb_sock_addr addr;
2065 struct ctdb_control_ip_iface pub;
2066 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2067 struct ctdb_all_public_ips *ips;
2070 talloc_free(tmp_ctx);
2074 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2075 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2079 if (options.pnn == CTDB_BROADCAST_ALL) {
2080 return control_delip_all(ctdb, argc, argv, &addr);
2087 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2089 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
2090 talloc_free(tmp_ctx);
2094 for (i=0;i<ips->num;i++) {
2095 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2101 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
2102 ctdb_addr_to_str(&addr)));
2103 talloc_free(tmp_ctx);
2107 if (ips->ips[i].pnn == options.pnn) {
2108 ret = find_other_host_for_public_ip(ctdb, &addr);
2111 ret = move_ip(ctdb, &addr, ret);
2113 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Wait 3 seconds and try again.\n", options.pnn));
2117 } while (retries < 5 && ret != 0);
2119 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Giving up.\n", options.pnn));
2125 ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
2127 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
2128 talloc_free(tmp_ctx);
2132 talloc_free(tmp_ctx);
2137 kill a tcp connection
2139 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2142 struct ctdb_control_killtcp killtcp;
2148 if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
2149 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2153 if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
2154 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2158 ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
2160 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
2171 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
2174 ctdb_sock_addr addr;
2180 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2181 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
2185 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
2187 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
2195 register a server id
2197 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2200 struct ctdb_server_id server_id;
2206 server_id.pnn = strtoul(argv[0], NULL, 0);
2207 server_id.type = strtoul(argv[1], NULL, 0);
2208 server_id.server_id = strtoul(argv[2], NULL, 0);
2210 ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
2212 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
2215 DEBUG(DEBUG_ERR,("Srvid registered. Sleeping for 999 seconds\n"));
2221 unregister a server id
2223 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2226 struct ctdb_server_id server_id;
2232 server_id.pnn = strtoul(argv[0], NULL, 0);
2233 server_id.type = strtoul(argv[1], NULL, 0);
2234 server_id.server_id = strtoul(argv[2], NULL, 0);
2236 ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
2238 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
2245 check if a server id exists
2247 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2251 struct ctdb_server_id server_id;
2257 server_id.pnn = strtoul(argv[0], NULL, 0);
2258 server_id.type = strtoul(argv[1], NULL, 0);
2259 server_id.server_id = strtoul(argv[2], NULL, 0);
2261 ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
2263 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
2268 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
2270 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
2276 get a list of all server ids that are registered on a node
2278 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
2281 struct ctdb_server_id_list *server_ids;
2283 ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
2285 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
2289 for (i=0; i<server_ids->num; i++) {
2290 printf("Server id %d:%d:%d\n",
2291 server_ids->server_ids[i].pnn,
2292 server_ids->server_ids[i].type,
2293 server_ids->server_ids[i].server_id);
2300 check if a server id exists
2302 static int check_srvids(struct ctdb_context *ctdb, int argc, const char **argv)
2304 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
2310 talloc_free(tmp_ctx);
2314 ids = talloc_array(tmp_ctx, uint64_t, argc);
2315 result = talloc_array(tmp_ctx, uint8_t, argc);
2317 for (i = 0; i < argc; i++) {
2318 ids[i] = strtoull(argv[i], NULL, 0);
2321 if (!ctdb_check_message_handlers(ctdb_connection,
2322 options.pnn, argc, ids, result)) {
2323 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n",
2325 talloc_free(tmp_ctx);
2329 for (i=0; i < argc; i++) {
2330 printf("Server id %d:%llu %s\n", options.pnn, (long long)ids[i],
2331 result[i] ? "exists" : "does not exist");
2334 talloc_free(tmp_ctx);
2339 send a tcp tickle ack
2341 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2344 ctdb_sock_addr src, dst;
2350 if (!parse_ip_port(argv[0], &src)) {
2351 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2355 if (!parse_ip_port(argv[1], &dst)) {
2356 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2360 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
2364 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
2371 display public ip status
2373 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
2376 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2377 struct ctdb_all_public_ips *ips;
2379 if (options.pnn == CTDB_BROADCAST_ALL) {
2380 /* read the list of public ips from all nodes */
2381 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
2383 /* read the public ip list from this node */
2384 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2387 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
2388 talloc_free(tmp_ctx);
2392 if (options.machinereadable){
2393 printf(":Public IP:Node:");
2394 if (options.verbose){
2395 printf("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2399 if (options.pnn == CTDB_BROADCAST_ALL) {
2400 printf("Public IPs on ALL nodes\n");
2402 printf("Public IPs on node %u\n", options.pnn);
2406 for (i=1;i<=ips->num;i++) {
2407 struct ctdb_control_public_ip_info *info = NULL;
2409 char *aciface = NULL;
2410 char *avifaces = NULL;
2411 char *cifaces = NULL;
2413 if (options.pnn == CTDB_BROADCAST_ALL) {
2414 pnn = ips->ips[ips->num-i].pnn;
2420 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
2421 &ips->ips[ips->num-i].addr, &info);
2428 for (j=0; j < info->num; j++) {
2429 if (cifaces == NULL) {
2430 cifaces = talloc_strdup(info,
2431 info->ifaces[j].name);
2433 cifaces = talloc_asprintf_append(cifaces,
2435 info->ifaces[j].name);
2438 if (info->active_idx == j) {
2439 aciface = info->ifaces[j].name;
2442 if (info->ifaces[j].link_state == 0) {
2446 if (avifaces == NULL) {
2447 avifaces = talloc_strdup(info, info->ifaces[j].name);
2449 avifaces = talloc_asprintf_append(avifaces,
2451 info->ifaces[j].name);
2456 if (options.machinereadable){
2458 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2459 ips->ips[ips->num-i].pnn);
2460 if (options.verbose){
2463 avifaces?avifaces:"",
2464 cifaces?cifaces:"");
2468 if (options.verbose) {
2469 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
2470 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2471 ips->ips[ips->num-i].pnn,
2473 avifaces?avifaces:"",
2474 cifaces?cifaces:"");
2477 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2478 ips->ips[ips->num-i].pnn);
2484 talloc_free(tmp_ctx);
2491 static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
2494 ctdb_sock_addr addr;
2495 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2496 struct ctdb_control_public_ip_info *info;
2499 talloc_free(tmp_ctx);
2503 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2504 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2508 /* read the public ip info from this node */
2509 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), options.pnn,
2510 tmp_ctx, &addr, &info);
2512 DEBUG(DEBUG_ERR, ("Unable to get public ip[%s]info from node %u\n",
2513 argv[0], options.pnn));
2514 talloc_free(tmp_ctx);
2518 printf("Public IP[%s] info on node %u\n",
2519 ctdb_addr_to_str(&info->ip.addr),
2522 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
2523 ctdb_addr_to_str(&info->ip.addr),
2524 info->ip.pnn, info->num);
2526 for (i=0; i<info->num; i++) {
2527 info->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
2529 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
2530 i+1, info->ifaces[i].name,
2531 info->ifaces[i].link_state?"up":"down",
2532 (unsigned int)info->ifaces[i].references,
2533 (i==info->active_idx)?" (active)":"");
2536 talloc_free(tmp_ctx);
2541 display interfaces status
2543 static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
2546 struct ctdb_ifaces_list *ifaces;
2548 /* read the public ip list from this node */
2549 if (!ctdb_getifaces(ctdb_connection, options.pnn, &ifaces)) {
2550 DEBUG(DEBUG_ERR, ("Unable to get interfaces from node %u\n",
2555 if (options.machinereadable){
2556 printf(":Name:LinkStatus:References:\n");
2558 printf("Interfaces on node %u\n", options.pnn);
2561 for (i=0; i<ifaces->num; i++) {
2562 if (options.machinereadable){
2563 printf(":%s:%s:%u\n",
2564 ifaces->ifaces[i].name,
2565 ifaces->ifaces[i].link_state?"1":"0",
2566 (unsigned int)ifaces->ifaces[i].references);
2568 printf("name:%s link:%s references:%u\n",
2569 ifaces->ifaces[i].name,
2570 ifaces->ifaces[i].link_state?"up":"down",
2571 (unsigned int)ifaces->ifaces[i].references);
2575 ctdb_free_ifaces(ifaces);
2581 set link status of an interface
2583 static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
2586 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2587 struct ctdb_control_iface_info info;
2595 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
2596 DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
2598 talloc_free(tmp_ctx);
2601 strcpy(info.name, argv[0]);
2603 if (strcmp(argv[1], "up") == 0) {
2604 info.link_state = 1;
2605 } else if (strcmp(argv[1], "down") == 0) {
2606 info.link_state = 0;
2608 DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
2610 talloc_free(tmp_ctx);
2614 /* read the public ip list from this node */
2615 ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
2618 DEBUG(DEBUG_ERR, ("Unable to set link state for interfaces %s node %u\n",
2619 argv[0], options.pnn));
2620 talloc_free(tmp_ctx);
2624 talloc_free(tmp_ctx);
2629 display pid of a ctdb daemon
2631 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
2636 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
2638 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
2641 printf("Pid:%d\n", pid);
2647 disable a remote node
2649 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
2652 struct ctdb_node_map *nodemap=NULL;
2654 /* check if the node is already disabled */
2655 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2656 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2659 if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2660 DEBUG(DEBUG_ERR,("Node %d is already disabled.\n", options.pnn));
2665 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, NODE_FLAGS_PERMANENTLY_DISABLED, 0);
2667 DEBUG(DEBUG_ERR, ("Unable to disable node %u\n", options.pnn));
2673 /* read the nodemap and verify the change took effect */
2674 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2675 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2679 } while (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED));
2680 ret = control_ipreallocate(ctdb, argc, argv);
2682 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2690 enable a disabled remote node
2692 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
2696 struct ctdb_node_map *nodemap=NULL;
2699 /* check if the node is already enabled */
2700 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2701 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2704 if (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED)) {
2705 DEBUG(DEBUG_ERR,("Node %d is already enabled.\n", options.pnn));
2710 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, 0, NODE_FLAGS_PERMANENTLY_DISABLED);
2712 DEBUG(DEBUG_ERR, ("Unable to enable node %u\n", options.pnn));
2718 /* read the nodemap and verify the change took effect */
2719 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2720 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2724 } while (nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED);
2726 ret = control_ipreallocate(ctdb, argc, argv);
2728 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2738 static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
2741 struct ctdb_node_map *nodemap=NULL;
2744 ret = ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn);
2746 DEBUG(DEBUG_ERR, ("Unable to stop node %u try again\n", options.pnn));
2751 /* read the nodemap and verify the change took effect */
2752 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2753 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2757 } while (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_STOPPED));
2758 ret = control_ipreallocate(ctdb, argc, argv);
2760 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2768 restart a stopped remote node
2770 static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
2774 struct ctdb_node_map *nodemap=NULL;
2777 ret = ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn);
2779 DEBUG(DEBUG_ERR, ("Unable to continue node %u\n", options.pnn));
2785 /* read the nodemap and verify the change took effect */
2786 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2787 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2791 } while (nodemap->nodes[options.pnn].flags & NODE_FLAGS_STOPPED);
2792 ret = control_ipreallocate(ctdb, argc, argv);
2794 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2801 static uint32_t get_generation(struct ctdb_context *ctdb)
2803 struct ctdb_vnn_map *vnnmap=NULL;
2806 /* wait until the recmaster is not in recovery mode */
2808 uint32_t recmode, recmaster;
2810 if (vnnmap != NULL) {
2811 talloc_free(vnnmap);
2815 /* get the recmaster */
2816 if (!ctdb_getrecmaster(ctdb_connection, CTDB_CURRENT_NODE, &recmaster)) {
2817 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
2821 /* get recovery mode */
2822 if (!ctdb_getrecmode(ctdb_connection, recmaster, &recmode)) {
2823 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
2827 /* get the current generation number */
2828 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, ctdb, &vnnmap);
2830 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
2834 if ((recmode == CTDB_RECOVERY_NORMAL)
2835 && (vnnmap->generation != 1)){
2836 return vnnmap->generation;
2843 ban a node from the cluster
2845 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
2848 struct ctdb_node_map *nodemap=NULL;
2849 struct ctdb_ban_time bantime;
2855 /* verify the node exists */
2856 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2858 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2862 if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_BANNED) {
2863 DEBUG(DEBUG_ERR,("Node %u is already banned.\n", options.pnn));
2867 bantime.pnn = options.pnn;
2868 bantime.time = strtoul(argv[0], NULL, 0);
2870 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, &bantime);
2872 DEBUG(DEBUG_ERR,("Banning node %d for %d seconds failed.\n", bantime.pnn, bantime.time));
2876 ret = control_ipreallocate(ctdb, argc, argv);
2878 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2887 unban a node from the cluster
2889 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
2892 struct ctdb_node_map *nodemap=NULL;
2893 struct ctdb_ban_time bantime;
2895 /* verify the node exists */
2896 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2898 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2902 if (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_BANNED)) {
2903 DEBUG(DEBUG_ERR,("Node %u is not banned.\n", options.pnn));
2907 bantime.pnn = options.pnn;
2910 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, &bantime);
2912 DEBUG(DEBUG_ERR,("Unbanning node %d failed.\n", bantime.pnn));
2916 ret = control_ipreallocate(ctdb, argc, argv);
2918 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2927 show ban information for a node
2929 static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
2932 struct ctdb_node_map *nodemap=NULL;
2933 struct ctdb_ban_time *bantime;
2935 /* verify the node exists */
2936 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2938 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2942 ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
2944 DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
2948 if (bantime->time == 0) {
2949 printf("Node %u is not banned\n", bantime->pnn);
2951 printf("Node %u is banned banned for %d seconds\n", bantime->pnn, bantime->time);
2960 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
2964 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
2966 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
2976 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
2979 uint32_t generation, next_generation;
2981 /* record the current generation number */
2982 generation = get_generation(ctdb);
2984 ret = ctdb_ctrl_freeze_priority(ctdb, TIMELIMIT(), options.pnn, 1);
2986 DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
2990 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2992 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
2996 /* wait until we are in a new generation */
2998 next_generation = get_generation(ctdb);
2999 if (next_generation != generation) {
3010 display monitoring mode of a remote node
3012 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
3017 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
3019 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
3022 if (!options.machinereadable){
3023 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
3026 printf(":%d:\n",monmode);
3033 display capabilities of a remote node
3035 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
3037 uint32_t capabilities;
3040 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
3042 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
3046 if (!options.machinereadable){
3047 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
3048 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
3049 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
3050 printf("NATGW: %s\n", (capabilities&CTDB_CAP_NATGW)?"YES":"NO");
3052 printf(":RECMASTER:LMASTER:LVS:NATGW:\n");
3053 printf(":%d:%d:%d:%d:\n",
3054 !!(capabilities&CTDB_CAP_RECMASTER),
3055 !!(capabilities&CTDB_CAP_LMASTER),
3056 !!(capabilities&CTDB_CAP_LVS),
3057 !!(capabilities&CTDB_CAP_NATGW));
3063 display lvs configuration
3065 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
3067 uint32_t *capabilities;
3068 struct ctdb_node_map *nodemap=NULL;
3070 int healthy_count = 0;
3072 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
3074 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3078 capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
3079 CTDB_NO_MEMORY(ctdb, capabilities);
3081 /* collect capabilities for all connected nodes */
3082 for (i=0; i<nodemap->num; i++) {
3083 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3086 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3090 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
3092 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
3096 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3100 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
3105 /* Print all LVS nodes */
3106 for (i=0; i<nodemap->num; i++) {
3107 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3110 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3113 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3117 if (healthy_count != 0) {
3118 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
3123 printf("%d:%s\n", i,
3124 ctdb_addr_to_str(&nodemap->nodes[i].addr));
3131 display who is the lvs master
3133 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
3135 uint32_t *capabilities;
3136 struct ctdb_node_map *nodemap=NULL;
3138 int healthy_count = 0;
3140 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
3142 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3146 capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
3147 CTDB_NO_MEMORY(ctdb, capabilities);
3149 /* collect capabilities for all connected nodes */
3150 for (i=0; i<nodemap->num; i++) {
3151 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3154 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3158 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
3160 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
3164 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3168 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
3173 /* find and show the lvsmaster */
3174 for (i=0; i<nodemap->num; i++) {
3175 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3178 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3181 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3185 if (healthy_count != 0) {
3186 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
3191 if (options.machinereadable){
3194 printf("Node %d is LVS master\n", i);
3199 printf("There is no LVS master\n");
3204 disable monitoring on a node
3206 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3211 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
3213 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
3216 printf("Monitoring mode:%s\n","DISABLED");
3222 enable monitoring on a node
3224 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3229 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
3231 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
3234 printf("Monitoring mode:%s\n","ACTIVE");
3240 display remote list of keys/data for a db
3242 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
3244 const char *db_name;
3245 struct ctdb_db_context *ctdb_db;
3248 struct ctdb_dump_db_context c;
3257 if (db_exists(ctdb, db_name, &persistent)) {
3258 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3262 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3264 if (ctdb_db == NULL) {
3265 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3269 if (options.printlmaster) {
3270 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn,
3271 ctdb, &ctdb->vnn_map);
3273 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
3281 c.printemptyrecords = (bool)options.printemptyrecords;
3282 c.printdatasize = (bool)options.printdatasize;
3283 c.printlmaster = (bool)options.printlmaster;
3284 c.printhash = (bool)options.printhash;
3285 c.printrecordflags = (bool)options.printrecordflags;
3287 /* traverse and dump the cluster tdb */
3288 ret = ctdb_dump_db(ctdb_db, &c);
3290 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
3291 DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
3292 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3296 talloc_free(ctdb_db);
3298 printf("Dumped %d records\n", ret);
3302 struct cattdb_data {
3303 struct ctdb_context *ctdb;
3307 static int cattdb_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
3309 struct cattdb_data *d = private_data;
3310 struct ctdb_dump_db_context c;
3316 c.printemptyrecords = (bool)options.printemptyrecords;
3317 c.printdatasize = (bool)options.printdatasize;
3318 c.printlmaster = false;
3319 c.printhash = (bool)options.printhash;
3320 c.printrecordflags = true;
3322 return ctdb_dumpdb_record(d->ctdb, key, data, &c);
3326 cat the local tdb database using same format as catdb
3328 static int control_cattdb(struct ctdb_context *ctdb, int argc, const char **argv)
3330 const char *db_name;
3331 struct ctdb_db_context *ctdb_db;
3332 struct cattdb_data d;
3342 if (db_exists(ctdb, db_name, &persistent)) {
3343 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3347 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, false, 0);
3349 if (ctdb_db == NULL) {
3350 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3354 /* traverse the local tdb */
3357 if (tdb_traverse_read(ctdb_db->ltdb->tdb, cattdb_traverse, &d) == -1) {
3358 printf("Failed to cattdb data\n");
3361 talloc_free(ctdb_db);
3363 printf("Dumped %d records\n", d.count);
3368 display the content of a database key
3370 static int control_readkey(struct ctdb_context *ctdb, int argc, const char **argv)
3372 const char *db_name;
3373 struct ctdb_db_context *ctdb_db;
3374 struct ctdb_record_handle *h;
3375 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3385 if (db_exists(ctdb, db_name, &persistent)) {
3386 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3390 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3392 if (ctdb_db == NULL) {
3393 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3397 key.dptr = discard_const(argv[1]);
3398 key.dsize = strlen((char *)key.dptr);
3400 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3402 printf("Failed to fetch record '%s' on node %d\n",
3403 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3404 talloc_free(tmp_ctx);
3408 printf("Data: size:%d ptr:[%s]\n", (int)data.dsize, data.dptr);
3410 talloc_free(ctdb_db);
3411 talloc_free(tmp_ctx);
3416 display the content of a database key
3418 static int control_writekey(struct ctdb_context *ctdb, int argc, const char **argv)
3420 const char *db_name;
3421 struct ctdb_db_context *ctdb_db;
3422 struct ctdb_record_handle *h;
3423 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3433 if (db_exists(ctdb, db_name, &persistent)) {
3434 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3438 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3440 if (ctdb_db == NULL) {
3441 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3445 key.dptr = discard_const(argv[1]);
3446 key.dsize = strlen((char *)key.dptr);
3448 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3450 printf("Failed to fetch record '%s' on node %d\n",
3451 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3452 talloc_free(tmp_ctx);
3456 data.dptr = discard_const(argv[2]);
3457 data.dsize = strlen((char *)data.dptr);
3459 if (ctdb_record_store(h, data) != 0) {
3460 printf("Failed to store record\n");
3464 talloc_free(ctdb_db);
3465 talloc_free(tmp_ctx);
3470 fetch a record from a persistent database
3472 static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3474 const char *db_name;
3475 struct ctdb_db_context *ctdb_db;
3476 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3477 struct ctdb_transaction_handle *h;
3483 talloc_free(tmp_ctx);
3490 if (db_exists(ctdb, db_name, &persistent)) {
3491 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3492 talloc_free(tmp_ctx);
3497 DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", db_name));
3498 talloc_free(tmp_ctx);
3502 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3504 if (ctdb_db == NULL) {
3505 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3506 talloc_free(tmp_ctx);
3510 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3512 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3513 talloc_free(tmp_ctx);
3517 key.dptr = discard_const(argv[1]);
3518 key.dsize = strlen(argv[1]);
3519 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
3521 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
3522 talloc_free(tmp_ctx);
3526 if (data.dsize == 0 || data.dptr == NULL) {
3527 DEBUG(DEBUG_ERR,("Record is empty\n"));
3528 talloc_free(tmp_ctx);
3533 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3535 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3536 talloc_free(tmp_ctx);
3539 write(fd, data.dptr, data.dsize);
3542 write(1, data.dptr, data.dsize);
3545 /* abort the transaction */
3549 talloc_free(tmp_ctx);
3554 fetch a record from a tdb-file
3556 static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3558 const char *tdb_file;
3561 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
3570 tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
3572 printf("Failed to open TDB file %s\n", tdb_file);
3576 if (!strncmp(argv[1], "0x", 2)) {
3577 key = hextodata(tmp_ctx, argv[1] + 2);
3578 if (key.dsize == 0) {
3579 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
3583 key.dptr = discard_const(argv[1]);
3584 key.dsize = strlen(argv[1]);
3587 data = tdb_fetch(tdb, key);
3588 if (data.dptr == NULL || data.dsize < sizeof(struct ctdb_ltdb_header)) {
3589 printf("Failed to read record %s from tdb %s\n", argv[1], tdb_file);
3597 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3599 printf("Failed to open output file %s\n", argv[2]);
3602 if (options.verbose){
3603 write(fd, data.dptr, data.dsize);
3605 write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3609 if (options.verbose){
3610 write(1, data.dptr, data.dsize);
3612 write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3616 talloc_free(tmp_ctx);
3621 store a record and header to a tdb-file
3623 static int control_tstore(struct ctdb_context *ctdb, int argc, const char **argv)
3625 const char *tdb_file;
3628 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
3636 tdb = tdb_open(tdb_file, 0, 0, O_RDWR, 0);
3638 printf("Failed to open TDB file %s\n", tdb_file);
3642 if (!strncmp(argv[1], "0x", 2)) {
3643 key = hextodata(tmp_ctx, argv[1] + 2);
3644 if (key.dsize == 0) {
3645 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
3649 key.dptr = discard_const(argv[1]);
3650 key.dsize = strlen(argv[1]);
3653 if (!strncmp(argv[2], "0x", 2)) {
3654 data = hextodata(tmp_ctx, argv[2] + 2);
3655 if (data.dsize == 0) {
3656 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[2]);
3660 data.dptr = discard_const(argv[2]);
3661 data.dsize = strlen(argv[2]);
3664 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
3665 printf("Not enough data. You must specify the full ctdb_ltdb_header too when storing\n");
3668 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0) {
3669 printf("Failed to write record %s to tdb %s\n", argv[1], tdb_file);
3676 talloc_free(tmp_ctx);
3681 write a record to a persistent database
3683 static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv)
3685 const char *db_name;
3686 struct ctdb_db_context *ctdb_db;
3687 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3688 struct ctdb_transaction_handle *h;
3694 talloc_free(tmp_ctx);
3698 fd = open(argv[2], O_RDONLY);
3700 DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s %s\n", argv[2], strerror(errno)));
3701 talloc_free(tmp_ctx);
3705 ret = fstat(fd, &st);
3707 DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
3709 talloc_free(tmp_ctx);
3713 if (!S_ISREG(st.st_mode)) {
3714 DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
3716 talloc_free(tmp_ctx);
3720 data.dsize = st.st_size;
3721 if (data.dsize == 0) {
3724 data.dptr = talloc_size(tmp_ctx, data.dsize);
3725 if (data.dptr == NULL) {
3726 DEBUG(DEBUG_ERR,("Failed to talloc %d of memory to store record data\n", (int)data.dsize));
3728 talloc_free(tmp_ctx);
3731 ret = read(fd, data.dptr, data.dsize);
3732 if (ret != data.dsize) {
3733 DEBUG(DEBUG_ERR,("Failed to read %d bytes of record data\n", (int)data.dsize));
3735 talloc_free(tmp_ctx);
3744 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
3745 if (ctdb_db == NULL) {
3746 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3747 talloc_free(tmp_ctx);
3751 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3753 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3754 talloc_free(tmp_ctx);
3758 key.dptr = discard_const(argv[1]);
3759 key.dsize = strlen(argv[1]);
3760 ret = ctdb_transaction_store(h, key, data);
3762 DEBUG(DEBUG_ERR,("Failed to store record\n"));
3763 talloc_free(tmp_ctx);
3767 ret = ctdb_transaction_commit(h);
3769 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
3770 talloc_free(tmp_ctx);
3775 talloc_free(tmp_ctx);
3780 check if a service is bound to a port or not
3782 static int control_chktcpport(struct ctdb_context *ctdb, int argc, const char **argv)
3787 struct sockaddr_in sin;
3790 printf("Use: ctdb chktcport <port>\n");
3794 port = atoi(argv[0]);
3796 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
3798 printf("Failed to open local socket\n");
3802 v = fcntl(s, F_GETFL, 0);
3803 fcntl(s, F_SETFL, v | O_NONBLOCK);
3805 bzero(&sin, sizeof(sin));
3806 sin.sin_family = PF_INET;
3807 sin.sin_port = htons(port);
3808 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
3811 printf("Failed to bind to local socket: %d %s\n", errno, strerror(errno));
3820 static void log_handler(struct ctdb_context *ctdb, uint64_t srvid,
3821 TDB_DATA data, void *private_data)
3823 DEBUG(DEBUG_ERR,("Log data received\n"));
3824 if (data.dsize > 0) {
3825 printf("%s", data.dptr);
3832 display a list of log messages from the in memory ringbuffer
3834 static int control_getlog(struct ctdb_context *ctdb, int argc, const char **argv)
3838 struct ctdb_get_log_addr log_addr;
3840 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3845 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
3846 talloc_free(tmp_ctx);
3850 log_addr.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
3851 log_addr.srvid = getpid();
3852 if (isalpha(argv[0][0]) || argv[0][0] == '-') {
3853 log_addr.level = get_debug_by_desc(argv[0]);
3855 log_addr.level = strtol(argv[0], NULL, 0);
3859 data.dptr = (unsigned char *)&log_addr;
3860 data.dsize = sizeof(log_addr);
3862 DEBUG(DEBUG_ERR, ("Pulling logs from node %u\n", options.pnn));
3864 ctdb_client_set_message_handler(ctdb, log_addr.srvid, log_handler, NULL);
3867 DEBUG(DEBUG_ERR,("Listen for response on %d\n", (int)log_addr.srvid));
3869 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_GET_LOG,
3870 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
3871 if (ret != 0 || res != 0) {
3872 DEBUG(DEBUG_ERR,("Failed to get logs - %s\n", errmsg));
3873 talloc_free(tmp_ctx);
3878 tv = timeval_current();
3879 /* this loop will terminate when we have received the reply */
3880 while (timeval_elapsed(&tv) < 3.0) {
3881 event_loop_once(ctdb->ev);
3884 DEBUG(DEBUG_INFO,("Timed out waiting for log data.\n"));
3886 talloc_free(tmp_ctx);
3891 clear the in memory log area
3893 static int control_clearlog(struct ctdb_context *ctdb, int argc, const char **argv)
3898 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3900 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_CLEAR_LOG,
3901 0, tdb_null, tmp_ctx, NULL, &res, NULL, &errmsg);
3902 if (ret != 0 || res != 0) {
3903 DEBUG(DEBUG_ERR,("Failed to clear logs\n"));
3904 talloc_free(tmp_ctx);
3908 talloc_free(tmp_ctx);
3915 display a list of the databases on a remote ctdb
3917 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
3920 struct ctdb_dbid_map *dbmap=NULL;
3922 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
3924 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
3928 if(options.machinereadable){
3929 printf(":ID:Name:Path:Persistent:Unhealthy:ReadOnly:\n");
3930 for(i=0;i<dbmap->num;i++){
3937 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
3938 dbmap->dbs[i].dbid, ctdb, &path);
3939 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
3940 dbmap->dbs[i].dbid, ctdb, &name);
3941 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
3942 dbmap->dbs[i].dbid, ctdb, &health);
3943 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
3944 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
3945 printf(":0x%08X:%s:%s:%d:%d:%d:\n",
3946 dbmap->dbs[i].dbid, name, path,
3947 !!(persistent), !!(health), !!(readonly));
3952 printf("Number of databases:%d\n", dbmap->num);
3953 for(i=0;i<dbmap->num;i++){
3960 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
3961 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
3962 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
3963 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
3964 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
3965 printf("dbid:0x%08x name:%s path:%s%s%s%s\n",
3966 dbmap->dbs[i].dbid, name, path,
3967 persistent?" PERSISTENT":"",
3968 readonly?" READONLY":"",
3969 health?" UNHEALTHY":"");
3976 display the status of a database on a remote ctdb
3978 static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
3981 struct ctdb_dbid_map *dbmap=NULL;
3982 const char *db_name;
3990 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
3992 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
3996 for(i=0;i<dbmap->num;i++){
4003 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
4004 if (strcmp(name, db_name) != 0) {
4008 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
4009 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
4010 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4011 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4012 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nREADONLY: %s\nHEALTH: %s\n",
4013 dbmap->dbs[i].dbid, name, path,
4014 persistent?"yes":"no",
4015 readonly?"yes":"no",
4016 health?health:"OK");
4020 DEBUG(DEBUG_ERR, ("db %s doesn't exist on node %u\n", db_name, options.pnn));
4025 check if the local node is recmaster or not
4026 it will return 1 if this node is the recmaster and 0 if it is not
4027 or if the local ctdb daemon could not be contacted
4029 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
4031 uint32_t mypnn, recmaster;
4033 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
4035 printf("Failed to get pnn of node\n");
4039 if (!ctdb_getrecmaster(ctdb_connection, options.pnn, &recmaster)) {
4040 printf("Failed to get the recmaster\n");
4044 if (recmaster != mypnn) {
4045 printf("this node is not the recmaster\n");
4049 printf("this node is the recmaster\n");
4056 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
4059 struct timeval tv = timeval_current();
4060 ret = ctdb_ctrl_ping(ctdb, options.pnn);
4062 printf("Unable to get ping response from node %u\n", options.pnn);
4065 printf("response from %u time=%.6f sec (%d clients)\n",
4066 options.pnn, timeval_elapsed(&tv), ret);
4075 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
4086 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
4088 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
4092 printf("%-23s = %u\n", name, value);
4099 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
4110 value = strtoul(argv[1], NULL, 0);
4112 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
4114 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
4123 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
4129 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
4131 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
4135 for (i=0;i<count;i++) {
4136 control_getvar(ctdb, 1, &list[i]);
4145 display debug level on a node
4147 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4152 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
4154 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
4157 if (options.machinereadable){
4158 printf(":Name:Level:\n");
4159 printf(":%s:%d:\n",get_debug_by_level(level),level);
4161 printf("Node %u is at debug level %s (%d)\n", options.pnn, get_debug_by_level(level), level);
4168 display reclock file of a node
4170 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4173 const char *reclock;
4175 ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
4177 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4180 if (options.machinereadable){
4181 if (reclock != NULL) {
4182 printf("%s", reclock);
4185 if (reclock == NULL) {
4186 printf("No reclock file used.\n");
4188 printf("Reclock file:%s\n", reclock);
4196 set the reclock file of a node
4198 static int control_setreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4201 const char *reclock;
4205 } else if (argc == 1) {
4211 ret = ctdb_ctrl_setreclock(ctdb, TIMELIMIT(), options.pnn, reclock);
4213 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4220 set the natgw state on/off
4222 static int control_setnatgwstate(struct ctdb_context *ctdb, int argc, const char **argv)
4225 uint32_t natgwstate;
4231 if (!strcmp(argv[0], "on")) {
4233 } else if (!strcmp(argv[0], "off")) {
4239 ret = ctdb_ctrl_setnatgwstate(ctdb, TIMELIMIT(), options.pnn, natgwstate);
4241 DEBUG(DEBUG_ERR, ("Unable to set the natgw state for node %u\n", options.pnn));
4249 set the lmaster role on/off
4251 static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4254 uint32_t lmasterrole;
4260 if (!strcmp(argv[0], "on")) {
4262 } else if (!strcmp(argv[0], "off")) {
4268 ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
4270 DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
4278 set the recmaster role on/off
4280 static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4283 uint32_t recmasterrole;
4289 if (!strcmp(argv[0], "on")) {
4291 } else if (!strcmp(argv[0], "off")) {
4297 ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
4299 DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
4307 set debug level on a node or all nodes
4309 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4315 printf("You must specify the debug level. Valid levels are:\n");
4316 for (i=0; debug_levels[i].description != NULL; i++) {
4317 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
4323 if (isalpha(argv[0][0]) || argv[0][0] == '-') {
4324 level = get_debug_by_desc(argv[0]);
4326 level = strtol(argv[0], NULL, 0);
4329 for (i=0; debug_levels[i].description != NULL; i++) {
4330 if (level == debug_levels[i].level) {
4334 if (debug_levels[i].description == NULL) {
4335 printf("Invalid debug level, must be one of\n");
4336 for (i=0; debug_levels[i].description != NULL; i++) {
4337 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
4342 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
4344 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
4353 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
4359 priority = strtol(argv[0], NULL, 0);
4363 DEBUG(DEBUG_ERR,("Thaw by priority %u\n", priority));
4365 ret = ctdb_ctrl_thaw_priority(ctdb, TIMELIMIT(), options.pnn, priority);
4367 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
4374 attach to a database
4376 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
4378 const char *db_name;
4379 struct ctdb_db_context *ctdb_db;
4380 bool persistent = false;
4390 if (strcmp(argv[1], "persistent") != 0) {
4396 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4397 if (ctdb_db == NULL) {
4398 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4408 static int control_setdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
4410 struct ctdb_db_priority db_prio;
4417 db_prio.db_id = strtoul(argv[0], NULL, 0);
4418 db_prio.priority = strtoul(argv[1], NULL, 0);
4420 ret = ctdb_ctrl_set_db_priority(ctdb, TIMELIMIT(), options.pnn, &db_prio);
4422 DEBUG(DEBUG_ERR,("Unable to set db prio\n"));
4432 static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
4434 uint32_t db_id, priority;
4441 db_id = strtoul(argv[0], NULL, 0);
4443 ret = ctdb_ctrl_get_db_priority(ctdb, TIMELIMIT(), options.pnn, db_id, &priority);
4445 DEBUG(DEBUG_ERR,("Unable to get db prio\n"));
4449 DEBUG(DEBUG_ERR,("Priority:%u\n", priority));
4455 set the readonly capability for a database
4457 static int control_setdbreadonly(struct ctdb_context *ctdb, int argc, const char **argv)
4459 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4461 struct ctdb_dbid_map *dbmap=NULL;
4468 if (!strncmp(argv[0], "0x", 2)) {
4469 db_id = strtoul(argv[0] + 2, NULL, 0);
4471 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
4473 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4474 talloc_free(tmp_ctx);
4477 for(i=0;i<dbmap->num;i++){
4480 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
4481 if(!strcmp(argv[0], name)){
4482 talloc_free(discard_const(name));
4485 talloc_free(discard_const(name));
4487 if (i == dbmap->num) {
4488 DEBUG(DEBUG_ERR,("No database with name '%s' found\n", argv[0]));
4489 talloc_free(tmp_ctx);
4492 db_id = dbmap->dbs[i].dbid;
4495 ret = ctdb_ctrl_set_db_readonly(ctdb, options.pnn, db_id);
4497 DEBUG(DEBUG_ERR,("Unable to set db to support readonly\n"));
4498 talloc_free(tmp_ctx);
4502 talloc_free(tmp_ctx);
4509 static int control_getdbseqnum(struct ctdb_context *ctdb, int argc, const char **argv)
4519 db_id = strtoul(argv[0], NULL, 0);
4521 ret = ctdb_getdbseqnum(ctdb_connection, options.pnn, db_id, &seqnum);
4523 DEBUG(DEBUG_ERR, ("Unable to get seqnum from node."));
4527 printf("Sequence number:%lld\n", (long long)seqnum);
4533 run an eventscript on a node
4535 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
4541 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4544 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4548 data.dptr = (unsigned char *)discard_const(argv[0]);
4549 data.dsize = strlen((char *)data.dptr) + 1;
4551 DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
4553 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
4554 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
4555 if (ret != 0 || res != 0) {
4556 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
4557 talloc_free(tmp_ctx);
4560 talloc_free(tmp_ctx);
4564 #define DB_VERSION 1
4565 #define MAX_DB_NAME 64
4566 struct db_file_header {
4567 unsigned long version;
4569 unsigned long persistent;
4571 const char name[MAX_DB_NAME];
4574 struct backup_data {
4575 struct ctdb_marshall_buffer *records;
4578 bool traverse_error;
4581 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
4583 struct backup_data *bd = talloc_get_type(private, struct backup_data);
4584 struct ctdb_rec_data *rec;
4586 /* add the record */
4587 rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
4589 bd->traverse_error = true;
4590 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
4593 bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
4594 if (bd->records == NULL) {
4595 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
4596 bd->traverse_error = true;
4599 bd->records->count++;
4600 memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
4601 bd->len += rec->length;
4609 * backup a database to a file
4611 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
4614 struct ctdb_dbid_map *dbmap=NULL;
4615 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4616 struct db_file_header dbhdr;
4617 struct ctdb_db_context *ctdb_db;
4618 struct backup_data *bd;
4621 const char *reason = NULL;
4624 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4628 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
4630 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4634 for(i=0;i<dbmap->num;i++){
4637 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
4638 if(!strcmp(argv[0], name)){
4639 talloc_free(discard_const(name));
4642 talloc_free(discard_const(name));
4644 if (i == dbmap->num) {
4645 DEBUG(DEBUG_ERR,("No database with name '%s' found\n", argv[0]));
4646 talloc_free(tmp_ctx);
4650 ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
4651 dbmap->dbs[i].dbid, tmp_ctx, &reason);
4653 DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
4655 talloc_free(tmp_ctx);
4659 uint32_t allow_unhealthy = 0;
4661 ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
4662 "AllowUnhealthyDBRead",
4665 if (allow_unhealthy != 1) {
4666 DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
4669 DEBUG(DEBUG_ERR,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
4671 talloc_free(tmp_ctx);
4675 DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
4677 DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
4678 "tunnable AllowUnhealthyDBRead = %u\n",
4682 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), argv[0], dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT, 0);
4683 if (ctdb_db == NULL) {
4684 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
4685 talloc_free(tmp_ctx);
4690 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
4692 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
4693 talloc_free(tmp_ctx);
4698 bd = talloc_zero(tmp_ctx, struct backup_data);
4700 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
4701 talloc_free(tmp_ctx);
4705 bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
4706 if (bd->records == NULL) {
4707 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
4708 talloc_free(tmp_ctx);
4712 bd->len = offsetof(struct ctdb_marshall_buffer, data);
4713 bd->records->db_id = ctdb_db->db_id;
4714 /* traverse the database collecting all records */
4715 if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
4716 bd->traverse_error) {
4717 DEBUG(DEBUG_ERR,("Traverse error\n"));
4718 talloc_free(tmp_ctx);
4722 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
4725 fh = open(argv[1], O_RDWR|O_CREAT, 0600);
4727 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
4728 talloc_free(tmp_ctx);
4732 dbhdr.version = DB_VERSION;
4733 dbhdr.timestamp = time(NULL);
4734 dbhdr.persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4735 dbhdr.size = bd->len;
4736 if (strlen(argv[0]) >= MAX_DB_NAME) {
4737 DEBUG(DEBUG_ERR,("Too long dbname\n"));
4740 strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME);
4741 ret = write(fh, &dbhdr, sizeof(dbhdr));
4743 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
4746 ret = write(fh, bd->records, bd->len);
4748 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
4757 DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
4761 DEBUG(DEBUG_ERR,("Database backed up to %s\n", argv[1]));
4763 talloc_free(tmp_ctx);
4768 * restore a database from a file
4770 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
4773 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4776 struct db_file_header dbhdr;
4777 struct ctdb_db_context *ctdb_db;
4778 struct ctdb_node_map *nodemap=NULL;
4779 struct ctdb_vnn_map *vnnmap=NULL;
4781 struct ctdb_control_wipe_database w;
4783 uint32_t generation;
4788 if (argc < 1 || argc > 2) {
4789 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4793 fh = open(argv[0], O_RDONLY);
4795 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
4796 talloc_free(tmp_ctx);
4800 read(fh, &dbhdr, sizeof(dbhdr));
4801 if (dbhdr.version != DB_VERSION) {
4802 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
4803 talloc_free(tmp_ctx);
4807 dbname = discard_const(dbhdr.name);
4809 dbname = discard_const(argv[1]);
4812 outdata.dsize = dbhdr.size;
4813 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
4814 if (outdata.dptr == NULL) {
4815 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
4817 talloc_free(tmp_ctx);
4820 read(fh, outdata.dptr, outdata.dsize);
4823 tm = localtime(&dbhdr.timestamp);
4824 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
4825 printf("Restoring database '%s' from backup @ %s\n",
4829 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), dbname, dbhdr.persistent, 0);
4830 if (ctdb_db == NULL) {
4831 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbname));
4832 talloc_free(tmp_ctx);
4836 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
4838 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
4839 talloc_free(tmp_ctx);
4844 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
4846 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
4847 talloc_free(tmp_ctx);
4851 /* freeze all nodes */
4852 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4853 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
4854 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
4860 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
4861 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4862 talloc_free(tmp_ctx);
4867 generation = vnnmap->generation;
4868 data.dptr = (void *)&generation;
4869 data.dsize = sizeof(generation);
4871 /* start a cluster wide transaction */
4872 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4873 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
4875 TIMELIMIT(), false, data,
4878 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
4883 w.db_id = ctdb_db->db_id;
4884 w.transaction_id = generation;
4886 data.dptr = (void *)&w;
4887 data.dsize = sizeof(w);
4889 /* wipe all the remote databases. */
4890 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4891 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
4893 TIMELIMIT(), false, data,
4896 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
4897 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4898 talloc_free(tmp_ctx);
4902 /* push the database */
4903 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4904 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
4906 TIMELIMIT(), false, outdata,
4909 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
4910 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4911 talloc_free(tmp_ctx);
4915 data.dptr = (void *)&ctdb_db->db_id;
4916 data.dsize = sizeof(ctdb_db->db_id);
4918 /* mark the database as healthy */
4919 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4920 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
4922 TIMELIMIT(), false, data,
4925 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
4926 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4927 talloc_free(tmp_ctx);
4931 data.dptr = (void *)&generation;
4932 data.dsize = sizeof(generation);
4934 /* commit all the changes */
4935 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
4937 TIMELIMIT(), false, data,
4940 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
4941 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4942 talloc_free(tmp_ctx);
4947 /* thaw all nodes */
4948 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4949 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
4955 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
4956 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4957 talloc_free(tmp_ctx);
4962 talloc_free(tmp_ctx);
4967 * dump a database backup from a file
4969 static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
4971 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4973 struct db_file_header dbhdr;
4977 struct ctdb_rec_data *rec = NULL;
4978 struct ctdb_marshall_buffer *m;
4979 struct ctdb_dump_db_context c;
4982 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4986 fh = open(argv[0], O_RDONLY);
4988 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
4989 talloc_free(tmp_ctx);
4993 read(fh, &dbhdr, sizeof(dbhdr));
4994 if (dbhdr.version != DB_VERSION) {
4995 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
4996 talloc_free(tmp_ctx);
5000 outdata.dsize = dbhdr.size;
5001 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5002 if (outdata.dptr == NULL) {
5003 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5005 talloc_free(tmp_ctx);
5008 read(fh, outdata.dptr, outdata.dsize);
5010 m = (struct ctdb_marshall_buffer *)outdata.dptr;
5012 tm = localtime(&dbhdr.timestamp);
5013 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5014 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
5015 dbhdr.name, m->db_id, tbuf);
5019 c.printemptyrecords = (bool)options.printemptyrecords;
5020 c.printdatasize = (bool)options.printdatasize;
5021 c.printlmaster = false;
5022 c.printhash = (bool)options.printhash;
5023 c.printrecordflags = (bool)options.printrecordflags;
5025 for (i=0; i < m->count; i++) {
5029 /* we do not want the header splitted, so we pass NULL*/
5030 rec = ctdb_marshall_loop_next(m, rec, &reqid,
5033 ctdb_dumpdb_record(ctdb, key, data, &c);
5036 printf("Dumped %d records\n", i);
5037 talloc_free(tmp_ctx);
5042 * wipe a database from a file
5044 static int control_wipedb(struct ctdb_context *ctdb, int argc,
5048 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5050 struct ctdb_db_context *ctdb_db;
5051 struct ctdb_node_map *nodemap = NULL;
5052 struct ctdb_vnn_map *vnnmap = NULL;
5054 struct ctdb_control_wipe_database w;
5056 uint32_t generation;
5057 struct ctdb_dbid_map *dbmap = NULL;
5060 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5064 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5067 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n",
5072 for(i=0;i<dbmap->num;i++){
5075 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
5076 dbmap->dbs[i].dbid, tmp_ctx, &name);
5077 if(!strcmp(argv[0], name)){
5078 talloc_free(discard_const(name));
5081 talloc_free(discard_const(name));
5083 if (i == dbmap->num) {
5084 DEBUG(DEBUG_ERR, ("No database with name '%s' found\n",
5086 talloc_free(tmp_ctx);
5090 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), argv[0], dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5091 if (ctdb_db == NULL) {
5092 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
5094 talloc_free(tmp_ctx);
5098 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
5101 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5103 talloc_free(tmp_ctx);
5107 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5110 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
5112 talloc_free(tmp_ctx);
5116 /* freeze all nodes */
5117 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5118 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5119 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5126 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5127 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
5128 CTDB_RECOVERY_ACTIVE);
5129 talloc_free(tmp_ctx);
5134 generation = vnnmap->generation;
5135 data.dptr = (void *)&generation;
5136 data.dsize = sizeof(generation);
5138 /* start a cluster wide transaction */
5139 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5140 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5142 TIMELIMIT(), false, data,
5146 DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
5147 "transactions.\n"));
5151 w.db_id = ctdb_db->db_id;
5152 w.transaction_id = generation;
5154 data.dptr = (void *)&w;
5155 data.dsize = sizeof(w);
5157 /* wipe all the remote databases. */
5158 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5159 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5161 TIMELIMIT(), false, data,
5164 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5165 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5166 talloc_free(tmp_ctx);
5170 data.dptr = (void *)&ctdb_db->db_id;
5171 data.dsize = sizeof(ctdb_db->db_id);
5173 /* mark the database as healthy */
5174 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5175 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5177 TIMELIMIT(), false, data,
5180 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5181 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5182 talloc_free(tmp_ctx);
5186 data.dptr = (void *)&generation;
5187 data.dsize = sizeof(generation);
5189 /* commit all the changes */
5190 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5192 TIMELIMIT(), false, data,
5195 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5196 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5197 talloc_free(tmp_ctx);
5201 /* thaw all nodes */
5202 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5203 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5209 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5210 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5211 talloc_free(tmp_ctx);
5215 DEBUG(DEBUG_ERR, ("Database wiped.\n"));
5217 talloc_free(tmp_ctx);
5224 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
5230 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5231 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
5232 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
5233 if (ret != 0 || res != 0) {
5234 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
5235 talloc_free(tmp_ctx);
5238 write(1, data.dptr, data.dsize);
5239 talloc_free(tmp_ctx);
5244 handler for memory dumps
5246 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid,
5247 TDB_DATA data, void *private_data)
5249 write(1, data.dptr, data.dsize);
5254 dump memory usage on the recovery daemon
5256 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
5260 struct rd_memdump_reply rd;
5262 rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
5264 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
5267 rd.srvid = getpid();
5269 /* register a message port for receiveing the reply so that we
5270 can receive the reply
5272 ctdb_client_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
5275 data.dptr = (uint8_t *)&rd;
5276 data.dsize = sizeof(rd);
5278 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
5280 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
5284 /* this loop will terminate when we have received the reply */
5286 event_loop_once(ctdb->ev);
5293 send a message to a srvid
5295 static int control_msgsend(struct ctdb_context *ctdb, int argc, const char **argv)
5297 unsigned long srvid;
5305 srvid = strtoul(argv[0], NULL, 0);
5307 data.dptr = (uint8_t *)discard_const(argv[1]);
5308 data.dsize= strlen(argv[1]);
5310 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, srvid, data);
5312 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
5320 handler for msglisten
5322 static void msglisten_handler(struct ctdb_context *ctdb, uint64_t srvid,
5323 TDB_DATA data, void *private_data)
5327 printf("Message received: ");
5328 for (i=0;i<data.dsize;i++) {
5329 printf("%c", data.dptr[i]);
5335 listen for messages on a messageport
5337 static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
5343 /* register a message port and listen for messages
5345 ctdb_client_set_message_handler(ctdb, srvid, msglisten_handler, NULL);
5346 printf("Listening for messages on srvid:%d\n", (int)srvid);
5349 event_loop_once(ctdb->ev);
5356 list all nodes in the cluster
5357 we parse the nodes file directly
5359 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
5361 TALLOC_CTX *mem_ctx = talloc_new(NULL);
5362 struct pnn_node *pnn_nodes;
5363 struct pnn_node *pnn_node;
5365 pnn_nodes = read_nodes_file(mem_ctx);
5366 if (pnn_nodes == NULL) {
5367 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
5368 talloc_free(mem_ctx);
5372 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
5373 ctdb_sock_addr addr;
5374 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
5375 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
5376 talloc_free(mem_ctx);
5379 if (options.machinereadable){
5380 printf(":%d:%s:\n", pnn_node->pnn, pnn_node->addr);
5382 printf("%s\n", pnn_node->addr);
5385 talloc_free(mem_ctx);
5391 reload the nodes file on the local node
5393 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
5397 struct ctdb_node_map *nodemap=NULL;
5399 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
5401 DEBUG(DEBUG_ERR, ("Failed to read pnn of local node\n"));
5405 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
5407 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
5411 /* reload the nodes file on all remote nodes */
5412 for (i=0;i<nodemap->num;i++) {
5413 if (nodemap->nodes[i].pnn == mypnn) {
5416 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
5417 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
5418 nodemap->nodes[i].pnn);
5420 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
5424 /* reload the nodes file on the local node */
5425 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
5426 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
5428 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
5431 /* initiate a recovery */
5432 control_recover(ctdb, argc, argv);
5438 static const struct {
5440 int (*fn)(struct ctdb_context *, int, const char **);
5442 bool without_daemon; /* can be run without daemon running ? */
5445 } ctdb_commands[] = {
5447 { "version", control_version, true, false, "show version of ctdb" },
5449 { "status", control_status, true, false, "show node status" },
5450 { "uptime", control_uptime, true, false, "show node uptime" },
5451 { "ping", control_ping, true, false, "ping all nodes" },
5452 { "getvar", control_getvar, true, false, "get a tunable variable", "<name>"},
5453 { "setvar", control_setvar, true, false, "set a tunable variable", "<name> <value>"},
5454 { "listvars", control_listvars, true, false, "list tunable variables"},
5455 { "statistics", control_statistics, false, false, "show statistics" },
5456 { "statisticsreset", control_statistics_reset, true, false, "reset statistics"},
5457 { "stats", control_stats, false, false, "show rolling statistics", "[number of history records]" },
5458 { "ip", control_ip, false, false, "show which public ip's that ctdb manages" },
5459 { "ipinfo", control_ipinfo, true, false, "show details about a public ip that ctdb manages", "<ip>" },
5460 { "ifaces", control_ifaces, true, false, "show which interfaces that ctdb manages" },
5461 { "setifacelink", control_setifacelink, true, false, "set interface link status", "<iface> <status>" },
5462 { "process-exists", control_process_exists, true, false, "check if a process exists on a node", "<pid>"},
5463 { "getdbmap", control_getdbmap, true, false, "show the database map" },
5464 { "getdbstatus", control_getdbstatus, true, false, "show the status of a database", "<dbname>" },
5465 { "catdb", control_catdb, true, false, "dump a ctdb database" , "<dbname>"},
5466 { "cattdb", control_cattdb, true, false, "dump a local tdb database" , "<dbname>"},
5467 { "getmonmode", control_getmonmode, true, false, "show monitoring mode" },
5468 { "getcapabilities", control_getcapabilities, true, false, "show node capabilities" },
5469 { "pnn", control_pnn, true, false, "show the pnn of the currnet node" },
5470 { "lvs", control_lvs, true, false, "show lvs configuration" },
5471 { "lvsmaster", control_lvsmaster, true, false, "show which node is the lvs master" },
5472 { "disablemonitor", control_disable_monmode,true, false, "set monitoring mode to DISABLE" },
5473 { "enablemonitor", control_enable_monmode, true, false, "set monitoring mode to ACTIVE" },
5474 { "setdebug", control_setdebug, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
5475 { "getdebug", control_getdebug, true, false, "get debug level" },
5476 { "getlog", control_getlog, true, false, "get the log data from the in memory ringbuffer", "<level>" },
5477 { "clearlog", control_clearlog, true, false, "clear the log data from the in memory ringbuffer" },
5478 { "attach", control_attach, true, false, "attach to a database", "<dbname> [persistent]" },
5479 { "dumpmemory", control_dumpmemory, true, false, "dump memory map to stdout" },
5480 { "rddumpmemory", control_rddumpmemory, true, false, "dump memory map from the recovery daemon to stdout" },
5481 { "getpid", control_getpid, true, false, "get ctdbd process ID" },
5482 { "disable", control_disable, true, false, "disable a nodes public IP" },
5483 { "enable", control_enable, true, false, "enable a nodes public IP" },
5484 { "stop", control_stop, true, false, "stop a node" },
5485 { "continue", control_continue, true, false, "re-start a stopped node" },
5486 { "ban", control_ban, true, false, "ban a node from the cluster", "<bantime|0>"},
5487 { "unban", control_unban, true, false, "unban a node" },
5488 { "showban", control_showban, true, false, "show ban information"},
5489 { "shutdown", control_shutdown, true, false, "shutdown ctdbd" },
5490 { "recover", control_recover, true, false, "force recovery" },
5491 { "sync", control_ipreallocate, true, false, "wait until ctdbd has synced all state changes" },
5492 { "ipreallocate", control_ipreallocate, true, false, "force the recovery daemon to perform a ip reallocation procedure" },
5493 { "thaw", control_thaw, true, false, "thaw databases", "[priority:1-3]" },
5494 { "isnotrecmaster", control_isnotrecmaster, false, false, "check if the local node is recmaster or not" },
5495 { "killtcp", kill_tcp, false, false, "kill a tcp connection.", "<srcip:port> <dstip:port>" },
5496 { "gratiousarp", control_gratious_arp, false, false, "send a gratious arp", "<ip> <interface>" },
5497 { "tickle", tickle_tcp, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
5498 { "gettickles", control_get_tickles, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
5499 { "addtickle", control_add_tickle, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
5501 { "deltickle", control_del_tickle, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
5503 { "regsrvid", regsrvid, false, false, "register a server id", "<pnn> <type> <id>" },
5504 { "unregsrvid", unregsrvid, false, false, "unregister a server id", "<pnn> <type> <id>" },
5505 { "chksrvid", chksrvid, false, false, "check if a server id exists", "<pnn> <type> <id>" },
5506 { "getsrvids", getsrvids, false, false, "get a list of all server ids"},
5507 { "check_srvids", check_srvids, false, false, "check if a srvid exists", "<id>+" },
5508 { "vacuum", ctdb_vacuum, false, true, "vacuum the databases of empty records", "[max_records]"},
5509 { "repack", ctdb_repack, false, false, "repack all databases", "[max_freelist]"},
5510 { "listnodes", control_listnodes, false, true, "list all nodes in the cluster"},
5511 { "reloadnodes", control_reload_nodes_file, false, false, "reload the nodes file and restart the transport on all nodes"},
5512 { "moveip", control_moveip, false, false, "move/failover an ip address to another node", "<ip> <node>"},
5513 { "addip", control_addip, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
5514 { "delip", control_delip, false, false, "delete an ip address from a node", "<ip>"},
5515 { "eventscript", control_eventscript, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
5516 { "backupdb", control_backupdb, false, false, "backup the database into a file.", "<database> <file>"},
5517 { "restoredb", control_restoredb, false, false, "restore the database from a file.", "<file> [dbname]"},
5518 { "dumpdbbackup", control_dumpdbbackup, false, true, "dump database backup from a file.", "<file>"},
5519 { "wipedb", control_wipedb, false, false, "wipe the contents of a database.", "<dbname>"},
5520 { "recmaster", control_recmaster, false, false, "show the pnn for the recovery master."},
5521 { "scriptstatus", control_scriptstatus, true, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
5522 { "enablescript", control_enablescript, false, false, "enable an eventscript", "<script>"},
5523 { "disablescript", control_disablescript, false, false, "disable an eventscript", "<script>"},
5524 { "natgwlist", control_natgwlist, false, false, "show the nodes belonging to this natgw configuration"},
5525 { "xpnn", control_xpnn, true, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
5526 { "getreclock", control_getreclock, false, false, "Show the reclock file of a node"},
5527 { "setreclock", control_setreclock, false, false, "Set/clear the reclock file of a node", "[filename]"},
5528 { "setnatgwstate", control_setnatgwstate, false, false, "Set NATGW state to on/off", "{on|off}"},
5529 { "setlmasterrole", control_setlmasterrole, false, false, "Set LMASTER role to on/off", "{on|off}"},
5530 { "setrecmasterrole", control_setrecmasterrole, false, false, "Set RECMASTER role to on/off", "{on|off}"},
5531 { "setdbprio", control_setdbprio, false, false, "Set DB priority", "<dbid> <prio:1-3>"},
5532 { "getdbprio", control_getdbprio, false, false, "Get DB priority", "<dbid>"},
5533 { "setdbreadonly", control_setdbreadonly, false, false, "Set DB readonly capable", "<dbid>"},
5534 { "msglisten", control_msglisten, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
5535 { "msgsend", control_msgsend, false, false, "Send a message to srvid", "<srvid> <message>"},
5536 { "sync", control_ipreallocate, false, false, "wait until ctdbd has synced all state changes" },
5537 { "pfetch", control_pfetch, false, false, "fetch a record from a persistent database", "<db> <key> [<file>]" },
5538 { "pstore", control_pstore, false, false, "write a record to a persistent database", "<db> <key> <file containing record>" },
5539 { "tfetch", control_tfetch, false, true, "fetch a record from a [c]tdb-file [-v]", "<tdb-file> <key> [<file>]" },
5540 { "tstore", control_tstore, false, true, "store a record (including ltdb header)", "<tdb-file> <key> <data+header>" },
5541 { "readkey", control_readkey, true, false, "read the content off a database key", "<tdb-file> <key>" },
5542 { "writekey", control_writekey, true, false, "write to a database key", "<tdb-file> <key> <value>" },
5543 { "checktcpport", control_chktcpport, false, true, "check if a service is bound to a specific tcp port or not", "<port>" },
5544 { "rebalancenode", control_rebalancenode, false, false, "release a node by allowing it to takeover ips", "<pnn>"},
5545 { "getdbseqnum", control_getdbseqnum, false, false, "get the sequence number off a database", "<dbid>" },
5546 { "nodestatus", control_nodestatus, true, false, "show and return node status" },
5547 { "dbstatistics", control_dbstatistics, false, false, "show db statistics", "<db>" },
5553 static void usage(void)
5557 "Usage: ctdb [options] <control>\n" \
5559 " -n <node> choose node number, or 'all' (defaults to local node)\n"
5560 " -Y generate machinereadable output\n"
5561 " -v generate verbose output\n"
5562 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
5563 printf("Controls:\n");
5564 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
5565 printf(" %-15s %-27s %s\n",
5566 ctdb_commands[i].name,
5567 ctdb_commands[i].args?ctdb_commands[i].args:"",
5568 ctdb_commands[i].msg);
5574 static void ctdb_alarm(int sig)
5576 printf("Maximum runtime exceeded - exiting\n");
5583 int main(int argc, const char *argv[])
5585 struct ctdb_context *ctdb;
5586 char *nodestring = NULL;
5587 struct poptOption popt_options[] = {
5590 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
5591 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
5592 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
5593 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
5594 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
5595 { "print-emptyrecords", 0, POPT_ARG_NONE, &options.printemptyrecords, 0, "print the empty records when dumping databases (catdb, cattdb, dumpdbbackup)", NULL },
5596 { "print-datasize", 0, POPT_ARG_NONE, &options.printdatasize, 0, "do not print record data when dumping databases, only the data size", NULL },
5597 { "print-lmaster", 0, POPT_ARG_NONE, &options.printlmaster, 0, "print the record's lmaster in catdb", NULL },
5598 { "print-hash", 0, POPT_ARG_NONE, &options.printhash, 0, "print the record's hash when dumping databases", NULL },
5599 { "print-recordflags", 0, POPT_ARG_NONE, &options.printrecordflags, 0, "print the record flags in catdb and dumpdbbackup", NULL },
5603 const char **extra_argv;
5607 struct event_context *ev;
5608 const char *control;
5609 const char *socket_name;
5613 /* set some defaults */
5614 options.maxruntime = 0;
5615 options.timelimit = 3;
5616 options.pnn = CTDB_CURRENT_NODE;
5618 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
5620 while ((opt = poptGetNextOpt(pc)) != -1) {
5623 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
5624 poptBadOption(pc, 0), poptStrerror(opt)));
5629 /* setup the remaining options for the main program to use */
5630 extra_argv = poptGetArgs(pc);
5633 while (extra_argv[extra_argc]) extra_argc++;
5636 if (extra_argc < 1) {
5640 if (options.maxruntime == 0) {
5641 const char *ctdb_timeout;
5642 ctdb_timeout = getenv("CTDB_TIMEOUT");
5643 if (ctdb_timeout != NULL) {
5644 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
5646 /* default timeout is 120 seconds */
5647 options.maxruntime = 120;
5651 signal(SIGALRM, ctdb_alarm);
5652 alarm(options.maxruntime);
5654 control = extra_argv[0];
5656 ev = event_context_init(NULL);
5658 DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
5661 tevent_loop_allow_nesting(ev);
5663 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
5664 if (strcmp(control, ctdb_commands[i].name) == 0) {
5669 if (i == ARRAY_SIZE(ctdb_commands)) {
5670 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
5674 if (ctdb_commands[i].without_daemon == true) {
5675 if (nodestring != NULL) {
5676 DEBUG(DEBUG_ERR, ("Can't specify node(s) with \"ctdb %s\"\n", control));
5680 return ctdb_commands[i].fn(NULL, extra_argc-1, extra_argv+1);
5683 /* initialise ctdb */
5684 ctdb = ctdb_cmdline_client(ev, TIMELIMIT());
5687 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
5691 /* initialize a libctdb connection as well */
5692 socket_name = ctdb_get_socketname(ctdb);
5693 ctdb_connection = ctdb_connect(socket_name,
5694 ctdb_log_file, stderr);
5695 if (ctdb_connection == NULL) {
5696 DEBUG(DEBUG_ERR, ("Failed to connect to daemon from libctdb\n"));
5700 /* setup the node number(s) to contact */
5701 if (!parse_nodestring(ctdb, nodestring, CTDB_CURRENT_NODE, false,
5702 &options.nodes, &options.pnn)) {
5706 if (options.pnn == CTDB_CURRENT_NODE) {
5707 options.pnn = options.nodes[0];
5710 if (ctdb_commands[i].auto_all &&
5711 ((options.pnn == CTDB_BROADCAST_ALL) ||
5712 (options.pnn == CTDB_MULTICAST))) {
5716 for (j = 0; j < talloc_array_length(options.nodes); j++) {
5717 options.pnn = options.nodes[j];
5718 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
5721 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
5724 ctdb_disconnect(ctdb_connection);
5726 (void)poptFreeContext(pc);