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/events/events.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_private.h"
32 static void usage(void);
41 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
44 see if a process exists
46 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
54 if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
55 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
59 ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
61 printf("%u:%u exists\n", pnn, pid);
63 printf("%u:%u does not exist\n", pnn, pid);
69 display statistics structure
71 static void show_statistics(struct ctdb_statistics *s)
73 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
75 const char *prefix=NULL;
81 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
82 STATISTICS_FIELD(num_clients),
83 STATISTICS_FIELD(frozen),
84 STATISTICS_FIELD(recovering),
85 STATISTICS_FIELD(client_packets_sent),
86 STATISTICS_FIELD(client_packets_recv),
87 STATISTICS_FIELD(node_packets_sent),
88 STATISTICS_FIELD(node_packets_recv),
89 STATISTICS_FIELD(keepalive_packets_sent),
90 STATISTICS_FIELD(keepalive_packets_recv),
91 STATISTICS_FIELD(node.req_call),
92 STATISTICS_FIELD(node.reply_call),
93 STATISTICS_FIELD(node.req_dmaster),
94 STATISTICS_FIELD(node.reply_dmaster),
95 STATISTICS_FIELD(node.reply_error),
96 STATISTICS_FIELD(node.req_message),
97 STATISTICS_FIELD(node.req_control),
98 STATISTICS_FIELD(node.reply_control),
99 STATISTICS_FIELD(client.req_call),
100 STATISTICS_FIELD(client.req_message),
101 STATISTICS_FIELD(client.req_control),
102 STATISTICS_FIELD(timeouts.call),
103 STATISTICS_FIELD(timeouts.control),
104 STATISTICS_FIELD(timeouts.traverse),
105 STATISTICS_FIELD(total_calls),
106 STATISTICS_FIELD(pending_calls),
107 STATISTICS_FIELD(lockwait_calls),
108 STATISTICS_FIELD(pending_lockwait_calls),
109 STATISTICS_FIELD(memory_used),
110 STATISTICS_FIELD(max_hop_count),
112 printf("CTDB version %u\n", CTDB_VERSION);
113 for (i=0;i<ARRAY_SIZE(fields);i++) {
114 if (strchr(fields[i].name, '.')) {
115 preflen = strcspn(fields[i].name, ".")+1;
116 if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
117 prefix = fields[i].name;
118 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
123 printf(" %*s%-22s%*s%10u\n",
125 fields[i].name+preflen,
127 *(uint32_t *)(fields[i].offset+(uint8_t *)s));
129 printf(" %-30s %.6f sec\n", "max_call_latency", s->max_call_latency);
130 printf(" %-30s %.6f sec\n", "max_lockwait_latency", s->max_lockwait_latency);
131 talloc_free(tmp_ctx);
135 display remote ctdb statistics combined from all nodes
137 static int control_statistics_all(struct ctdb_context *ctdb)
140 struct ctdb_statistics statistics;
144 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
145 CTDB_NO_MEMORY(ctdb, nodes);
147 ZERO_STRUCT(statistics);
149 for (i=0;i<num_nodes;i++) {
150 struct ctdb_statistics s1;
152 uint32_t *v1 = (uint32_t *)&s1;
153 uint32_t *v2 = (uint32_t *)&statistics;
155 offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
156 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
158 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
161 for (j=0;j<num_ints;j++) {
164 statistics.max_hop_count =
165 MAX(statistics.max_hop_count, s1.max_hop_count);
166 statistics.max_call_latency =
167 MAX(statistics.max_call_latency, s1.max_call_latency);
168 statistics.max_lockwait_latency =
169 MAX(statistics.max_lockwait_latency, s1.max_lockwait_latency);
172 printf("Gathered statistics for %u nodes\n", num_nodes);
173 show_statistics(&statistics);
178 display remote ctdb statistics
180 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
183 struct ctdb_statistics statistics;
185 if (options.pnn == CTDB_BROADCAST_ALL) {
186 return control_statistics_all(ctdb);
189 ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
191 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
194 show_statistics(&statistics);
200 reset remote ctdb statistics
202 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
206 ret = ctdb_statistics_reset(ctdb, options.pnn);
208 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
216 display uptime of remote node
218 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
222 struct ctdb_uptime *uptime = NULL;
223 int tmp, days, hours, minutes, seconds;
225 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
230 ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
232 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
236 if (options.machinereadable){
237 printf(":Current Node Time:Ctdb Start Time:Last Recovery Time:\n");
238 printf(":%u:%u:%u:\n",
239 (unsigned int)uptime->current_time.tv_sec,
240 (unsigned int)uptime->ctdbd_start_time.tv_sec,
241 (unsigned int)uptime->last_recovery_time.tv_sec);
245 printf("Current time of node : %s", ctime(&uptime->current_time.tv_sec));
247 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
255 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
257 tmp = uptime->current_time.tv_sec - uptime->last_recovery_time.tv_sec;
265 printf("Time of last recovery : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_time.tv_sec));
271 display remote ctdb status
273 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
276 struct ctdb_vnn_map *vnnmap=NULL;
277 struct ctdb_node_map *nodemap=NULL;
278 uint32_t recmode, recmaster;
281 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
286 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
288 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
292 if(options.machinereadable){
293 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:\n");
294 for(i=0;i<nodemap->num;i++){
295 printf(":%d:%s:%d:%d:%d:%d:\n", nodemap->nodes[i].pnn,
296 inet_ntoa(nodemap->nodes[i].sin.sin_addr),
297 !!(nodemap->nodes[i].flags&NODE_FLAGS_DISCONNECTED),
298 !!(nodemap->nodes[i].flags&NODE_FLAGS_BANNED),
299 !!(nodemap->nodes[i].flags&NODE_FLAGS_PERMANENTLY_DISABLED),
300 !!(nodemap->nodes[i].flags&NODE_FLAGS_UNHEALTHY));
305 printf("Number of nodes:%d\n", nodemap->num);
306 for(i=0;i<nodemap->num;i++){
307 static const struct {
311 { NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
312 { NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
313 { NODE_FLAGS_BANNED, "BANNED" },
314 { NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
316 char *flags_str = NULL;
318 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
319 if (nodemap->nodes[i].flags & flag_names[j].flag) {
320 if (flags_str == NULL) {
321 flags_str = talloc_strdup(ctdb, flag_names[j].name);
323 flags_str = talloc_asprintf_append(flags_str, "|%s",
326 CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
329 if (flags_str == NULL) {
330 flags_str = talloc_strdup(ctdb, "OK");
331 CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
333 printf("pnn:%d %-16s %s%s\n", nodemap->nodes[i].pnn,
334 inet_ntoa(nodemap->nodes[i].sin.sin_addr),
336 nodemap->nodes[i].pnn == mypnn?" (THIS NODE)":"");
337 talloc_free(flags_str);
340 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &vnnmap);
342 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
345 if (vnnmap->generation == INVALID_GENERATION) {
346 printf("Generation:INVALID\n");
348 printf("Generation:%d\n",vnnmap->generation);
350 printf("Size:%d\n",vnnmap->size);
351 for(i=0;i<vnnmap->size;i++){
352 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
355 ret = ctdb_ctrl_getrecmode(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmode);
357 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
360 printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
362 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
364 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
367 printf("Recovery master:%d\n",recmaster);
373 get a list of all tickles for this pnn
375 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
377 struct ctdb_control_tcp_tickle_list *list;
378 struct sockaddr_in ip;
385 ip.sin_family = AF_INET;
386 if (inet_aton(argv[0], &ip.sin_addr) == 0) {
387 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
391 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &ip, &list);
393 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
397 printf("Tickles for ip:%s\n", inet_ntoa(list->ip.sin_addr));
398 printf("Num tickles:%u\n", list->tickles.num);
399 for (i=0;i<list->tickles.num;i++) {
400 printf("SRC: %s:%u ", inet_ntoa(list->tickles.connections[i].saddr.sin_addr), ntohs(list->tickles.connections[i].saddr.sin_port));
401 printf("DST: %s:%u\n", inet_ntoa(list->tickles.connections[i].daddr.sin_addr), ntohs(list->tickles.connections[i].daddr.sin_port));
410 move/failover an ip address to a specific node
412 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
415 struct sockaddr_in ip;
417 struct ctdb_all_public_ips *ips;
418 struct ctdb_public_ip pip;
420 struct ctdb_node_map *nodemap=NULL;
427 ip.sin_family = AF_INET;
428 if (inet_aton(argv[0], &ip.sin_addr) == 0) {
429 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
434 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
436 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
440 if (sscanf(argv[1], "%u", &pnn) != 1) {
441 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
445 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, "DeterministicIPs", &value);
447 DEBUG(DEBUG_ERR, ("Unable to get tunable variable 'DeterministicIPs' from local node\n"));
451 DEBUG(DEBUG_ERR, ("The tunable 'DeterministicIPs' is set. You can only move ip addresses when this feature is disabled\n"));
455 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, "NoIPFailback", &value);
457 DEBUG(DEBUG_ERR, ("Unable to get tunable variable 'NoIPFailback' from local node\n"));
461 DEBUG(DEBUG_ERR, ("The tunable 'NoIPFailback' is NOT set. You can only move ip addresses when this feature is enabled\n"));
465 /* read the public ip list from the node */
466 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
468 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
472 for (i=0;i<ips->num;i++) {
473 if (ctdb_same_ip(&ip, &ips->ips[i].sin)) {
478 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
479 pnn, inet_ntoa(ip.sin_addr)));
482 if (ips->ips[i].pnn == pnn) {
483 DEBUG(DEBUG_ERR, ("Host %u is already hosting '%s'\n",
484 pnn, inet_ntoa(ips->ips[i].sin.sin_addr)));
488 /* send a moveip message to the recovery master */
490 pip.sin.sin_family = AF_INET;
491 pip.sin.sin_addr = ips->ips[i].sin.sin_addr;
492 data.dsize = sizeof(pip);
493 data.dptr = (unsigned char *)&pip;
496 /* send release ip to all nodes */
497 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
498 list_of_active_nodes(ctdb, nodemap, ctdb, true),
499 TIMELIMIT(), false, data) != 0) {
500 DEBUG(DEBUG_ERR, (__location__ " Unable to send 'ReleaseIP' to all nodes.\n"));
508 add a public ip address to a node
510 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
515 struct sockaddr_in addr;
516 struct ctdb_control_ip_iface *pub;
522 if (!parse_ip_mask(argv[0], &addr, &mask)) {
523 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
527 len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]);
528 pub = talloc_size(ctdb, len);
529 CTDB_NO_MEMORY(ctdb, pub);
533 pub->len = strlen(argv[1])+1;
534 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
536 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
538 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u\n", options.pnn));
546 delete a public ip address from a node
548 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
551 struct sockaddr_in addr;
552 struct ctdb_control_ip_iface pub;
558 addr.sin_family = AF_INET;
559 if (inet_aton(argv[0], &addr.sin_addr) == 0) {
560 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
568 ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
570 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
578 kill a tcp connection
580 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
583 struct ctdb_control_killtcp killtcp;
589 if (!parse_ip_port(argv[0], &killtcp.src)) {
590 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
594 if (!parse_ip_port(argv[1], &killtcp.dst)) {
595 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
599 ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
601 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
612 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
615 struct sockaddr_in sin;
621 sin.sin_family = AF_INET;
622 if (inet_aton(argv[0], &sin.sin_addr) == 0) {
623 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
627 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &sin, argv[1]);
629 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
639 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
642 struct ctdb_server_id server_id;
648 server_id.pnn = strtoul(argv[0], NULL, 0);
649 server_id.type = strtoul(argv[1], NULL, 0);
650 server_id.server_id = strtoul(argv[2], NULL, 0);
652 ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
654 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
661 unregister a server id
663 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
666 struct ctdb_server_id server_id;
672 server_id.pnn = strtoul(argv[0], NULL, 0);
673 server_id.type = strtoul(argv[1], NULL, 0);
674 server_id.server_id = strtoul(argv[2], NULL, 0);
676 ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
678 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
685 check if a server id exists
687 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
691 struct ctdb_server_id server_id;
697 server_id.pnn = strtoul(argv[0], NULL, 0);
698 server_id.type = strtoul(argv[1], NULL, 0);
699 server_id.server_id = strtoul(argv[2], NULL, 0);
701 ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
703 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
708 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
710 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
716 get a list of all server ids that are registered on a node
718 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
721 struct ctdb_server_id_list *server_ids;
723 ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
725 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
729 for (i=0; i<server_ids->num; i++) {
730 printf("Server id %d:%d:%d\n",
731 server_ids->server_ids[i].pnn,
732 server_ids->server_ids[i].type,
733 server_ids->server_ids[i].server_id);
740 send a tcp tickle ack
742 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
745 struct sockaddr_in src, dst;
751 if (!parse_ip_port(argv[0], &src)) {
752 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
756 if (!parse_ip_port(argv[1], &dst)) {
757 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
761 s = ctdb_sys_open_sending_socket();
763 DEBUG(DEBUG_ERR, ("Failed to open socket for sending tickle\n"));
767 ret = ctdb_sys_send_tcp(s, &src, &dst, 0, 0, 0);
772 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
779 display public ip status
781 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
784 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
785 struct ctdb_all_public_ips *ips;
787 /* read the public ip list from this node */
788 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
790 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
791 talloc_free(tmp_ctx);
795 if (options.machinereadable){
796 printf(":Public IP:Node:\n");
798 printf("Public IPs on node %u\n", options.pnn);
801 for (i=1;i<=ips->num;i++) {
802 if (options.machinereadable){
803 printf(":%s:%d:\n", inet_ntoa(ips->ips[ips->num-i].sin.sin_addr), ips->ips[ips->num-i].pnn);
805 printf("%s %d\n", inet_ntoa(ips->ips[ips->num-i].sin.sin_addr), ips->ips[ips->num-i].pnn);
809 talloc_free(tmp_ctx);
814 display pid of a ctdb daemon
816 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
821 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
823 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
826 printf("Pid:%d\n", pid);
832 disable a remote node
834 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
838 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, NODE_FLAGS_PERMANENTLY_DISABLED, 0);
840 DEBUG(DEBUG_ERR, ("Unable to disable node %u\n", options.pnn));
848 enable a disabled remote node
850 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
854 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, 0, NODE_FLAGS_PERMANENTLY_DISABLED);
856 DEBUG(DEBUG_ERR, ("Unable to enable node %u\n", options.pnn));
864 ban a node from the cluster
866 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
869 struct ctdb_ban_info b;
877 ban_time = strtoul(argv[0], NULL, 0);
880 b.ban_time = ban_time;
882 data.dptr = (uint8_t *)&b;
883 data.dsize = sizeof(b);
885 ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_BAN_NODE, data);
887 DEBUG(DEBUG_ERR,("Failed to ban node %u\n", options.pnn));
896 unban a node from the cluster
898 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
903 data.dptr = (uint8_t *)&options.pnn;
904 data.dsize = sizeof(uint32_t);
906 ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_UNBAN_NODE, data);
908 DEBUG(DEBUG_ERR,("Failed to to unban node %u\n", options.pnn));
919 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
923 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
925 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
935 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
939 ret = ctdb_ctrl_freeze(ctdb, TIMELIMIT(), options.pnn);
941 DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
945 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
947 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
956 display monitoring mode of a remote node
958 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
963 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
965 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
968 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
975 disable monitoring on a node
977 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
982 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
984 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
987 printf("Monitoring mode:%s\n","DISABLED");
993 enable monitoring on a node
995 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
1000 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
1002 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
1005 printf("Monitoring mode:%s\n","ACTIVE");
1011 display remote list of keys/data for a db
1013 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
1015 const char *db_name;
1016 struct ctdb_db_context *ctdb_db;
1024 ctdb_db = ctdb_attach(ctdb, db_name, false);
1026 if (ctdb_db == NULL) {
1027 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
1031 /* traverse and dump the cluster tdb */
1032 ret = ctdb_dump_db(ctdb_db, stdout);
1034 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
1037 talloc_free(ctdb_db);
1039 printf("Dumped %d records\n", ret);
1045 display a list of the databases on a remote ctdb
1047 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
1050 struct ctdb_dbid_map *dbmap=NULL;
1052 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
1054 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
1058 printf("Number of databases:%d\n", dbmap->num);
1059 for(i=0;i<dbmap->num;i++){
1064 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
1065 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
1066 persistent = dbmap->dbs[i].persistent;
1067 printf("dbid:0x%08x name:%s path:%s %s\n", dbmap->dbs[i].dbid, name,
1068 path, persistent?"PERSISTENT":"");
1075 get the filename of the reclock file
1077 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
1080 const char *reclock;
1081 struct ctdb_node_map *nodemap=NULL;
1084 ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
1086 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
1090 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
1092 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1097 pnnfile = talloc_asprintf(ctdb, "%s.pnn", reclock);
1098 CTDB_NO_MEMORY(ctdb, pnnfile);
1100 fd = open(pnnfile, O_RDONLY);
1102 DEBUG(DEBUG_CRIT,(__location__ " Failed to open reclock pnn file %s - (%s)\n",
1103 pnnfile, strerror(errno)));
1108 printf("Reclock file : %s\n", reclock);
1109 for (i=0; i<nodemap->num; i++) {
1112 count = ctdb_read_pnn_lock(fd, nodemap->nodes[i].pnn);
1114 printf("pnn:%d %-16s", nodemap->nodes[i].pnn,
1115 inet_ntoa(nodemap->nodes[i].sin.sin_addr));
1117 printf(" NOT ACTIVE\n");
1119 printf(" ACTIVE with %d connections\n", count);
1129 check if the local node is recmaster or not
1130 it will return 1 if this node is the recmaster and 0 if it is not
1131 or if the local ctdb daemon could not be contacted
1133 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1135 uint32_t mypnn, recmaster;
1138 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
1140 printf("Failed to get pnn of node\n");
1144 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
1146 printf("Failed to get the recmaster\n");
1150 if (recmaster != mypnn) {
1151 printf("this node is not the recmaster\n");
1155 printf("this node is the recmaster\n");
1162 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
1165 struct timeval tv = timeval_current();
1166 ret = ctdb_ctrl_ping(ctdb, options.pnn);
1168 printf("Unable to get ping response from node %u\n", options.pnn);
1170 printf("response from %u time=%.6f sec (%d clients)\n",
1171 options.pnn, timeval_elapsed(&tv), ret);
1180 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
1191 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
1193 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
1197 printf("%-19s = %u\n", name, value);
1204 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
1215 value = strtoul(argv[1], NULL, 0);
1217 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
1219 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
1228 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
1234 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
1236 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
1240 for (i=0;i<count;i++) {
1241 control_getvar(ctdb, 1, &list[i]);
1251 const char *description;
1252 } debug_levels[] = {
1253 {DEBUG_EMERG, "EMERG"},
1254 {DEBUG_ALERT, "ALERT"},
1255 {DEBUG_CRIT, "CRIT"},
1257 {DEBUG_WARNING, "WARNING"},
1258 {DEBUG_NOTICE, "NOTICE"},
1259 {DEBUG_INFO, "INFO"},
1260 {DEBUG_DEBUG, "DEBUG"}
1263 static const char *get_debug_by_level(int32_t level)
1267 for (i=0;i<ARRAY_SIZE(debug_levels);i++) {
1268 if (debug_levels[i].level == level) {
1269 return debug_levels[i].description;
1275 static int32_t get_debug_by_desc(const char *desc)
1279 for (i=0;i<ARRAY_SIZE(debug_levels);i++) {
1280 if (!strcmp(debug_levels[i].description, desc)) {
1281 return debug_levels[i].level;
1288 display debug level on a node
1290 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
1295 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
1297 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
1300 if (options.machinereadable){
1301 printf(":Name:Level:\n");
1302 printf(":%s:%d:\n",get_debug_by_level(level),level);
1304 printf("Node %u is at debug level %s (%u)\n", options.pnn, get_debug_by_level(level), level);
1312 set debug level on a node or all nodes
1314 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
1323 if (isalpha(argv[0][0])) {
1324 level = get_debug_by_desc(argv[0]);
1326 level = strtoul(argv[0], NULL, 0);
1329 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
1331 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
1340 static int control_freeze(struct ctdb_context *ctdb, int argc, const char **argv)
1344 ret = ctdb_ctrl_freeze(ctdb, TIMELIMIT(), options.pnn);
1346 DEBUG(DEBUG_ERR, ("Unable to freeze node %u\n", options.pnn));
1354 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
1358 ret = ctdb_ctrl_thaw(ctdb, TIMELIMIT(), options.pnn);
1360 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
1367 attach to a database
1369 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
1371 const char *db_name;
1372 struct ctdb_db_context *ctdb_db;
1379 ctdb_db = ctdb_attach(ctdb, db_name, false);
1380 if (ctdb_db == NULL) {
1381 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
1391 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
1397 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1398 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
1399 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
1400 if (ret != 0 || res != 0) {
1401 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
1402 talloc_free(tmp_ctx);
1405 write(1, data.dptr, data.dsize);
1406 talloc_free(tmp_ctx);
1411 handler for memory dumps
1413 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid,
1414 TDB_DATA data, void *private_data)
1416 write(1, data.dptr, data.dsize);
1421 dump memory usage on the recovery daemon
1423 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
1427 struct rd_memdump_reply rd;
1429 rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
1431 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
1434 rd.srvid = getpid();
1436 /* register a message port for receiveing the reply so that we
1437 can receive the reply
1439 ctdb_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
1442 data.dptr = (uint8_t *)&rd;
1443 data.dsize = sizeof(rd);
1445 ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
1447 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
1451 /* this loop will terminate when we have received the reply */
1453 event_loop_once(ctdb->ev);
1460 list all nodes in the cluster
1462 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
1465 struct ctdb_node_map *nodemap=NULL;
1467 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
1469 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1473 for(i=0;i<nodemap->num;i++){
1474 printf("%s\n", inet_ntoa(nodemap->nodes[i].sin.sin_addr));
1481 reload the nodes file on the local node
1483 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
1487 struct ctdb_node_map *nodemap=NULL;
1489 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
1491 DEBUG(DEBUG_ERR, ("Failed to read pnn of local node\n"));
1495 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
1497 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
1501 /* reload the nodes file on all remote nodes */
1502 for (i=0;i<nodemap->num;i++) {
1503 if (nodemap->nodes[i].pnn == mypnn) {
1506 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
1507 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
1508 nodemap->nodes[i].pnn);
1510 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
1514 /* reload the nodes file on the local node */
1515 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
1516 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
1518 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
1525 static const struct {
1527 int (*fn)(struct ctdb_context *, int, const char **);
1531 } ctdb_commands[] = {
1532 { "status", control_status, true, "show node status" },
1533 { "uptime", control_uptime, true, "show node uptime" },
1534 { "ping", control_ping, true, "ping all nodes" },
1535 { "getvar", control_getvar, true, "get a tunable variable", "<name>"},
1536 { "setvar", control_setvar, true, "set a tunable variable", "<name> <value>"},
1537 { "listvars", control_listvars, true, "list tunable variables"},
1538 { "statistics", control_statistics, false, "show statistics" },
1539 { "statisticsreset", control_statistics_reset, true, "reset statistics"},
1540 { "ip", control_ip, true, "show which public ip's that ctdb manages" },
1541 { "process-exists", control_process_exists, true, "check if a process exists on a node", "<pid>"},
1542 { "getdbmap", control_getdbmap, true, "show the database map" },
1543 { "catdb", control_catdb, true, "dump a database" , "<dbname>"},
1544 { "getmonmode", control_getmonmode, true, "show monitoring mode" },
1545 { "disablemonitor", control_disable_monmode, true, "set monitoring mode to DISABLE" },
1546 { "enablemonitor", control_enable_monmode, true, "set monitoring mode to ACTIVE" },
1547 { "setdebug", control_setdebug, true, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
1548 { "getdebug", control_getdebug, true, "get debug level" },
1549 { "attach", control_attach, true, "attach to a database", "<dbname>" },
1550 { "dumpmemory", control_dumpmemory, true, "dump memory map to stdout" },
1551 { "rddumpmemory", control_rddumpmemory, true, "dump memory map from the recovery daemon to stdout" },
1552 { "getpid", control_getpid, true, "get ctdbd process ID" },
1553 { "disable", control_disable, true, "disable a nodes public IP" },
1554 { "enable", control_enable, true, "enable a nodes public IP" },
1555 { "ban", control_ban, true, "ban a node from the cluster", "<bantime|0>"},
1556 { "unban", control_unban, true, "unban a node from the cluster" },
1557 { "shutdown", control_shutdown, true, "shutdown ctdbd" },
1558 { "recover", control_recover, true, "force recovery" },
1559 { "freeze", control_freeze, true, "freeze all databases" },
1560 { "thaw", control_thaw, true, "thaw all databases" },
1561 { "isnotrecmaster", control_isnotrecmaster, false, "check if the local node is recmaster or not" },
1562 { "killtcp", kill_tcp, false, "kill a tcp connection.", "<srcip:port> <dstip:port>" },
1563 { "gratiousarp", control_gratious_arp, false, "send a gratious arp", "<ip> <interface>" },
1564 { "tickle", tickle_tcp, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
1565 { "gettickles", control_get_tickles, false, "get the list of tickles registered for this ip", "<ip>" },
1567 { "regsrvid", regsrvid, false, "register a server id", "<pnn> <type> <id>" },
1568 { "unregsrvid", unregsrvid, false, "unregister a server id", "<pnn> <type> <id>" },
1569 { "chksrvid", chksrvid, false, "check if a server id exists", "<pnn> <type> <id>" },
1570 { "getsrvids", getsrvids, false, "get a list of all server ids"},
1571 { "vacuum", ctdb_vacuum, false, "vacuum the databases of empty records", "[max_records]"},
1572 { "repack", ctdb_repack, false, "repack all databases", "[max_freelist]"},
1573 { "listnodes", control_listnodes, false, "list all nodes in the cluster"},
1574 { "reloadnodes", control_reload_nodes_file, false, "reload the nodes file and restart the transport on all nodes"},
1575 { "getreclock", control_getreclock, false, "get the path to the reclock file" },
1576 { "moveip", control_moveip, false, "move/failover an ip address to another node", "<ip> <node>"},
1577 { "addip", control_addip, false, "add a ip address to a node", "<ip/mask> <iface>"},
1578 { "delip", control_delip, false, "delete an ip address from a node", "<ip>"},
1584 static void usage(void)
1588 "Usage: ctdb [options] <control>\n" \
1590 " -n <node> choose node number, or 'all' (defaults to local node)\n"
1591 " -Y generate machinereadable output\n"
1592 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
1593 printf("Controls:\n");
1594 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
1595 printf(" %-15s %-27s %s\n",
1596 ctdb_commands[i].name,
1597 ctdb_commands[i].args?ctdb_commands[i].args:"",
1598 ctdb_commands[i].msg);
1604 static void ctdb_alarm(int sig)
1606 printf("Maximum runtime exceeded - exiting\n");
1613 int main(int argc, const char *argv[])
1615 struct ctdb_context *ctdb;
1616 char *nodestring = NULL;
1617 struct poptOption popt_options[] = {
1620 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
1621 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
1622 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
1623 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
1627 const char **extra_argv;
1631 struct event_context *ev;
1632 const char *control;
1636 /* set some defaults */
1637 options.maxruntime = 0;
1638 options.timelimit = 3;
1639 options.pnn = CTDB_CURRENT_NODE;
1641 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
1643 while ((opt = poptGetNextOpt(pc)) != -1) {
1646 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
1647 poptBadOption(pc, 0), poptStrerror(opt)));
1652 /* setup the remaining options for the main program to use */
1653 extra_argv = poptGetArgs(pc);
1656 while (extra_argv[extra_argc]) extra_argc++;
1659 if (extra_argc < 1) {
1663 if (options.maxruntime != 0) {
1664 signal(SIGALRM, ctdb_alarm);
1665 alarm(options.maxruntime);
1668 /* setup the node number to contact */
1669 if (nodestring != NULL) {
1670 if (strcmp(nodestring, "all") == 0) {
1671 options.pnn = CTDB_BROADCAST_ALL;
1673 options.pnn = strtoul(nodestring, NULL, 0);
1677 control = extra_argv[0];
1679 ev = event_context_init(NULL);
1681 /* initialise ctdb */
1682 ctdb = ctdb_cmdline_client(ev);
1684 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
1688 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
1689 if (strcmp(control, ctdb_commands[i].name) == 0) {
1692 if (options.pnn == CTDB_CURRENT_NODE) {
1694 pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
1701 if (ctdb_commands[i].auto_all &&
1702 options.pnn == CTDB_BROADCAST_ALL) {
1707 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
1708 CTDB_NO_MEMORY(ctdb, nodes);
1710 for (j=0;j<num_nodes;j++) {
1711 options.pnn = nodes[j];
1712 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
1716 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
1722 if (i == ARRAY_SIZE(ctdb_commands)) {
1723 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));