537723026d04f1076f74568f1392384426a0d605
[sahlberg/ctdb.git] / tools / ctdb.c
1 /* 
2    ctdb control tool
3
4    Copyright (C) Andrew Tridgell  2007
5    Copyright (C) Ronnie Sahlberg  2007
6
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.
11    
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.
16    
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/>.
19 */
20
21 #include "includes.h"
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"
27 #include "popt.h"
28 #include "cmdline.h"
29 #include "../include/ctdb.h"
30 #include "../include/ctdb_private.h"
31
32 static void usage(void);
33
34 static struct {
35         int timelimit;
36         uint32_t pnn;
37         int machinereadable;
38         int maxruntime;
39 } options;
40
41 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
42
43 /*
44   see if a process exists
45  */
46 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
47 {
48         uint32_t pnn, pid;
49         int ret;
50         if (argc < 1) {
51                 usage();
52         }
53
54         if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
55                 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
56                 return -1;
57         }
58
59         ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
60         if (ret == 0) {
61                 printf("%u:%u exists\n", pnn, pid);
62         } else {
63                 printf("%u:%u does not exist\n", pnn, pid);
64         }
65         return ret;
66 }
67
68 /*
69   display statistics structure
70  */
71 static void show_statistics(struct ctdb_statistics *s)
72 {
73         TALLOC_CTX *tmp_ctx = talloc_new(NULL);
74         int i;
75         const char *prefix=NULL;
76         int preflen=0;
77         const struct {
78                 const char *name;
79                 uint32_t offset;
80         } fields[] = {
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),
111         };
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);
119                         }
120                 } else {
121                         preflen = 0;
122                 }
123                 printf(" %*s%-22s%*s%10u\n", 
124                        preflen?4:0, "",
125                        fields[i].name+preflen, 
126                        preflen?0:4, "",
127                        *(uint32_t *)(fields[i].offset+(uint8_t *)s));
128         }
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);
132 }
133
134 /*
135   display remote ctdb statistics combined from all nodes
136  */
137 static int control_statistics_all(struct ctdb_context *ctdb)
138 {
139         int ret, i;
140         struct ctdb_statistics statistics;
141         uint32_t *nodes;
142         uint32_t num_nodes;
143
144         nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
145         CTDB_NO_MEMORY(ctdb, nodes);
146         
147         ZERO_STRUCT(statistics);
148
149         for (i=0;i<num_nodes;i++) {
150                 struct ctdb_statistics s1;
151                 int j;
152                 uint32_t *v1 = (uint32_t *)&s1;
153                 uint32_t *v2 = (uint32_t *)&statistics;
154                 uint32_t num_ints = 
155                         offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
156                 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
157                 if (ret != 0) {
158                         DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
159                         return ret;
160                 }
161                 for (j=0;j<num_ints;j++) {
162                         v2[j] += v1[j];
163                 }
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);
170         }
171         talloc_free(nodes);
172         printf("Gathered statistics for %u nodes\n", num_nodes);
173         show_statistics(&statistics);
174         return 0;
175 }
176
177 /*
178   display remote ctdb statistics
179  */
180 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
181 {
182         int ret;
183         struct ctdb_statistics statistics;
184
185         if (options.pnn == CTDB_BROADCAST_ALL) {
186                 return control_statistics_all(ctdb);
187         }
188
189         ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
190         if (ret != 0) {
191                 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
192                 return ret;
193         }
194         show_statistics(&statistics);
195         return 0;
196 }
197
198
199 /*
200   reset remote ctdb statistics
201  */
202 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
203 {
204         int ret;
205
206         ret = ctdb_statistics_reset(ctdb, options.pnn);
207         if (ret != 0) {
208                 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
209                 return ret;
210         }
211         return 0;
212 }
213
214
215 /*
216   display uptime of remote node
217  */
218 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
219 {
220         int ret;
221         int mypnn;
222         struct ctdb_uptime *uptime = NULL;
223         int tmp, days, hours, minutes, seconds;
224
225         mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
226         if (mypnn == -1) {
227                 return -1;
228         }
229
230         ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
231         if (ret != 0) {
232                 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
233                 return ret;
234         }
235
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);
242                 return 0;
243         }
244
245         printf("Current time of node  : %s", ctime(&uptime->current_time.tv_sec));
246
247         tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
248         seconds = tmp%60;
249         tmp    /= 60;
250         minutes = tmp%60;
251         tmp    /= 60;
252         hours   = tmp%24;
253         tmp    /= 24;
254         days    = tmp;
255         printf("Ctdbd start time      : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
256
257         tmp = uptime->current_time.tv_sec - uptime->last_recovery_time.tv_sec;
258         seconds = tmp%60;
259         tmp    /= 60;
260         minutes = tmp%60;
261         tmp    /= 60;
262         hours   = tmp%24;
263         tmp    /= 24;
264         days    = tmp;
265         printf("Time of last recovery : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_time.tv_sec));
266
267         return 0;
268 }
269
270 /*
271   display remote ctdb status
272  */
273 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
274 {
275         int i, ret;
276         struct ctdb_vnn_map *vnnmap=NULL;
277         struct ctdb_node_map *nodemap=NULL;
278         uint32_t recmode, recmaster;
279         int mypnn;
280
281         mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
282         if (mypnn == -1) {
283                 return -1;
284         }
285
286         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
287         if (ret != 0) {
288                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
289                 return ret;
290         }
291
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));
301                 }
302                 return 0;
303         }
304
305         printf("Number of nodes:%d\n", nodemap->num);
306         for(i=0;i<nodemap->num;i++){
307                 static const struct {
308                         uint32_t flag;
309                         const char *name;
310                 } flag_names[] = {
311                         { NODE_FLAGS_DISCONNECTED,          "DISCONNECTED" },
312                         { NODE_FLAGS_PERMANENTLY_DISABLED,  "DISABLED" },
313                         { NODE_FLAGS_BANNED,                "BANNED" },
314                         { NODE_FLAGS_UNHEALTHY,             "UNHEALTHY" },
315                 };
316                 char *flags_str = NULL;
317                 int j;
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);
322                                 } else {
323                                         flags_str = talloc_asprintf_append(flags_str, "|%s",
324                                                                            flag_names[j].name);
325                                 }
326                                 CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
327                         }
328                 }
329                 if (flags_str == NULL) {
330                         flags_str = talloc_strdup(ctdb, "OK");
331                         CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
332                 }
333                 printf("pnn:%d %-16s %s%s\n", nodemap->nodes[i].pnn,
334                        inet_ntoa(nodemap->nodes[i].sin.sin_addr),
335                        flags_str,
336                        nodemap->nodes[i].pnn == mypnn?" (THIS NODE)":"");
337                 talloc_free(flags_str);
338         }
339
340         ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &vnnmap);
341         if (ret != 0) {
342                 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
343                 return ret;
344         }
345         if (vnnmap->generation == INVALID_GENERATION) {
346                 printf("Generation:INVALID\n");
347         } else {
348                 printf("Generation:%d\n",vnnmap->generation);
349         }
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]);
353         }
354
355         ret = ctdb_ctrl_getrecmode(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmode);
356         if (ret != 0) {
357                 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
358                 return ret;
359         }
360         printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
361
362         ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
363         if (ret != 0) {
364                 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
365                 return ret;
366         }
367         printf("Recovery master:%d\n",recmaster);
368
369         return 0;
370 }
371
372 /*
373   get a list of all tickles for this pnn
374  */
375 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
376 {
377         struct ctdb_control_tcp_tickle_list *list;
378         struct sockaddr_in ip;
379         int i, ret;
380
381         if (argc < 1) {
382                 usage();
383         }
384
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]));
388                 return -1;
389         }
390
391         ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &ip, &list);
392         if (ret == -1) {
393                 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
394                 return -1;
395         }
396
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));
402         }
403
404         talloc_free(list);
405         
406         return 0;
407 }
408
409 /*
410   move/failover an ip address to a specific node
411  */
412 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
413 {
414         uint32_t pnn;
415         struct sockaddr_in ip;
416         uint32_t value;
417         struct ctdb_all_public_ips *ips;
418         struct ctdb_public_ip pip;
419         TDB_DATA data;
420         struct ctdb_node_map *nodemap=NULL;
421         int i, ret;
422
423         if (argc < 2) {
424                 usage();
425         }
426
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]));
430                 return -1;
431         }
432
433
434         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
435         if (ret != 0) {
436                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
437                 return ret;
438         }
439
440         if (sscanf(argv[1], "%u", &pnn) != 1) {
441                 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
442                 return -1;
443         }
444
445         ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, "DeterministicIPs", &value);
446         if (ret == -1) {
447                 DEBUG(DEBUG_ERR, ("Unable to get tunable variable 'DeterministicIPs' from local node\n"));
448                 return -1;
449         }
450         if (value != 0) {
451                 DEBUG(DEBUG_ERR, ("The tunable 'DeterministicIPs' is set. You can only move ip addresses when this feature is disabled\n"));
452                 return -1;
453         }
454
455         ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, "NoIPFailback", &value);
456         if (ret == -1) {
457                 DEBUG(DEBUG_ERR, ("Unable to get tunable variable 'NoIPFailback' from local node\n"));
458                 return -1;
459         }
460         if (value == 0) {
461                 DEBUG(DEBUG_ERR, ("The tunable 'NoIPFailback' is NOT set. You can only move ip addresses when this feature is enabled\n"));
462                 return -1;
463         }
464
465         /* read the public ip list from the node */
466         ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
467         if (ret != 0) {
468                 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
469                 return -1;
470         }
471
472         for (i=0;i<ips->num;i++) {
473                 if (ctdb_same_ip(&ip, &ips->ips[i].sin)) {
474                         break;
475                 }
476         }
477         if (i==ips->num) {
478                 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
479                         pnn, inet_ntoa(ip.sin_addr)));
480                 return -1;
481         }
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)));
485                 return -1;
486         }
487
488         /* send a moveip message to the recovery master */
489         pip.pnn = pnn;
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;
494
495
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"));
501                 return -1;
502         }
503
504         return 0;
505 }
506
507 /*
508   add a public ip address to a node
509  */
510 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
511 {
512         int ret;
513         int len;
514         unsigned mask;
515         struct sockaddr_in addr;
516         struct ctdb_control_ip_iface *pub;
517
518         if (argc != 2) {
519                 usage();
520         }
521
522         if (!parse_ip_mask(argv[0], &addr, &mask)) {
523                 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
524                 return -1;
525         }
526
527         len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]);
528         pub = talloc_size(ctdb, len); 
529         CTDB_NO_MEMORY(ctdb, pub);
530
531         pub->sin   = addr;
532         pub->mask  = mask;
533         pub->len   = strlen(argv[1])+1;
534         memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
535
536         ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
537         if (ret != 0) {
538                 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u\n", options.pnn));
539                 return ret;
540         }
541
542         return 0;
543 }
544
545 /*
546   delete a public ip address from a node
547  */
548 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
549 {
550         int ret;
551         struct sockaddr_in addr;
552         struct ctdb_control_ip_iface pub;
553
554         if (argc != 1) {
555                 usage();
556         }
557
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]));
561                 return -1;
562         }
563
564         pub.sin   = addr;
565         pub.mask  = 0;
566         pub.len   = 0;
567
568         ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
569         if (ret != 0) {
570                 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
571                 return ret;
572         }
573
574         return 0;
575 }
576
577 /*
578   kill a tcp connection
579  */
580 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
581 {
582         int ret;
583         struct ctdb_control_killtcp killtcp;
584
585         if (argc < 2) {
586                 usage();
587         }
588
589         if (!parse_ip_port(argv[0], &killtcp.src)) {
590                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
591                 return -1;
592         }
593
594         if (!parse_ip_port(argv[1], &killtcp.dst)) {
595                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
596                 return -1;
597         }
598
599         ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
600         if (ret != 0) {
601                 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
602                 return ret;
603         }
604
605         return 0;
606 }
607
608
609 /*
610   send a gratious arp
611  */
612 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
613 {
614         int ret;
615         struct sockaddr_in sin;
616
617         if (argc < 2) {
618                 usage();
619         }
620
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]));
624                 return -1;
625         }
626
627         ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &sin, argv[1]);
628         if (ret != 0) {
629                 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
630                 return ret;
631         }
632
633         return 0;
634 }
635
636 /*
637   register a server id
638  */
639 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
640 {
641         int ret;
642         struct ctdb_server_id server_id;
643
644         if (argc < 3) {
645                 usage();
646         }
647
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);
651
652         ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
653         if (ret != 0) {
654                 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
655                 return ret;
656         }
657         return -1;
658 }
659
660 /*
661   unregister a server id
662  */
663 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
664 {
665         int ret;
666         struct ctdb_server_id server_id;
667
668         if (argc < 3) {
669                 usage();
670         }
671
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);
675
676         ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
677         if (ret != 0) {
678                 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
679                 return ret;
680         }
681         return -1;
682 }
683
684 /*
685   check if a server id exists
686  */
687 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
688 {
689         uint32_t status;
690         int ret;
691         struct ctdb_server_id server_id;
692
693         if (argc < 3) {
694                 usage();
695         }
696
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);
700
701         ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
702         if (ret != 0) {
703                 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
704                 return ret;
705         }
706
707         if (status) {
708                 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
709         } else {
710                 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
711         }
712         return 0;
713 }
714
715 /*
716   get a list of all server ids that are registered on a node
717  */
718 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
719 {
720         int i, ret;
721         struct ctdb_server_id_list *server_ids;
722
723         ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
724         if (ret != 0) {
725                 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
726                 return ret;
727         }
728
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); 
734         }
735
736         return -1;
737 }
738
739 /*
740   send a tcp tickle ack
741  */
742 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
743 {
744         int s, ret;
745         struct sockaddr_in src, dst;
746
747         if (argc < 2) {
748                 usage();
749         }
750
751         if (!parse_ip_port(argv[0], &src)) {
752                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
753                 return -1;
754         }
755
756         if (!parse_ip_port(argv[1], &dst)) {
757                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
758                 return -1;
759         }
760
761         s = ctdb_sys_open_sending_socket();
762         if (s == -1) {
763                 DEBUG(DEBUG_ERR, ("Failed to open socket for sending tickle\n"));
764                 return 0;
765         }
766
767         ret = ctdb_sys_send_tcp(s, &src, &dst, 0, 0, 0);
768         close(s);
769         if (ret==0) {
770                 return 0;
771         }
772         DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
773
774         return -1;
775 }
776
777
778 /*
779   display public ip status
780  */
781 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
782 {
783         int i, ret;
784         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
785         struct ctdb_all_public_ips *ips;
786
787         /* read the public ip list from this node */
788         ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
789         if (ret != 0) {
790                 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
791                 talloc_free(tmp_ctx);
792                 return ret;
793         }
794
795         if (options.machinereadable){
796                 printf(":Public IP:Node:\n");
797         } else {
798                 printf("Public IPs on node %u\n", options.pnn);
799         }
800
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);
804                 } else {
805                         printf("%s %d\n", inet_ntoa(ips->ips[ips->num-i].sin.sin_addr), ips->ips[ips->num-i].pnn);
806                 }
807         }
808
809         talloc_free(tmp_ctx);
810         return 0;
811 }
812
813 /*
814   display pid of a ctdb daemon
815  */
816 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
817 {
818         uint32_t pid;
819         int ret;
820
821         ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
822         if (ret != 0) {
823                 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
824                 return ret;
825         }
826         printf("Pid:%d\n", pid);
827
828         return 0;
829 }
830
831 /*
832   disable a remote node
833  */
834 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
835 {
836         int ret;
837
838         ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, NODE_FLAGS_PERMANENTLY_DISABLED, 0);
839         if (ret != 0) {
840                 DEBUG(DEBUG_ERR, ("Unable to disable node %u\n", options.pnn));
841                 return ret;
842         }
843
844         return 0;
845 }
846
847 /*
848   enable a disabled remote node
849  */
850 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
851 {
852         int ret;
853
854         ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, 0, NODE_FLAGS_PERMANENTLY_DISABLED);
855         if (ret != 0) {
856                 DEBUG(DEBUG_ERR, ("Unable to enable node %u\n", options.pnn));
857                 return ret;
858         }
859
860         return 0;
861 }
862
863 /*
864   ban a node from the cluster
865  */
866 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
867 {
868         int ret;
869         struct ctdb_ban_info b;
870         TDB_DATA data;
871         uint32_t ban_time;
872
873         if (argc < 1) {
874                 usage();
875         }
876
877         ban_time = strtoul(argv[0], NULL, 0);
878
879         b.pnn = options.pnn;
880         b.ban_time = ban_time;
881
882         data.dptr = (uint8_t *)&b;
883         data.dsize = sizeof(b);
884
885         ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_BAN_NODE, data);
886         if (ret != 0) {
887                 DEBUG(DEBUG_ERR,("Failed to ban node %u\n", options.pnn));
888                 return -1;
889         }
890         
891         return 0;
892 }
893
894
895 /*
896   unban a node from the cluster
897  */
898 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
899 {
900         int ret;
901         TDB_DATA data;
902
903         data.dptr = (uint8_t *)&options.pnn;
904         data.dsize = sizeof(uint32_t);
905
906         ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_UNBAN_NODE, data);
907         if (ret != 0) {
908                 DEBUG(DEBUG_ERR,("Failed to to unban node %u\n", options.pnn));
909                 return -1;
910         }
911         
912         return 0;
913 }
914
915
916 /*
917   shutdown a daemon
918  */
919 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
920 {
921         int ret;
922
923         ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
924         if (ret != 0) {
925                 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
926                 return ret;
927         }
928
929         return 0;
930 }
931
932 /*
933   trigger a recovery
934  */
935 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
936 {
937         int ret;
938
939         ret = ctdb_ctrl_freeze(ctdb, TIMELIMIT(), options.pnn);
940         if (ret != 0) {
941                 DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
942                 return ret;
943         }
944
945         ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
946         if (ret != 0) {
947                 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
948                 return ret;
949         }
950
951         return 0;
952 }
953
954
955 /*
956   display monitoring mode of a remote node
957  */
958 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
959 {
960         uint32_t monmode;
961         int ret;
962
963         ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
964         if (ret != 0) {
965                 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
966                 return ret;
967         }
968         printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
969
970         return 0;
971 }
972
973
974 /*
975   disable monitoring on a  node
976  */
977 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
978 {
979         
980         int ret;
981
982         ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
983         if (ret != 0) {
984                 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
985                 return ret;
986         }
987         printf("Monitoring mode:%s\n","DISABLED");
988
989         return 0;
990 }
991
992 /*
993   enable monitoring on a  node
994  */
995 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
996 {
997         
998         int ret;
999
1000         ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
1001         if (ret != 0) {
1002                 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
1003                 return ret;
1004         }
1005         printf("Monitoring mode:%s\n","ACTIVE");
1006
1007         return 0;
1008 }
1009
1010 /*
1011   display remote list of keys/data for a db
1012  */
1013 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
1014 {
1015         const char *db_name;
1016         struct ctdb_db_context *ctdb_db;
1017         int ret;
1018
1019         if (argc < 1) {
1020                 usage();
1021         }
1022
1023         db_name = argv[0];
1024         ctdb_db = ctdb_attach(ctdb, db_name, false);
1025
1026         if (ctdb_db == NULL) {
1027                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
1028                 return -1;
1029         }
1030
1031         /* traverse and dump the cluster tdb */
1032         ret = ctdb_dump_db(ctdb_db, stdout);
1033         if (ret == -1) {
1034                 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
1035                 return -1;
1036         }
1037         talloc_free(ctdb_db);
1038
1039         printf("Dumped %d records\n", ret);
1040         return 0;
1041 }
1042
1043
1044 /*
1045   display a list of the databases on a remote ctdb
1046  */
1047 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
1048 {
1049         int i, ret;
1050         struct ctdb_dbid_map *dbmap=NULL;
1051
1052         ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
1053         if (ret != 0) {
1054                 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
1055                 return ret;
1056         }
1057
1058         printf("Number of databases:%d\n", dbmap->num);
1059         for(i=0;i<dbmap->num;i++){
1060                 const char *path;
1061                 const char *name;
1062                 bool persistent;
1063
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":"");
1069         }
1070
1071         return 0;
1072 }
1073
1074 /*
1075   get the filename of the reclock file
1076  */
1077 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
1078 {
1079         int i, ret, fd;
1080         const char *reclock;
1081         struct ctdb_node_map *nodemap=NULL;
1082         char *pnnfile;
1083
1084         ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
1085         if (ret != 0) {
1086                 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
1087                 return ret;
1088         }
1089
1090         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
1091         if (ret != 0) {
1092                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1093                 return ret;
1094         }
1095
1096
1097         pnnfile = talloc_asprintf(ctdb, "%s.pnn", reclock);
1098         CTDB_NO_MEMORY(ctdb, pnnfile);
1099
1100         fd = open(pnnfile, O_RDONLY);
1101         if (fd == -1) {
1102                 DEBUG(DEBUG_CRIT,(__location__ " Failed to open reclock pnn file %s - (%s)\n", 
1103                          pnnfile, strerror(errno)));
1104                 exit(10);
1105         }
1106
1107
1108         printf("Reclock file : %s\n", reclock);
1109         for (i=0; i<nodemap->num; i++) {
1110                 int count;
1111
1112                 count = ctdb_read_pnn_lock(fd, nodemap->nodes[i].pnn);
1113
1114                 printf("pnn:%d %-16s", nodemap->nodes[i].pnn,
1115                        inet_ntoa(nodemap->nodes[i].sin.sin_addr));
1116                 if (count == -1) {
1117                         printf(" NOT ACTIVE\n");
1118                 } else {
1119                         printf(" ACTIVE with %d connections\n", count);
1120                 }
1121         }
1122
1123         close(fd);
1124         return 0;
1125 }
1126
1127
1128 /*
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
1132  */
1133 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1134 {
1135         uint32_t mypnn, recmaster;
1136         int ret;
1137
1138         mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
1139         if (mypnn == -1) {
1140                 printf("Failed to get pnn of node\n");
1141                 return 1;
1142         }
1143
1144         ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
1145         if (ret != 0) {
1146                 printf("Failed to get the recmaster\n");
1147                 return 1;
1148         }
1149
1150         if (recmaster != mypnn) {
1151                 printf("this node is not the recmaster\n");
1152                 return 1;
1153         }
1154
1155         printf("this node is the recmaster\n");
1156         return 0;
1157 }
1158
1159 /*
1160   ping a node
1161  */
1162 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
1163 {
1164         int ret;
1165         struct timeval tv = timeval_current();
1166         ret = ctdb_ctrl_ping(ctdb, options.pnn);
1167         if (ret == -1) {
1168                 printf("Unable to get ping response from node %u\n", options.pnn);
1169         } else {
1170                 printf("response from %u time=%.6f sec  (%d clients)\n", 
1171                        options.pnn, timeval_elapsed(&tv), ret);
1172         }
1173         return 0;
1174 }
1175
1176
1177 /*
1178   get a tunable
1179  */
1180 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
1181 {
1182         const char *name;
1183         uint32_t value;
1184         int ret;
1185
1186         if (argc < 1) {
1187                 usage();
1188         }
1189
1190         name = argv[0];
1191         ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
1192         if (ret == -1) {
1193                 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
1194                 return -1;
1195         }
1196
1197         printf("%-19s = %u\n", name, value);
1198         return 0;
1199 }
1200
1201 /*
1202   set a tunable
1203  */
1204 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
1205 {
1206         const char *name;
1207         uint32_t value;
1208         int ret;
1209
1210         if (argc < 2) {
1211                 usage();
1212         }
1213
1214         name = argv[0];
1215         value = strtoul(argv[1], NULL, 0);
1216
1217         ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
1218         if (ret == -1) {
1219                 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
1220                 return -1;
1221         }
1222         return 0;
1223 }
1224
1225 /*
1226   list all tunables
1227  */
1228 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
1229 {
1230         uint32_t count;
1231         const char **list;
1232         int ret, i;
1233
1234         ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
1235         if (ret == -1) {
1236                 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
1237                 return -1;
1238         }
1239
1240         for (i=0;i<count;i++) {
1241                 control_getvar(ctdb, 1, &list[i]);
1242         }
1243
1244         talloc_free(list);
1245         
1246         return 0;
1247 }
1248
1249 static struct {
1250         int32_t level;
1251         const char *description;
1252 } debug_levels[] = {
1253         {DEBUG_EMERG,   "EMERG"},
1254         {DEBUG_ALERT,   "ALERT"},
1255         {DEBUG_CRIT,    "CRIT"},
1256         {DEBUG_ERR,     "ERR"},
1257         {DEBUG_WARNING, "WARNING"},
1258         {DEBUG_NOTICE,  "NOTICE"},
1259         {DEBUG_INFO,    "INFO"},
1260         {DEBUG_DEBUG,   "DEBUG"}
1261 };
1262
1263 static const char *get_debug_by_level(int32_t level)
1264 {
1265         int i;
1266
1267         for (i=0;i<ARRAY_SIZE(debug_levels);i++) {
1268                 if (debug_levels[i].level == level) {
1269                         return debug_levels[i].description;
1270                 }
1271         }
1272         return "Unknown";
1273 }
1274
1275 static int32_t get_debug_by_desc(const char *desc)
1276 {
1277         int i;
1278
1279         for (i=0;i<ARRAY_SIZE(debug_levels);i++) {
1280                 if (!strcmp(debug_levels[i].description, desc)) {
1281                         return debug_levels[i].level;
1282                 }
1283         }
1284         return DEBUG_ERR;
1285 }
1286
1287 /*
1288   display debug level on a node
1289  */
1290 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
1291 {
1292         int ret;
1293         int32_t level;
1294
1295         ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
1296         if (ret != 0) {
1297                 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
1298                 return ret;
1299         } else {
1300                 if (options.machinereadable){
1301                         printf(":Name:Level:\n");
1302                         printf(":%s:%d:\n",get_debug_by_level(level),level);
1303                 } else {
1304                         printf("Node %u is at debug level %s (%u)\n", options.pnn, get_debug_by_level(level), level);
1305                 }
1306         }
1307         return 0;
1308 }
1309
1310
1311 /*
1312   set debug level on a node or all nodes
1313  */
1314 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
1315 {
1316         int ret;
1317         uint32_t level;
1318
1319         if (argc < 1) {
1320                 usage();
1321         }
1322
1323         if (isalpha(argv[0][0])) { 
1324                 level = get_debug_by_desc(argv[0]);
1325         } else {
1326                 level = strtoul(argv[0], NULL, 0);
1327         }
1328
1329         ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
1330         if (ret != 0) {
1331                 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
1332         }
1333         return 0;
1334 }
1335
1336
1337 /*
1338   freeze a node
1339  */
1340 static int control_freeze(struct ctdb_context *ctdb, int argc, const char **argv)
1341 {
1342         int ret;
1343
1344         ret = ctdb_ctrl_freeze(ctdb, TIMELIMIT(), options.pnn);
1345         if (ret != 0) {
1346                 DEBUG(DEBUG_ERR, ("Unable to freeze node %u\n", options.pnn));
1347         }               
1348         return 0;
1349 }
1350
1351 /*
1352   thaw a node
1353  */
1354 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
1355 {
1356         int ret;
1357
1358         ret = ctdb_ctrl_thaw(ctdb, TIMELIMIT(), options.pnn);
1359         if (ret != 0) {
1360                 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
1361         }               
1362         return 0;
1363 }
1364
1365
1366 /*
1367   attach to a database
1368  */
1369 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
1370 {
1371         const char *db_name;
1372         struct ctdb_db_context *ctdb_db;
1373
1374         if (argc < 1) {
1375                 usage();
1376         }
1377         db_name = argv[0];
1378
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));
1382                 return -1;
1383         }
1384
1385         return 0;
1386 }
1387
1388 /*
1389   dump memory usage
1390  */
1391 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
1392 {
1393         TDB_DATA data;
1394         int ret;
1395         int32_t res;
1396         char *errmsg;
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);
1403                 return -1;
1404         }
1405         write(1, data.dptr, data.dsize);
1406         talloc_free(tmp_ctx);
1407         return 0;
1408 }
1409
1410 /*
1411   handler for memory dumps
1412 */
1413 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid, 
1414                              TDB_DATA data, void *private_data)
1415 {
1416         write(1, data.dptr, data.dsize);
1417         exit(0);
1418 }
1419
1420 /*
1421   dump memory usage on the recovery daemon
1422  */
1423 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
1424 {
1425         int ret;
1426         TDB_DATA data;
1427         struct rd_memdump_reply rd;
1428
1429         rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
1430         if (rd.pnn == -1) {
1431                 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
1432                 return -1;
1433         }
1434         rd.srvid = getpid();
1435
1436         /* register a message port for receiveing the reply so that we
1437            can receive the reply
1438         */
1439         ctdb_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
1440
1441
1442         data.dptr = (uint8_t *)&rd;
1443         data.dsize = sizeof(rd);
1444
1445         ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
1446         if (ret != 0) {
1447                 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
1448                 return -1;
1449         }
1450
1451         /* this loop will terminate when we have received the reply */
1452         while (1) {     
1453                 event_loop_once(ctdb->ev);
1454         }
1455
1456         return 0;
1457 }
1458
1459 /*
1460   list all nodes in the cluster
1461  */
1462 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
1463 {
1464         int i, ret;
1465         struct ctdb_node_map *nodemap=NULL;
1466
1467         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
1468         if (ret != 0) {
1469                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1470                 return ret;
1471         }
1472
1473         for(i=0;i<nodemap->num;i++){
1474                 printf("%s\n", inet_ntoa(nodemap->nodes[i].sin.sin_addr));
1475         }
1476
1477         return 0;
1478 }
1479
1480 /*
1481   reload the nodes file on the local node
1482  */
1483 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
1484 {
1485         int i, ret;
1486         int mypnn;
1487         struct ctdb_node_map *nodemap=NULL;
1488
1489         mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
1490         if (mypnn == -1) {
1491                 DEBUG(DEBUG_ERR, ("Failed to read pnn of local node\n"));
1492                 return -1;
1493         }
1494
1495         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
1496         if (ret != 0) {
1497                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
1498                 return ret;
1499         }
1500
1501         /* reload the nodes file on all remote nodes */
1502         for (i=0;i<nodemap->num;i++) {
1503                 if (nodemap->nodes[i].pnn == mypnn) {
1504                         continue;
1505                 }
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);
1509                 if (ret != 0) {
1510                         DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
1511                 }
1512         }
1513
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);
1517         if (ret != 0) {
1518                 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
1519         }
1520
1521         return 0;
1522 }
1523
1524
1525 static const struct {
1526         const char *name;
1527         int (*fn)(struct ctdb_context *, int, const char **);
1528         bool auto_all;
1529         const char *msg;
1530         const char *args;
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>" },
1566
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>"},
1579 };
1580
1581 /*
1582   show usage message
1583  */
1584 static void usage(void)
1585 {
1586         int i;
1587         printf(
1588 "Usage: ctdb [options] <control>\n" \
1589 "Options:\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);
1599         }
1600         exit(1);
1601 }
1602
1603
1604 static void ctdb_alarm(int sig)
1605 {
1606         printf("Maximum runtime exceeded - exiting\n");
1607         _exit(0);
1608 }
1609
1610 /*
1611   main program
1612 */
1613 int main(int argc, const char *argv[])
1614 {
1615         struct ctdb_context *ctdb;
1616         char *nodestring = NULL;
1617         struct poptOption popt_options[] = {
1618                 POPT_AUTOHELP
1619                 POPT_CTDB_CMDLINE
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" },
1624                 POPT_TABLEEND
1625         };
1626         int opt;
1627         const char **extra_argv;
1628         int extra_argc = 0;
1629         int ret=-1, i;
1630         poptContext pc;
1631         struct event_context *ev;
1632         const char *control;
1633
1634         setlinebuf(stdout);
1635         
1636         /* set some defaults */
1637         options.maxruntime = 0;
1638         options.timelimit = 3;
1639         options.pnn = CTDB_CURRENT_NODE;
1640
1641         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
1642
1643         while ((opt = poptGetNextOpt(pc)) != -1) {
1644                 switch (opt) {
1645                 default:
1646                         DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n", 
1647                                 poptBadOption(pc, 0), poptStrerror(opt)));
1648                         exit(1);
1649                 }
1650         }
1651
1652         /* setup the remaining options for the main program to use */
1653         extra_argv = poptGetArgs(pc);
1654         if (extra_argv) {
1655                 extra_argv++;
1656                 while (extra_argv[extra_argc]) extra_argc++;
1657         }
1658
1659         if (extra_argc < 1) {
1660                 usage();
1661         }
1662
1663         if (options.maxruntime != 0) {
1664                 signal(SIGALRM, ctdb_alarm);
1665                 alarm(options.maxruntime);
1666         }
1667
1668         /* setup the node number to contact */
1669         if (nodestring != NULL) {
1670                 if (strcmp(nodestring, "all") == 0) {
1671                         options.pnn = CTDB_BROADCAST_ALL;
1672                 } else {
1673                         options.pnn = strtoul(nodestring, NULL, 0);
1674                 }
1675         }
1676
1677         control = extra_argv[0];
1678
1679         ev = event_context_init(NULL);
1680
1681         /* initialise ctdb */
1682         ctdb = ctdb_cmdline_client(ev);
1683         if (ctdb == NULL) {
1684                 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
1685                 exit(1);
1686         }
1687
1688         for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
1689                 if (strcmp(control, ctdb_commands[i].name) == 0) {
1690                         int j;
1691
1692                         if (options.pnn == CTDB_CURRENT_NODE) {
1693                                 int pnn;
1694                                 pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);         
1695                                 if (pnn == -1) {
1696                                         return -1;
1697                                 }
1698                                 options.pnn = pnn;
1699                         }
1700
1701                         if (ctdb_commands[i].auto_all && 
1702                             options.pnn == CTDB_BROADCAST_ALL) {
1703                                 uint32_t *nodes;
1704                                 uint32_t num_nodes;
1705                                 ret = 0;
1706
1707                                 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
1708                                 CTDB_NO_MEMORY(ctdb, nodes);
1709         
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);
1713                                 }
1714                                 talloc_free(nodes);
1715                         } else {
1716                                 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
1717                         }
1718                         break;
1719                 }
1720         }
1721
1722         if (i == ARRAY_SIZE(ctdb_commands)) {
1723                 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
1724                 exit(1);
1725         }
1726
1727         return ret;
1728 }