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"
28 #include "../include/ctdb.h"
29 #include "../include/ctdb_private.h"
31 static void usage(void);
40 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
43 see if a process exists
45 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
53 if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
54 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
58 ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
60 printf("%u:%u exists\n", pnn, pid);
62 printf("%u:%u does not exist\n", pnn, pid);
68 display statistics structure
70 static void show_statistics(struct ctdb_statistics *s)
72 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
74 const char *prefix=NULL;
80 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
81 STATISTICS_FIELD(num_clients),
82 STATISTICS_FIELD(frozen),
83 STATISTICS_FIELD(recovering),
84 STATISTICS_FIELD(client_packets_sent),
85 STATISTICS_FIELD(client_packets_recv),
86 STATISTICS_FIELD(node_packets_sent),
87 STATISTICS_FIELD(node_packets_recv),
88 STATISTICS_FIELD(keepalive_packets_sent),
89 STATISTICS_FIELD(keepalive_packets_recv),
90 STATISTICS_FIELD(node.req_call),
91 STATISTICS_FIELD(node.reply_call),
92 STATISTICS_FIELD(node.req_dmaster),
93 STATISTICS_FIELD(node.reply_dmaster),
94 STATISTICS_FIELD(node.reply_error),
95 STATISTICS_FIELD(node.req_message),
96 STATISTICS_FIELD(node.req_control),
97 STATISTICS_FIELD(node.reply_control),
98 STATISTICS_FIELD(client.req_call),
99 STATISTICS_FIELD(client.req_message),
100 STATISTICS_FIELD(client.req_control),
101 STATISTICS_FIELD(timeouts.call),
102 STATISTICS_FIELD(timeouts.control),
103 STATISTICS_FIELD(timeouts.traverse),
104 STATISTICS_FIELD(total_calls),
105 STATISTICS_FIELD(pending_calls),
106 STATISTICS_FIELD(lockwait_calls),
107 STATISTICS_FIELD(pending_lockwait_calls),
108 STATISTICS_FIELD(memory_used),
109 STATISTICS_FIELD(max_hop_count),
111 printf("CTDB version %u\n", CTDB_VERSION);
112 for (i=0;i<ARRAY_SIZE(fields);i++) {
113 if (strchr(fields[i].name, '.')) {
114 preflen = strcspn(fields[i].name, ".")+1;
115 if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
116 prefix = fields[i].name;
117 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
122 printf(" %*s%-22s%*s%10u\n",
124 fields[i].name+preflen,
126 *(uint32_t *)(fields[i].offset+(uint8_t *)s));
128 printf(" %-30s %.6f sec\n", "max_call_latency", s->max_call_latency);
129 printf(" %-30s %.6f sec\n", "max_lockwait_latency", s->max_lockwait_latency);
130 talloc_free(tmp_ctx);
134 display remote ctdb statistics combined from all nodes
136 static int control_statistics_all(struct ctdb_context *ctdb)
139 struct ctdb_statistics statistics;
143 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
144 CTDB_NO_MEMORY(ctdb, nodes);
146 ZERO_STRUCT(statistics);
148 for (i=0;i<num_nodes;i++) {
149 struct ctdb_statistics s1;
151 uint32_t *v1 = (uint32_t *)&s1;
152 uint32_t *v2 = (uint32_t *)&statistics;
154 offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
155 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
157 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
160 for (j=0;j<num_ints;j++) {
163 statistics.max_hop_count =
164 MAX(statistics.max_hop_count, s1.max_hop_count);
165 statistics.max_call_latency =
166 MAX(statistics.max_call_latency, s1.max_call_latency);
167 statistics.max_lockwait_latency =
168 MAX(statistics.max_lockwait_latency, s1.max_lockwait_latency);
171 printf("Gathered statistics for %u nodes\n", num_nodes);
172 show_statistics(&statistics);
177 display remote ctdb statistics
179 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
182 struct ctdb_statistics statistics;
184 if (options.pnn == CTDB_BROADCAST_ALL) {
185 return control_statistics_all(ctdb);
188 ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
190 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
193 show_statistics(&statistics);
199 reset remote ctdb statistics
201 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
205 ret = ctdb_statistics_reset(ctdb, options.pnn);
207 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
215 display uptime of remote node
217 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
221 struct ctdb_uptime *uptime = NULL;
222 int tmp, days, hours, minutes, seconds;
224 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
229 ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
231 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
235 printf("Current time of node : %s", ctime(&uptime->current_time.tv_sec));
237 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
245 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
247 tmp = uptime->current_time.tv_sec - uptime->last_recovery_time.tv_sec;
255 printf("Time of last recovery : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_time.tv_sec));
261 display remote ctdb status
263 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
266 struct ctdb_vnn_map *vnnmap=NULL;
267 struct ctdb_node_map *nodemap=NULL;
268 uint32_t recmode, recmaster;
271 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
276 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
278 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
282 if(options.machinereadable){
283 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:\n");
284 for(i=0;i<nodemap->num;i++){
285 printf(":%d:%s:%d:%d:%d:%d:\n", nodemap->nodes[i].pnn,
286 inet_ntoa(nodemap->nodes[i].sin.sin_addr),
287 !!(nodemap->nodes[i].flags&NODE_FLAGS_DISCONNECTED),
288 !!(nodemap->nodes[i].flags&NODE_FLAGS_BANNED),
289 !!(nodemap->nodes[i].flags&NODE_FLAGS_PERMANENTLY_DISABLED),
290 !!(nodemap->nodes[i].flags&NODE_FLAGS_UNHEALTHY));
295 printf("Number of nodes:%d\n", nodemap->num);
296 for(i=0;i<nodemap->num;i++){
297 static const struct {
301 { NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
302 { NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
303 { NODE_FLAGS_BANNED, "BANNED" },
304 { NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
306 char *flags_str = NULL;
308 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
309 if (nodemap->nodes[i].flags & flag_names[j].flag) {
310 if (flags_str == NULL) {
311 flags_str = talloc_strdup(ctdb, flag_names[j].name);
313 flags_str = talloc_asprintf_append(flags_str, "|%s",
316 CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
319 if (flags_str == NULL) {
320 flags_str = talloc_strdup(ctdb, "OK");
321 CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
323 printf("pnn:%d %-16s %s%s\n", nodemap->nodes[i].pnn,
324 inet_ntoa(nodemap->nodes[i].sin.sin_addr),
326 nodemap->nodes[i].pnn == mypnn?" (THIS NODE)":"");
327 talloc_free(flags_str);
330 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &vnnmap);
332 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
335 if (vnnmap->generation == INVALID_GENERATION) {
336 printf("Generation:INVALID\n");
338 printf("Generation:%d\n",vnnmap->generation);
340 printf("Size:%d\n",vnnmap->size);
341 for(i=0;i<vnnmap->size;i++){
342 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
345 ret = ctdb_ctrl_getrecmode(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmode);
347 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
350 printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
352 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
354 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
357 printf("Recovery master:%d\n",recmaster);
363 get a list of all tickles for this pnn
365 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
367 struct ctdb_control_tcp_tickle_list *list;
368 struct sockaddr_in ip;
375 ip.sin_family = AF_INET;
376 if (inet_aton(argv[0], &ip.sin_addr) == 0) {
377 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
381 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &ip, &list);
383 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
387 printf("Tickles for ip:%s\n", inet_ntoa(list->ip.sin_addr));
388 printf("Num tickles:%u\n", list->tickles.num);
389 for (i=0;i<list->tickles.num;i++) {
390 printf("SRC: %s:%u ", inet_ntoa(list->tickles.connections[i].saddr.sin_addr), ntohs(list->tickles.connections[i].saddr.sin_port));
391 printf("DST: %s:%u\n", inet_ntoa(list->tickles.connections[i].daddr.sin_addr), ntohs(list->tickles.connections[i].daddr.sin_port));
400 kill a tcp connection
402 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
405 struct ctdb_control_killtcp killtcp;
411 if (!parse_ip_port(argv[0], &killtcp.src)) {
412 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
416 if (!parse_ip_port(argv[1], &killtcp.dst)) {
417 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
421 ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
423 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
434 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
437 struct sockaddr_in sin;
443 sin.sin_family = AF_INET;
444 if (inet_aton(argv[0], &sin.sin_addr) == 0) {
445 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
449 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &sin, argv[1]);
451 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
461 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
464 struct ctdb_server_id server_id;
470 server_id.pnn = strtoul(argv[0], NULL, 0);
471 server_id.type = strtoul(argv[1], NULL, 0);
472 server_id.server_id = strtoul(argv[2], NULL, 0);
474 ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
476 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
483 unregister a server id
485 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
488 struct ctdb_server_id server_id;
494 server_id.pnn = strtoul(argv[0], NULL, 0);
495 server_id.type = strtoul(argv[1], NULL, 0);
496 server_id.server_id = strtoul(argv[2], NULL, 0);
498 ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
500 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
507 check if a server id exists
509 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
513 struct ctdb_server_id server_id;
519 server_id.pnn = strtoul(argv[0], NULL, 0);
520 server_id.type = strtoul(argv[1], NULL, 0);
521 server_id.server_id = strtoul(argv[2], NULL, 0);
523 ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
525 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
530 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
532 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
538 get a list of all server ids that are registered on a node
540 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
543 struct ctdb_server_id_list *server_ids;
545 ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
547 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
551 for (i=0; i<server_ids->num; i++) {
552 printf("Server id %d:%d:%d\n",
553 server_ids->server_ids[i].pnn,
554 server_ids->server_ids[i].type,
555 server_ids->server_ids[i].server_id);
562 send a tcp tickle ack
564 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
567 struct sockaddr_in src, dst;
573 if (!parse_ip_port(argv[0], &src)) {
574 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
578 if (!parse_ip_port(argv[1], &dst)) {
579 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
583 s = ctdb_sys_open_sending_socket();
585 DEBUG(DEBUG_ERR, ("Failed to open socket for sending tickle\n"));
589 ret = ctdb_sys_send_tcp(s, &src, &dst, 0, 0, 0);
594 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
601 display public ip status
603 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
606 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
607 struct ctdb_all_public_ips *ips;
609 /* read the public ip list from this node */
610 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
612 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
613 talloc_free(tmp_ctx);
617 if (options.machinereadable){
618 printf(":Public IP:Node:\n");
620 printf("Public IPs on node %u\n", options.pnn);
623 for (i=1;i<=ips->num;i++) {
624 printf("%s %d\n", inet_ntoa(ips->ips[ips->num-i].sin.sin_addr), ips->ips[ips->num-i].pnn);
627 talloc_free(tmp_ctx);
632 display pid of a ctdb daemon
634 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
639 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
641 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
644 printf("Pid:%d\n", pid);
650 disable a remote node
652 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
656 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, NODE_FLAGS_PERMANENTLY_DISABLED, 0);
658 DEBUG(DEBUG_ERR, ("Unable to disable node %u\n", options.pnn));
666 enable a disabled remote node
668 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
672 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, 0, NODE_FLAGS_PERMANENTLY_DISABLED);
674 DEBUG(DEBUG_ERR, ("Unable to enable node %u\n", options.pnn));
682 ban a node from the cluster
684 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
687 struct ctdb_ban_info b;
695 ban_time = strtoul(argv[0], NULL, 0);
698 b.ban_time = ban_time;
700 data.dptr = (uint8_t *)&b;
701 data.dsize = sizeof(b);
703 ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_BAN_NODE, data);
705 DEBUG(DEBUG_ERR,("Failed to ban node %u\n", options.pnn));
714 unban a node from the cluster
716 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
721 data.dptr = (uint8_t *)&options.pnn;
722 data.dsize = sizeof(uint32_t);
724 ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_UNBAN_NODE, data);
726 DEBUG(DEBUG_ERR,("Failed to to unban node %u\n", options.pnn));
737 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
741 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
743 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
753 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
757 ret = ctdb_ctrl_freeze(ctdb, TIMELIMIT(), options.pnn);
759 DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
763 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
765 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
774 display monitoring mode of a remote node
776 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
781 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
783 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
786 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
792 display remote list of keys/data for a db
794 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
797 struct ctdb_db_context *ctdb_db;
805 ctdb_db = ctdb_attach(ctdb, db_name, false);
807 if (ctdb_db == NULL) {
808 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
812 /* traverse and dump the cluster tdb */
813 ret = ctdb_dump_db(ctdb_db, stdout);
815 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
818 talloc_free(ctdb_db);
820 printf("Dumped %d records\n", ret);
826 display a list of the databases on a remote ctdb
828 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
831 struct ctdb_dbid_map *dbmap=NULL;
833 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
835 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
839 printf("Number of databases:%d\n", dbmap->num);
840 for(i=0;i<dbmap->num;i++){
845 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
846 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
847 persistent = dbmap->dbs[i].persistent;
848 printf("dbid:0x%08x name:%s path:%s %s\n", dbmap->dbs[i].dbid, name,
849 path, persistent?"PERSISTENT":"");
856 check if the local node is recmaster or not
857 it will return 1 if this node is the recmaster and 0 if it is not
858 or if the local ctdb daemon could not be contacted
860 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
862 uint32_t mypnn, recmaster;
865 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
867 printf("Failed to get pnn of node\n");
871 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
873 printf("Failed to get the recmaster\n");
877 if (recmaster != mypnn) {
878 printf("this node is not the recmaster\n");
882 printf("this node is the recmaster\n");
889 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
892 struct timeval tv = timeval_current();
893 ret = ctdb_ctrl_ping(ctdb, options.pnn);
895 printf("Unable to get ping response from node %u\n", options.pnn);
897 printf("response from %u time=%.6f sec (%d clients)\n",
898 options.pnn, timeval_elapsed(&tv), ret);
907 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
918 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
920 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
924 printf("%-19s = %u\n", name, value);
931 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
942 value = strtoul(argv[1], NULL, 0);
944 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
946 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
955 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
961 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
963 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
967 for (i=0;i<count;i++) {
968 control_getvar(ctdb, 1, &list[i]);
977 display debug level on a node
979 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
984 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
986 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n",
989 printf("Node %u is at debug level %u\n", options.pnn, level);
996 set debug level on a node or all nodes
998 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
1007 level = strtoul(argv[0], NULL, 0);
1009 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
1011 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
1020 static int control_freeze(struct ctdb_context *ctdb, int argc, const char **argv)
1024 ret = ctdb_ctrl_freeze(ctdb, TIMELIMIT(), options.pnn);
1026 DEBUG(DEBUG_ERR, ("Unable to freeze node %u\n", options.pnn));
1034 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
1038 ret = ctdb_ctrl_thaw(ctdb, TIMELIMIT(), options.pnn);
1040 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
1047 attach to a database
1049 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
1051 const char *db_name;
1052 struct ctdb_db_context *ctdb_db;
1059 ctdb_db = ctdb_attach(ctdb, db_name, false);
1060 if (ctdb_db == NULL) {
1061 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
1071 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
1077 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1078 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
1079 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
1080 if (ret != 0 || res != 0) {
1081 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
1082 talloc_free(tmp_ctx);
1085 write(1, data.dptr, data.dsize);
1086 talloc_free(tmp_ctx);
1090 static const struct {
1092 int (*fn)(struct ctdb_context *, int, const char **);
1096 } ctdb_commands[] = {
1097 { "status", control_status, true, "show node status" },
1098 { "uptime", control_uptime, true, "show node uptime" },
1099 { "ping", control_ping, true, "ping all nodes" },
1100 { "getvar", control_getvar, true, "get a tunable variable", "<name>"},
1101 { "setvar", control_setvar, true, "set a tunable variable", "<name> <value>"},
1102 { "listvars", control_listvars, true, "list tunable variables"},
1103 { "statistics", control_statistics, false, "show statistics" },
1104 { "statisticsreset", control_statistics_reset, true, "reset statistics"},
1105 { "ip", control_ip, true, "show which public ip's that ctdb manages" },
1106 { "process-exists", control_process_exists, true, "check if a process exists on a node", "<pid>"},
1107 { "getdbmap", control_getdbmap, true, "show the database map" },
1108 { "catdb", control_catdb, true, "dump a database" , "<dbname>"},
1109 { "getmonmode", control_getmonmode, true, "show monitoring mode" },
1110 { "setdebug", control_setdebug, true, "set debug level", "<debuglevel>" },
1111 { "getdebug", control_getdebug, true, "get debug level" },
1112 { "attach", control_attach, true, "attach to a database", "<dbname>" },
1113 { "dumpmemory", control_dumpmemory, true, "dump memory map to stdout" },
1114 { "getpid", control_getpid, true, "get ctdbd process ID" },
1115 { "disable", control_disable, true, "disable a nodes public IP" },
1116 { "enable", control_enable, true, "enable a nodes public IP" },
1117 { "ban", control_ban, true, "ban a node from the cluster", "<bantime|0>"},
1118 { "unban", control_unban, true, "unban a node from the cluster" },
1119 { "shutdown", control_shutdown, true, "shutdown ctdbd" },
1120 { "recover", control_recover, true, "force recovery" },
1121 { "freeze", control_freeze, true, "freeze all databases" },
1122 { "thaw", control_thaw, true, "thaw all databases" },
1123 { "isnotrecmaster", control_isnotrecmaster, false, "check if the local node is recmaster or not" },
1124 { "killtcp", kill_tcp, false, "kill a tcp connection.", "<srcip:port> <dstip:port>" },
1125 { "gratiousarp", control_gratious_arp, false, "send a gratious arp", "<ip> <interface>" },
1126 { "tickle", tickle_tcp, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
1127 { "gettickles", control_get_tickles, false, "get the list of tickles registered for this ip", "<ip>" },
1129 { "regsrvid", regsrvid, false, "register a server id", "<pnn> <type> <id>" },
1130 { "unregsrvid", unregsrvid, false, "unregister a server id", "<pnn> <type> <id>" },
1131 { "chksrvid", chksrvid, false, "check if a server id exists", "<pnn> <type> <id>" },
1132 { "getsrvids", getsrvids, false, "get a list of all server ids"},
1133 { "vacuum", ctdb_vacuum, false, "vacuum the databases of empty records", "[max_records]"},
1134 { "repack", ctdb_repack, false, "repack all databases", "[max_freelist]"},
1140 static void usage(void)
1144 "Usage: ctdb [options] <control>\n" \
1146 " -n <node> choose node number, or 'all' (defaults to local node)\n"
1147 " -Y generate machinereadable output\n"
1148 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
1149 printf("Controls:\n");
1150 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
1151 printf(" %-15s %-27s %s\n",
1152 ctdb_commands[i].name,
1153 ctdb_commands[i].args?ctdb_commands[i].args:"",
1154 ctdb_commands[i].msg);
1160 static void ctdb_alarm(int sig)
1162 printf("Maximum runtime exceeded - exiting\n");
1169 int main(int argc, const char *argv[])
1171 struct ctdb_context *ctdb;
1172 char *nodestring = NULL;
1173 struct poptOption popt_options[] = {
1176 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
1177 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
1178 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
1179 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
1183 const char **extra_argv;
1187 struct event_context *ev;
1188 const char *control;
1192 /* set some defaults */
1193 options.maxruntime = 0;
1194 options.timelimit = 3;
1195 options.pnn = CTDB_CURRENT_NODE;
1197 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
1199 while ((opt = poptGetNextOpt(pc)) != -1) {
1202 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
1203 poptBadOption(pc, 0), poptStrerror(opt)));
1208 /* setup the remaining options for the main program to use */
1209 extra_argv = poptGetArgs(pc);
1212 while (extra_argv[extra_argc]) extra_argc++;
1215 if (extra_argc < 1) {
1219 if (options.maxruntime != 0) {
1220 signal(SIGALRM, ctdb_alarm);
1221 alarm(options.maxruntime);
1224 /* setup the node number to contact */
1225 if (nodestring != NULL) {
1226 if (strcmp(nodestring, "all") == 0) {
1227 options.pnn = CTDB_BROADCAST_ALL;
1229 options.pnn = strtoul(nodestring, NULL, 0);
1233 control = extra_argv[0];
1235 ev = event_context_init(NULL);
1237 /* initialise ctdb */
1238 ctdb = ctdb_cmdline_client(ev);
1240 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
1244 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
1245 if (strcmp(control, ctdb_commands[i].name) == 0) {
1248 if (options.pnn == CTDB_CURRENT_NODE) {
1250 pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
1257 if (ctdb_commands[i].auto_all &&
1258 options.pnn == CTDB_BROADCAST_ALL) {
1263 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
1264 CTDB_NO_MEMORY(ctdb, nodes);
1266 for (j=0;j<num_nodes;j++) {
1267 options.pnn = nodes[j];
1268 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
1272 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
1278 if (i == ARRAY_SIZE(ctdb_commands)) {
1279 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));