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 printf("Current time of node : %s", ctime(&uptime->current_time.tv_sec));
238 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
246 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
248 tmp = uptime->current_time.tv_sec - uptime->last_recovery_time.tv_sec;
256 printf("Time of last recovery : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_time.tv_sec));
262 display remote ctdb status
264 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
267 struct ctdb_vnn_map *vnnmap=NULL;
268 struct ctdb_node_map *nodemap=NULL;
269 uint32_t recmode, recmaster;
272 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
277 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
279 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
283 if(options.machinereadable){
284 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:\n");
285 for(i=0;i<nodemap->num;i++){
286 printf(":%d:%s:%d:%d:%d:%d:\n", nodemap->nodes[i].pnn,
287 inet_ntoa(nodemap->nodes[i].sin.sin_addr),
288 !!(nodemap->nodes[i].flags&NODE_FLAGS_DISCONNECTED),
289 !!(nodemap->nodes[i].flags&NODE_FLAGS_BANNED),
290 !!(nodemap->nodes[i].flags&NODE_FLAGS_PERMANENTLY_DISABLED),
291 !!(nodemap->nodes[i].flags&NODE_FLAGS_UNHEALTHY));
296 printf("Number of nodes:%d\n", nodemap->num);
297 for(i=0;i<nodemap->num;i++){
298 static const struct {
302 { NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
303 { NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
304 { NODE_FLAGS_BANNED, "BANNED" },
305 { NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
307 char *flags_str = NULL;
309 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
310 if (nodemap->nodes[i].flags & flag_names[j].flag) {
311 if (flags_str == NULL) {
312 flags_str = talloc_strdup(ctdb, flag_names[j].name);
314 flags_str = talloc_asprintf_append(flags_str, "|%s",
317 CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
320 if (flags_str == NULL) {
321 flags_str = talloc_strdup(ctdb, "OK");
322 CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
324 printf("pnn:%d %-16s %s%s\n", nodemap->nodes[i].pnn,
325 inet_ntoa(nodemap->nodes[i].sin.sin_addr),
327 nodemap->nodes[i].pnn == mypnn?" (THIS NODE)":"");
328 talloc_free(flags_str);
331 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &vnnmap);
333 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
336 if (vnnmap->generation == INVALID_GENERATION) {
337 printf("Generation:INVALID\n");
339 printf("Generation:%d\n",vnnmap->generation);
341 printf("Size:%d\n",vnnmap->size);
342 for(i=0;i<vnnmap->size;i++){
343 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
346 ret = ctdb_ctrl_getrecmode(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmode);
348 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
351 printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
353 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
355 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
358 printf("Recovery master:%d\n",recmaster);
364 get a list of all tickles for this pnn
366 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
368 struct ctdb_control_tcp_tickle_list *list;
369 struct sockaddr_in ip;
376 ip.sin_family = AF_INET;
377 if (inet_aton(argv[0], &ip.sin_addr) == 0) {
378 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
382 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &ip, &list);
384 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
388 printf("Tickles for ip:%s\n", inet_ntoa(list->ip.sin_addr));
389 printf("Num tickles:%u\n", list->tickles.num);
390 for (i=0;i<list->tickles.num;i++) {
391 printf("SRC: %s:%u ", inet_ntoa(list->tickles.connections[i].saddr.sin_addr), ntohs(list->tickles.connections[i].saddr.sin_port));
392 printf("DST: %s:%u\n", inet_ntoa(list->tickles.connections[i].daddr.sin_addr), ntohs(list->tickles.connections[i].daddr.sin_port));
401 kill a tcp connection
403 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
406 struct ctdb_control_killtcp killtcp;
412 if (!parse_ip_port(argv[0], &killtcp.src)) {
413 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
417 if (!parse_ip_port(argv[1], &killtcp.dst)) {
418 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
422 ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
424 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
435 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
438 struct sockaddr_in sin;
444 sin.sin_family = AF_INET;
445 if (inet_aton(argv[0], &sin.sin_addr) == 0) {
446 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
450 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &sin, argv[1]);
452 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
462 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
465 struct ctdb_server_id server_id;
471 server_id.pnn = strtoul(argv[0], NULL, 0);
472 server_id.type = strtoul(argv[1], NULL, 0);
473 server_id.server_id = strtoul(argv[2], NULL, 0);
475 ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
477 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
484 unregister a server id
486 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
489 struct ctdb_server_id server_id;
495 server_id.pnn = strtoul(argv[0], NULL, 0);
496 server_id.type = strtoul(argv[1], NULL, 0);
497 server_id.server_id = strtoul(argv[2], NULL, 0);
499 ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
501 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
508 check if a server id exists
510 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
514 struct ctdb_server_id server_id;
520 server_id.pnn = strtoul(argv[0], NULL, 0);
521 server_id.type = strtoul(argv[1], NULL, 0);
522 server_id.server_id = strtoul(argv[2], NULL, 0);
524 ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
526 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
531 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
533 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
539 get a list of all server ids that are registered on a node
541 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
544 struct ctdb_server_id_list *server_ids;
546 ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
548 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
552 for (i=0; i<server_ids->num; i++) {
553 printf("Server id %d:%d:%d\n",
554 server_ids->server_ids[i].pnn,
555 server_ids->server_ids[i].type,
556 server_ids->server_ids[i].server_id);
563 send a tcp tickle ack
565 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
568 struct sockaddr_in src, dst;
574 if (!parse_ip_port(argv[0], &src)) {
575 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
579 if (!parse_ip_port(argv[1], &dst)) {
580 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
584 s = ctdb_sys_open_sending_socket();
586 DEBUG(DEBUG_ERR, ("Failed to open socket for sending tickle\n"));
590 ret = ctdb_sys_send_tcp(s, &src, &dst, 0, 0, 0);
595 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
602 display public ip status
604 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
607 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
608 struct ctdb_all_public_ips *ips;
610 /* read the public ip list from this node */
611 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
613 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
614 talloc_free(tmp_ctx);
618 if (options.machinereadable){
619 printf(":Public IP:Node:\n");
621 printf("Public IPs on node %u\n", options.pnn);
624 for (i=1;i<=ips->num;i++) {
625 printf("%s %d\n", inet_ntoa(ips->ips[ips->num-i].sin.sin_addr), ips->ips[ips->num-i].pnn);
628 talloc_free(tmp_ctx);
633 display pid of a ctdb daemon
635 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
640 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
642 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
645 printf("Pid:%d\n", pid);
651 disable a remote node
653 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
657 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, NODE_FLAGS_PERMANENTLY_DISABLED, 0);
659 DEBUG(DEBUG_ERR, ("Unable to disable node %u\n", options.pnn));
667 enable a disabled remote node
669 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
673 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, 0, NODE_FLAGS_PERMANENTLY_DISABLED);
675 DEBUG(DEBUG_ERR, ("Unable to enable node %u\n", options.pnn));
683 ban a node from the cluster
685 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
688 struct ctdb_ban_info b;
696 ban_time = strtoul(argv[0], NULL, 0);
699 b.ban_time = ban_time;
701 data.dptr = (uint8_t *)&b;
702 data.dsize = sizeof(b);
704 ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_BAN_NODE, data);
706 DEBUG(DEBUG_ERR,("Failed to ban node %u\n", options.pnn));
715 unban a node from the cluster
717 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
722 data.dptr = (uint8_t *)&options.pnn;
723 data.dsize = sizeof(uint32_t);
725 ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_UNBAN_NODE, data);
727 DEBUG(DEBUG_ERR,("Failed to to unban node %u\n", options.pnn));
738 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
742 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
744 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
754 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
758 ret = ctdb_ctrl_freeze(ctdb, TIMELIMIT(), options.pnn);
760 DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
764 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
766 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
775 display monitoring mode of a remote node
777 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
782 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
784 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
787 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
793 display remote list of keys/data for a db
795 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
798 struct ctdb_db_context *ctdb_db;
806 ctdb_db = ctdb_attach(ctdb, db_name, false);
808 if (ctdb_db == NULL) {
809 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
813 /* traverse and dump the cluster tdb */
814 ret = ctdb_dump_db(ctdb_db, stdout);
816 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
819 talloc_free(ctdb_db);
821 printf("Dumped %d records\n", ret);
827 display a list of the databases on a remote ctdb
829 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
832 struct ctdb_dbid_map *dbmap=NULL;
834 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
836 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
840 printf("Number of databases:%d\n", dbmap->num);
841 for(i=0;i<dbmap->num;i++){
846 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
847 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
848 persistent = dbmap->dbs[i].persistent;
849 printf("dbid:0x%08x name:%s path:%s %s\n", dbmap->dbs[i].dbid, name,
850 path, persistent?"PERSISTENT":"");
857 check if the local node is recmaster or not
858 it will return 1 if this node is the recmaster and 0 if it is not
859 or if the local ctdb daemon could not be contacted
861 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
863 uint32_t mypnn, recmaster;
866 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
868 printf("Failed to get pnn of node\n");
872 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
874 printf("Failed to get the recmaster\n");
878 if (recmaster != mypnn) {
879 printf("this node is not the recmaster\n");
883 printf("this node is the recmaster\n");
890 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
893 struct timeval tv = timeval_current();
894 ret = ctdb_ctrl_ping(ctdb, options.pnn);
896 printf("Unable to get ping response from node %u\n", options.pnn);
898 printf("response from %u time=%.6f sec (%d clients)\n",
899 options.pnn, timeval_elapsed(&tv), ret);
908 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
919 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
921 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
925 printf("%-19s = %u\n", name, value);
932 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
943 value = strtoul(argv[1], NULL, 0);
945 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
947 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
956 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
962 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
964 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
968 for (i=0;i<count;i++) {
969 control_getvar(ctdb, 1, &list[i]);
979 const char *description;
981 {DEBUG_EMERG, "EMERG"},
982 {DEBUG_ALERT, "ALERT"},
983 {DEBUG_CRIT, "CRIT"},
985 {DEBUG_WARNING, "WARNING"},
986 {DEBUG_NOTICE, "NOTICE"},
987 {DEBUG_INFO, "INFO"},
988 {DEBUG_DEBUG, "DEBUG"}
991 static const char *get_debug_by_level(int32_t level)
995 for (i=0;i<ARRAY_SIZE(debug_levels);i++) {
996 if (debug_levels[i].level == level) {
997 return debug_levels[i].description;
1003 static int32_t get_debug_by_desc(const char *desc)
1007 for (i=0;i<ARRAY_SIZE(debug_levels);i++) {
1008 if (!strcmp(debug_levels[i].description, desc)) {
1009 return debug_levels[i].level;
1016 display debug level on a node
1018 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
1023 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
1025 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n",
1028 printf("Node %u is at debug level %s (%u)\n", options.pnn, get_debug_by_level(level), level);
1035 set debug level on a node or all nodes
1037 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
1046 if (isalpha(argv[0][0])) {
1047 level = get_debug_by_desc(argv[0]);
1049 level = strtoul(argv[0], NULL, 0);
1052 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
1054 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
1063 static int control_freeze(struct ctdb_context *ctdb, int argc, const char **argv)
1067 ret = ctdb_ctrl_freeze(ctdb, TIMELIMIT(), options.pnn);
1069 DEBUG(DEBUG_ERR, ("Unable to freeze node %u\n", options.pnn));
1077 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
1081 ret = ctdb_ctrl_thaw(ctdb, TIMELIMIT(), options.pnn);
1083 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
1090 attach to a database
1092 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
1094 const char *db_name;
1095 struct ctdb_db_context *ctdb_db;
1102 ctdb_db = ctdb_attach(ctdb, db_name, false);
1103 if (ctdb_db == NULL) {
1104 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
1114 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
1120 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1121 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
1122 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
1123 if (ret != 0 || res != 0) {
1124 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
1125 talloc_free(tmp_ctx);
1128 write(1, data.dptr, data.dsize);
1129 talloc_free(tmp_ctx);
1134 list all nodes in the cluster
1136 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
1139 struct ctdb_node_map *nodemap=NULL;
1141 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
1143 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1147 for(i=0;i<nodemap->num;i++){
1148 printf("%s\n", inet_ntoa(nodemap->nodes[i].sin.sin_addr));
1155 reload the nodes file on the local node
1157 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
1162 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
1165 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on local node You MUST fix that node manually!\n"));
1173 static const struct {
1175 int (*fn)(struct ctdb_context *, int, const char **);
1179 } ctdb_commands[] = {
1180 { "status", control_status, true, "show node status" },
1181 { "uptime", control_uptime, true, "show node uptime" },
1182 { "ping", control_ping, true, "ping all nodes" },
1183 { "getvar", control_getvar, true, "get a tunable variable", "<name>"},
1184 { "setvar", control_setvar, true, "set a tunable variable", "<name> <value>"},
1185 { "listvars", control_listvars, true, "list tunable variables"},
1186 { "statistics", control_statistics, false, "show statistics" },
1187 { "statisticsreset", control_statistics_reset, true, "reset statistics"},
1188 { "ip", control_ip, true, "show which public ip's that ctdb manages" },
1189 { "process-exists", control_process_exists, true, "check if a process exists on a node", "<pid>"},
1190 { "getdbmap", control_getdbmap, true, "show the database map" },
1191 { "catdb", control_catdb, true, "dump a database" , "<dbname>"},
1192 { "getmonmode", control_getmonmode, true, "show monitoring mode" },
1193 { "setdebug", control_setdebug, true, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
1194 { "getdebug", control_getdebug, true, "get debug level" },
1195 { "attach", control_attach, true, "attach to a database", "<dbname>" },
1196 { "dumpmemory", control_dumpmemory, true, "dump memory map to stdout" },
1197 { "getpid", control_getpid, true, "get ctdbd process ID" },
1198 { "disable", control_disable, true, "disable a nodes public IP" },
1199 { "enable", control_enable, true, "enable a nodes public IP" },
1200 { "ban", control_ban, true, "ban a node from the cluster", "<bantime|0>"},
1201 { "unban", control_unban, true, "unban a node from the cluster" },
1202 { "shutdown", control_shutdown, true, "shutdown ctdbd" },
1203 { "recover", control_recover, true, "force recovery" },
1204 { "freeze", control_freeze, true, "freeze all databases" },
1205 { "thaw", control_thaw, true, "thaw all databases" },
1206 { "isnotrecmaster", control_isnotrecmaster, false, "check if the local node is recmaster or not" },
1207 { "killtcp", kill_tcp, false, "kill a tcp connection.", "<srcip:port> <dstip:port>" },
1208 { "gratiousarp", control_gratious_arp, false, "send a gratious arp", "<ip> <interface>" },
1209 { "tickle", tickle_tcp, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
1210 { "gettickles", control_get_tickles, false, "get the list of tickles registered for this ip", "<ip>" },
1212 { "regsrvid", regsrvid, false, "register a server id", "<pnn> <type> <id>" },
1213 { "unregsrvid", unregsrvid, false, "unregister a server id", "<pnn> <type> <id>" },
1214 { "chksrvid", chksrvid, false, "check if a server id exists", "<pnn> <type> <id>" },
1215 { "getsrvids", getsrvids, false, "get a list of all server ids"},
1216 { "vacuum", ctdb_vacuum, false, "vacuum the databases of empty records", "[max_records]"},
1217 { "repack", ctdb_repack, false, "repack all databases", "[max_freelist]"},
1218 { "listnodes", control_listnodes, false, "list all nodes in the cluster"},
1219 { "reloadnodes", control_reload_nodes_file, false, "update the nodes file and restart the transport on the local node"},
1225 static void usage(void)
1229 "Usage: ctdb [options] <control>\n" \
1231 " -n <node> choose node number, or 'all' (defaults to local node)\n"
1232 " -Y generate machinereadable output\n"
1233 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
1234 printf("Controls:\n");
1235 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
1236 printf(" %-15s %-27s %s\n",
1237 ctdb_commands[i].name,
1238 ctdb_commands[i].args?ctdb_commands[i].args:"",
1239 ctdb_commands[i].msg);
1245 static void ctdb_alarm(int sig)
1247 printf("Maximum runtime exceeded - exiting\n");
1254 int main(int argc, const char *argv[])
1256 struct ctdb_context *ctdb;
1257 char *nodestring = NULL;
1258 struct poptOption popt_options[] = {
1261 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
1262 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
1263 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
1264 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
1268 const char **extra_argv;
1272 struct event_context *ev;
1273 const char *control;
1277 /* set some defaults */
1278 options.maxruntime = 0;
1279 options.timelimit = 3;
1280 options.pnn = CTDB_CURRENT_NODE;
1282 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
1284 while ((opt = poptGetNextOpt(pc)) != -1) {
1287 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
1288 poptBadOption(pc, 0), poptStrerror(opt)));
1293 /* setup the remaining options for the main program to use */
1294 extra_argv = poptGetArgs(pc);
1297 while (extra_argv[extra_argc]) extra_argc++;
1300 if (extra_argc < 1) {
1304 if (options.maxruntime != 0) {
1305 signal(SIGALRM, ctdb_alarm);
1306 alarm(options.maxruntime);
1309 /* setup the node number to contact */
1310 if (nodestring != NULL) {
1311 if (strcmp(nodestring, "all") == 0) {
1312 options.pnn = CTDB_BROADCAST_ALL;
1314 options.pnn = strtoul(nodestring, NULL, 0);
1318 control = extra_argv[0];
1320 ev = event_context_init(NULL);
1322 /* initialise ctdb */
1323 ctdb = ctdb_cmdline_client(ev);
1325 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
1329 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
1330 if (strcmp(control, ctdb_commands[i].name) == 0) {
1333 if (options.pnn == CTDB_CURRENT_NODE) {
1335 pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
1342 if (ctdb_commands[i].auto_all &&
1343 options.pnn == CTDB_BROADCAST_ALL) {
1348 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
1349 CTDB_NO_MEMORY(ctdb, nodes);
1351 for (j=0;j<num_nodes;j++) {
1352 options.pnn = nodes[j];
1353 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
1357 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
1363 if (i == ARRAY_SIZE(ctdb_commands)) {
1364 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));