merge from ronnie
[ambi/samba-autobuild/.git] / ctdb / 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 "popt.h"
27 #include "cmdline.h"
28 #include "../include/ctdb.h"
29 #include "../include/ctdb_private.h"
30
31 static void usage(void);
32
33 static struct {
34         int timelimit;
35         uint32_t pnn;
36         int machinereadable;
37         int maxruntime;
38 } options;
39
40 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
41
42 /*
43   see if a process exists
44  */
45 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
46 {
47         uint32_t pnn, pid;
48         int ret;
49         if (argc < 1) {
50                 usage();
51         }
52
53         if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
54                 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
55                 return -1;
56         }
57
58         ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
59         if (ret == 0) {
60                 printf("%u:%u exists\n", pnn, pid);
61         } else {
62                 printf("%u:%u does not exist\n", pnn, pid);
63         }
64         return ret;
65 }
66
67 /*
68   display statistics structure
69  */
70 static void show_statistics(struct ctdb_statistics *s)
71 {
72         TALLOC_CTX *tmp_ctx = talloc_new(NULL);
73         int i;
74         const char *prefix=NULL;
75         int preflen=0;
76         const struct {
77                 const char *name;
78                 uint32_t offset;
79         } fields[] = {
80 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
81                 STATISTICS_FIELD(num_clients),
82                 STATISTICS_FIELD(frozen),
83                 STATISTICS_FIELD(recovering),
84                 STATISTICS_FIELD(client_packets_sent),
85                 STATISTICS_FIELD(client_packets_recv),
86                 STATISTICS_FIELD(node_packets_sent),
87                 STATISTICS_FIELD(node_packets_recv),
88                 STATISTICS_FIELD(keepalive_packets_sent),
89                 STATISTICS_FIELD(keepalive_packets_recv),
90                 STATISTICS_FIELD(node.req_call),
91                 STATISTICS_FIELD(node.reply_call),
92                 STATISTICS_FIELD(node.req_dmaster),
93                 STATISTICS_FIELD(node.reply_dmaster),
94                 STATISTICS_FIELD(node.reply_error),
95                 STATISTICS_FIELD(node.req_message),
96                 STATISTICS_FIELD(node.req_control),
97                 STATISTICS_FIELD(node.reply_control),
98                 STATISTICS_FIELD(client.req_call),
99                 STATISTICS_FIELD(client.req_message),
100                 STATISTICS_FIELD(client.req_control),
101                 STATISTICS_FIELD(timeouts.call),
102                 STATISTICS_FIELD(timeouts.control),
103                 STATISTICS_FIELD(timeouts.traverse),
104                 STATISTICS_FIELD(total_calls),
105                 STATISTICS_FIELD(pending_calls),
106                 STATISTICS_FIELD(lockwait_calls),
107                 STATISTICS_FIELD(pending_lockwait_calls),
108                 STATISTICS_FIELD(memory_used),
109                 STATISTICS_FIELD(max_hop_count),
110         };
111         printf("CTDB version %u\n", CTDB_VERSION);
112         for (i=0;i<ARRAY_SIZE(fields);i++) {
113                 if (strchr(fields[i].name, '.')) {
114                         preflen = strcspn(fields[i].name, ".")+1;
115                         if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
116                                 prefix = fields[i].name;
117                                 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
118                         }
119                 } else {
120                         preflen = 0;
121                 }
122                 printf(" %*s%-22s%*s%10u\n", 
123                        preflen?4:0, "",
124                        fields[i].name+preflen, 
125                        preflen?0:4, "",
126                        *(uint32_t *)(fields[i].offset+(uint8_t *)s));
127         }
128         printf(" %-30s     %.6f sec\n", "max_call_latency", s->max_call_latency);
129         printf(" %-30s     %.6f sec\n", "max_lockwait_latency", s->max_lockwait_latency);
130         talloc_free(tmp_ctx);
131 }
132
133 /*
134   display remote ctdb statistics combined from all nodes
135  */
136 static int control_statistics_all(struct ctdb_context *ctdb)
137 {
138         int ret, i;
139         struct ctdb_statistics statistics;
140         uint32_t *nodes;
141         uint32_t num_nodes;
142
143         nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
144         CTDB_NO_MEMORY(ctdb, nodes);
145         
146         ZERO_STRUCT(statistics);
147
148         for (i=0;i<num_nodes;i++) {
149                 struct ctdb_statistics s1;
150                 int j;
151                 uint32_t *v1 = (uint32_t *)&s1;
152                 uint32_t *v2 = (uint32_t *)&statistics;
153                 uint32_t num_ints = 
154                         offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
155                 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
156                 if (ret != 0) {
157                         DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
158                         return ret;
159                 }
160                 for (j=0;j<num_ints;j++) {
161                         v2[j] += v1[j];
162                 }
163                 statistics.max_hop_count = 
164                         MAX(statistics.max_hop_count, s1.max_hop_count);
165                 statistics.max_call_latency = 
166                         MAX(statistics.max_call_latency, s1.max_call_latency);
167                 statistics.max_lockwait_latency = 
168                         MAX(statistics.max_lockwait_latency, s1.max_lockwait_latency);
169         }
170         talloc_free(nodes);
171         printf("Gathered statistics for %u nodes\n", num_nodes);
172         show_statistics(&statistics);
173         return 0;
174 }
175
176 /*
177   display remote ctdb statistics
178  */
179 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
180 {
181         int ret;
182         struct ctdb_statistics statistics;
183
184         if (options.pnn == CTDB_BROADCAST_ALL) {
185                 return control_statistics_all(ctdb);
186         }
187
188         ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
189         if (ret != 0) {
190                 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
191                 return ret;
192         }
193         show_statistics(&statistics);
194         return 0;
195 }
196
197
198 /*
199   reset remote ctdb statistics
200  */
201 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
202 {
203         int ret;
204
205         ret = ctdb_statistics_reset(ctdb, options.pnn);
206         if (ret != 0) {
207                 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
208                 return ret;
209         }
210         return 0;
211 }
212
213
214 /*
215   display uptime of remote node
216  */
217 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
218 {
219         int ret;
220         int mypnn;
221         struct ctdb_uptime *uptime = NULL;
222         int tmp, days, hours, minutes, seconds;
223
224         mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
225         if (mypnn == -1) {
226                 return -1;
227         }
228
229         ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
230         if (ret != 0) {
231                 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
232                 return ret;
233         }
234
235         printf("Current time of node  : %s", ctime(&uptime->current_time.tv_sec));
236
237         tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
238         seconds = tmp%60;
239         tmp    /= 60;
240         minutes = tmp%60;
241         tmp    /= 60;
242         hours   = tmp%24;
243         tmp    /= 24;
244         days    = tmp;
245         printf("Ctdbd start time      : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
246
247         tmp = uptime->current_time.tv_sec - uptime->last_recovery_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("Time of last recovery : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_time.tv_sec));
256
257         return 0;
258 }
259
260 /*
261   display remote ctdb status
262  */
263 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
264 {
265         int i, ret;
266         struct ctdb_vnn_map *vnnmap=NULL;
267         struct ctdb_node_map *nodemap=NULL;
268         uint32_t recmode, recmaster;
269         int mypnn;
270
271         mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
272         if (mypnn == -1) {
273                 return -1;
274         }
275
276         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
277         if (ret != 0) {
278                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
279                 return ret;
280         }
281
282         if(options.machinereadable){
283                 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:\n");
284                 for(i=0;i<nodemap->num;i++){
285                         printf(":%d:%s:%d:%d:%d:%d:\n", nodemap->nodes[i].pnn,
286                                 inet_ntoa(nodemap->nodes[i].sin.sin_addr),
287                                !!(nodemap->nodes[i].flags&NODE_FLAGS_DISCONNECTED),
288                                !!(nodemap->nodes[i].flags&NODE_FLAGS_BANNED),
289                                !!(nodemap->nodes[i].flags&NODE_FLAGS_PERMANENTLY_DISABLED),
290                                !!(nodemap->nodes[i].flags&NODE_FLAGS_UNHEALTHY));
291                 }
292                 return 0;
293         }
294
295         printf("Number of nodes:%d\n", nodemap->num);
296         for(i=0;i<nodemap->num;i++){
297                 static const struct {
298                         uint32_t flag;
299                         const char *name;
300                 } flag_names[] = {
301                         { NODE_FLAGS_DISCONNECTED,          "DISCONNECTED" },
302                         { NODE_FLAGS_PERMANENTLY_DISABLED,  "DISABLED" },
303                         { NODE_FLAGS_BANNED,                "BANNED" },
304                         { NODE_FLAGS_UNHEALTHY,             "UNHEALTHY" },
305                 };
306                 char *flags_str = NULL;
307                 int j;
308                 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
309                         if (nodemap->nodes[i].flags & flag_names[j].flag) {
310                                 if (flags_str == NULL) {
311                                         flags_str = talloc_strdup(ctdb, flag_names[j].name);
312                                 } else {
313                                         flags_str = talloc_asprintf_append(flags_str, "|%s",
314                                                                            flag_names[j].name);
315                                 }
316                                 CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
317                         }
318                 }
319                 if (flags_str == NULL) {
320                         flags_str = talloc_strdup(ctdb, "OK");
321                         CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
322                 }
323                 printf("pnn:%d %-16s %s%s\n", nodemap->nodes[i].pnn,
324                        inet_ntoa(nodemap->nodes[i].sin.sin_addr),
325                        flags_str,
326                        nodemap->nodes[i].pnn == mypnn?" (THIS NODE)":"");
327                 talloc_free(flags_str);
328         }
329
330         ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &vnnmap);
331         if (ret != 0) {
332                 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
333                 return ret;
334         }
335         if (vnnmap->generation == INVALID_GENERATION) {
336                 printf("Generation:INVALID\n");
337         } else {
338                 printf("Generation:%d\n",vnnmap->generation);
339         }
340         printf("Size:%d\n",vnnmap->size);
341         for(i=0;i<vnnmap->size;i++){
342                 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
343         }
344
345         ret = ctdb_ctrl_getrecmode(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmode);
346         if (ret != 0) {
347                 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
348                 return ret;
349         }
350         printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
351
352         ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
353         if (ret != 0) {
354                 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
355                 return ret;
356         }
357         printf("Recovery master:%d\n",recmaster);
358
359         return 0;
360 }
361
362 /*
363   get a list of all tickles for this pnn
364  */
365 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
366 {
367         struct ctdb_control_tcp_tickle_list *list;
368         struct sockaddr_in ip;
369         int i, ret;
370
371         if (argc < 1) {
372                 usage();
373         }
374
375         ip.sin_family = AF_INET;
376         if (inet_aton(argv[0], &ip.sin_addr) == 0) {
377                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
378                 return -1;
379         }
380
381         ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &ip, &list);
382         if (ret == -1) {
383                 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
384                 return -1;
385         }
386
387         printf("Tickles for ip:%s\n", inet_ntoa(list->ip.sin_addr));
388         printf("Num tickles:%u\n", list->tickles.num);
389         for (i=0;i<list->tickles.num;i++) {
390                 printf("SRC: %s:%u   ", inet_ntoa(list->tickles.connections[i].saddr.sin_addr), ntohs(list->tickles.connections[i].saddr.sin_port));
391                 printf("DST: %s:%u\n", inet_ntoa(list->tickles.connections[i].daddr.sin_addr), ntohs(list->tickles.connections[i].daddr.sin_port));
392         }
393
394         talloc_free(list);
395         
396         return 0;
397 }
398
399 /*
400   kill a tcp connection
401  */
402 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
403 {
404         int ret;
405         struct ctdb_control_killtcp killtcp;
406
407         if (argc < 2) {
408                 usage();
409         }
410
411         if (!parse_ip_port(argv[0], &killtcp.src)) {
412                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
413                 return -1;
414         }
415
416         if (!parse_ip_port(argv[1], &killtcp.dst)) {
417                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
418                 return -1;
419         }
420
421         ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
422         if (ret != 0) {
423                 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
424                 return ret;
425         }
426
427         return 0;
428 }
429
430
431 /*
432   send a gratious arp
433  */
434 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
435 {
436         int ret;
437         struct sockaddr_in sin;
438
439         if (argc < 2) {
440                 usage();
441         }
442
443         sin.sin_family = AF_INET;
444         if (inet_aton(argv[0], &sin.sin_addr) == 0) {
445                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
446                 return -1;
447         }
448
449         ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &sin, argv[1]);
450         if (ret != 0) {
451                 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
452                 return ret;
453         }
454
455         return 0;
456 }
457
458 /*
459   register a server id
460  */
461 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
462 {
463         int ret;
464         struct ctdb_server_id server_id;
465
466         if (argc < 3) {
467                 usage();
468         }
469
470         server_id.pnn       = strtoul(argv[0], NULL, 0);
471         server_id.type      = strtoul(argv[1], NULL, 0);
472         server_id.server_id = strtoul(argv[2], NULL, 0);
473
474         ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
475         if (ret != 0) {
476                 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
477                 return ret;
478         }
479         return -1;
480 }
481
482 /*
483   unregister a server id
484  */
485 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
486 {
487         int ret;
488         struct ctdb_server_id server_id;
489
490         if (argc < 3) {
491                 usage();
492         }
493
494         server_id.pnn       = strtoul(argv[0], NULL, 0);
495         server_id.type      = strtoul(argv[1], NULL, 0);
496         server_id.server_id = strtoul(argv[2], NULL, 0);
497
498         ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
499         if (ret != 0) {
500                 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
501                 return ret;
502         }
503         return -1;
504 }
505
506 /*
507   check if a server id exists
508  */
509 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
510 {
511         uint32_t status;
512         int ret;
513         struct ctdb_server_id server_id;
514
515         if (argc < 3) {
516                 usage();
517         }
518
519         server_id.pnn       = strtoul(argv[0], NULL, 0);
520         server_id.type      = strtoul(argv[1], NULL, 0);
521         server_id.server_id = strtoul(argv[2], NULL, 0);
522
523         ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
524         if (ret != 0) {
525                 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
526                 return ret;
527         }
528
529         if (status) {
530                 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
531         } else {
532                 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
533         }
534         return 0;
535 }
536
537 /*
538   get a list of all server ids that are registered on a node
539  */
540 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
541 {
542         int i, ret;
543         struct ctdb_server_id_list *server_ids;
544
545         ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
546         if (ret != 0) {
547                 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
548                 return ret;
549         }
550
551         for (i=0; i<server_ids->num; i++) {
552                 printf("Server id %d:%d:%d\n", 
553                         server_ids->server_ids[i].pnn, 
554                         server_ids->server_ids[i].type, 
555                         server_ids->server_ids[i].server_id); 
556         }
557
558         return -1;
559 }
560
561 /*
562   send a tcp tickle ack
563  */
564 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
565 {
566         int s, ret;
567         struct sockaddr_in src, dst;
568
569         if (argc < 2) {
570                 usage();
571         }
572
573         if (!parse_ip_port(argv[0], &src)) {
574                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
575                 return -1;
576         }
577
578         if (!parse_ip_port(argv[1], &dst)) {
579                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
580                 return -1;
581         }
582
583         s = ctdb_sys_open_sending_socket();
584         if (s == -1) {
585                 DEBUG(DEBUG_ERR, ("Failed to open socket for sending tickle\n"));
586                 return 0;
587         }
588
589         ret = ctdb_sys_send_tcp(s, &src, &dst, 0, 0, 0);
590         close(s);
591         if (ret==0) {
592                 return 0;
593         }
594         DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
595
596         return -1;
597 }
598
599
600 /*
601   display public ip status
602  */
603 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
604 {
605         int i, ret;
606         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
607         struct ctdb_all_public_ips *ips;
608
609         /* read the public ip list from this node */
610         ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
611         if (ret != 0) {
612                 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
613                 talloc_free(tmp_ctx);
614                 return ret;
615         }
616
617         if (options.machinereadable){
618                 printf(":Public IP:Node:\n");
619         } else {
620                 printf("Public IPs on node %u\n", options.pnn);
621         }
622
623         for (i=1;i<=ips->num;i++) {
624                 printf("%s %d\n", inet_ntoa(ips->ips[ips->num-i].sin.sin_addr), ips->ips[ips->num-i].pnn);
625         }
626
627         talloc_free(tmp_ctx);
628         return 0;
629 }
630
631 /*
632   display pid of a ctdb daemon
633  */
634 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
635 {
636         uint32_t pid;
637         int ret;
638
639         ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
640         if (ret != 0) {
641                 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
642                 return ret;
643         }
644         printf("Pid:%d\n", pid);
645
646         return 0;
647 }
648
649 /*
650   disable a remote node
651  */
652 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
653 {
654         int ret;
655
656         ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, NODE_FLAGS_PERMANENTLY_DISABLED, 0);
657         if (ret != 0) {
658                 DEBUG(DEBUG_ERR, ("Unable to disable node %u\n", options.pnn));
659                 return ret;
660         }
661
662         return 0;
663 }
664
665 /*
666   enable a disabled remote node
667  */
668 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
669 {
670         int ret;
671
672         ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, 0, NODE_FLAGS_PERMANENTLY_DISABLED);
673         if (ret != 0) {
674                 DEBUG(DEBUG_ERR, ("Unable to enable node %u\n", options.pnn));
675                 return ret;
676         }
677
678         return 0;
679 }
680
681 /*
682   ban a node from the cluster
683  */
684 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
685 {
686         int ret;
687         struct ctdb_ban_info b;
688         TDB_DATA data;
689         uint32_t ban_time;
690
691         if (argc < 1) {
692                 usage();
693         }
694
695         ban_time = strtoul(argv[0], NULL, 0);
696
697         b.pnn = options.pnn;
698         b.ban_time = ban_time;
699
700         data.dptr = (uint8_t *)&b;
701         data.dsize = sizeof(b);
702
703         ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_BAN_NODE, data);
704         if (ret != 0) {
705                 DEBUG(DEBUG_ERR,("Failed to ban node %u\n", options.pnn));
706                 return -1;
707         }
708         
709         return 0;
710 }
711
712
713 /*
714   unban a node from the cluster
715  */
716 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
717 {
718         int ret;
719         TDB_DATA data;
720
721         data.dptr = (uint8_t *)&options.pnn;
722         data.dsize = sizeof(uint32_t);
723
724         ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_UNBAN_NODE, data);
725         if (ret != 0) {
726                 DEBUG(DEBUG_ERR,("Failed to to unban node %u\n", options.pnn));
727                 return -1;
728         }
729         
730         return 0;
731 }
732
733
734 /*
735   shutdown a daemon
736  */
737 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
738 {
739         int ret;
740
741         ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
742         if (ret != 0) {
743                 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
744                 return ret;
745         }
746
747         return 0;
748 }
749
750 /*
751   trigger a recovery
752  */
753 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
754 {
755         int ret;
756
757         ret = ctdb_ctrl_freeze(ctdb, TIMELIMIT(), options.pnn);
758         if (ret != 0) {
759                 DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
760                 return ret;
761         }
762
763         ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
764         if (ret != 0) {
765                 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
766                 return ret;
767         }
768
769         return 0;
770 }
771
772
773 /*
774   display monitoring mode of a remote node
775  */
776 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
777 {
778         uint32_t monmode;
779         int ret;
780
781         ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
782         if (ret != 0) {
783                 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
784                 return ret;
785         }
786         printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
787
788         return 0;
789 }
790
791 /*
792   display remote list of keys/data for a db
793  */
794 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
795 {
796         const char *db_name;
797         struct ctdb_db_context *ctdb_db;
798         int ret;
799
800         if (argc < 1) {
801                 usage();
802         }
803
804         db_name = argv[0];
805         ctdb_db = ctdb_attach(ctdb, db_name, false);
806
807         if (ctdb_db == NULL) {
808                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
809                 return -1;
810         }
811
812         /* traverse and dump the cluster tdb */
813         ret = ctdb_dump_db(ctdb_db, stdout);
814         if (ret == -1) {
815                 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
816                 return -1;
817         }
818         talloc_free(ctdb_db);
819
820         printf("Dumped %d records\n", ret);
821         return 0;
822 }
823
824
825 /*
826   display a list of the databases on a remote ctdb
827  */
828 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
829 {
830         int i, ret;
831         struct ctdb_dbid_map *dbmap=NULL;
832
833         ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
834         if (ret != 0) {
835                 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
836                 return ret;
837         }
838
839         printf("Number of databases:%d\n", dbmap->num);
840         for(i=0;i<dbmap->num;i++){
841                 const char *path;
842                 const char *name;
843                 bool persistent;
844
845                 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
846                 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
847                 persistent = dbmap->dbs[i].persistent;
848                 printf("dbid:0x%08x name:%s path:%s %s\n", dbmap->dbs[i].dbid, name, 
849                        path, persistent?"PERSISTENT":"");
850         }
851
852         return 0;
853 }
854
855 /*
856   check if the local node is recmaster or not
857   it will return 1 if this node is the recmaster and 0 if it is not
858   or if the local ctdb daemon could not be contacted
859  */
860 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
861 {
862         uint32_t mypnn, recmaster;
863         int ret;
864
865         mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
866         if (mypnn == -1) {
867                 printf("Failed to get pnn of node\n");
868                 return 1;
869         }
870
871         ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
872         if (ret != 0) {
873                 printf("Failed to get the recmaster\n");
874                 return 1;
875         }
876
877         if (recmaster != mypnn) {
878                 printf("this node is not the recmaster\n");
879                 return 1;
880         }
881
882         printf("this node is the recmaster\n");
883         return 0;
884 }
885
886 /*
887   ping a node
888  */
889 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
890 {
891         int ret;
892         struct timeval tv = timeval_current();
893         ret = ctdb_ctrl_ping(ctdb, options.pnn);
894         if (ret == -1) {
895                 printf("Unable to get ping response from node %u\n", options.pnn);
896         } else {
897                 printf("response from %u time=%.6f sec  (%d clients)\n", 
898                        options.pnn, timeval_elapsed(&tv), ret);
899         }
900         return 0;
901 }
902
903
904 /*
905   get a tunable
906  */
907 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
908 {
909         const char *name;
910         uint32_t value;
911         int ret;
912
913         if (argc < 1) {
914                 usage();
915         }
916
917         name = argv[0];
918         ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
919         if (ret == -1) {
920                 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
921                 return -1;
922         }
923
924         printf("%-19s = %u\n", name, value);
925         return 0;
926 }
927
928 /*
929   set a tunable
930  */
931 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
932 {
933         const char *name;
934         uint32_t value;
935         int ret;
936
937         if (argc < 2) {
938                 usage();
939         }
940
941         name = argv[0];
942         value = strtoul(argv[1], NULL, 0);
943
944         ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
945         if (ret == -1) {
946                 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
947                 return -1;
948         }
949         return 0;
950 }
951
952 /*
953   list all tunables
954  */
955 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
956 {
957         uint32_t count;
958         const char **list;
959         int ret, i;
960
961         ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
962         if (ret == -1) {
963                 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
964                 return -1;
965         }
966
967         for (i=0;i<count;i++) {
968                 control_getvar(ctdb, 1, &list[i]);
969         }
970
971         talloc_free(list);
972         
973         return 0;
974 }
975
976 /*
977   display debug level on a node
978  */
979 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
980 {
981         int ret;
982         uint32_t level;
983
984         ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
985         if (ret != 0) {
986           DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", 
987                        options.pnn));
988         } else {
989                 printf("Node %u is at debug level %u\n", options.pnn, level);
990         }
991         return 0;
992 }
993
994
995 /*
996   set debug level on a node or all nodes
997  */
998 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
999 {
1000         int ret;
1001         uint32_t level;
1002
1003         if (argc < 1) {
1004                 usage();
1005         }
1006
1007         level = strtoul(argv[0], NULL, 0);
1008
1009         ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
1010         if (ret != 0) {
1011                 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
1012         }
1013         return 0;
1014 }
1015
1016
1017 /*
1018   freeze a node
1019  */
1020 static int control_freeze(struct ctdb_context *ctdb, int argc, const char **argv)
1021 {
1022         int ret;
1023
1024         ret = ctdb_ctrl_freeze(ctdb, TIMELIMIT(), options.pnn);
1025         if (ret != 0) {
1026                 DEBUG(DEBUG_ERR, ("Unable to freeze node %u\n", options.pnn));
1027         }               
1028         return 0;
1029 }
1030
1031 /*
1032   thaw a node
1033  */
1034 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
1035 {
1036         int ret;
1037
1038         ret = ctdb_ctrl_thaw(ctdb, TIMELIMIT(), options.pnn);
1039         if (ret != 0) {
1040                 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
1041         }               
1042         return 0;
1043 }
1044
1045
1046 /*
1047   attach to a database
1048  */
1049 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
1050 {
1051         const char *db_name;
1052         struct ctdb_db_context *ctdb_db;
1053
1054         if (argc < 1) {
1055                 usage();
1056         }
1057         db_name = argv[0];
1058
1059         ctdb_db = ctdb_attach(ctdb, db_name, false);
1060         if (ctdb_db == NULL) {
1061                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
1062                 return -1;
1063         }
1064
1065         return 0;
1066 }
1067
1068 /*
1069   dump memory usage
1070  */
1071 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
1072 {
1073         TDB_DATA data;
1074         int ret;
1075         int32_t res;
1076         char *errmsg;
1077         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1078         ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
1079                            0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
1080         if (ret != 0 || res != 0) {
1081                 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
1082                 talloc_free(tmp_ctx);
1083                 return -1;
1084         }
1085         write(1, data.dptr, data.dsize);
1086         talloc_free(tmp_ctx);
1087         return 0;
1088 }
1089
1090 static const struct {
1091         const char *name;
1092         int (*fn)(struct ctdb_context *, int, const char **);
1093         bool auto_all;
1094         const char *msg;
1095         const char *args;
1096 } ctdb_commands[] = {
1097         { "status",          control_status,            true,  "show node status" },
1098         { "uptime",          control_uptime,            true,  "show node uptime" },
1099         { "ping",            control_ping,              true,  "ping all nodes" },
1100         { "getvar",          control_getvar,            true,  "get a tunable variable",               "<name>"},
1101         { "setvar",          control_setvar,            true,  "set a tunable variable",               "<name> <value>"},
1102         { "listvars",        control_listvars,          true,  "list tunable variables"},
1103         { "statistics",      control_statistics,        false, "show statistics" },
1104         { "statisticsreset", control_statistics_reset,  true,  "reset statistics"},
1105         { "ip",              control_ip,                true,  "show which public ip's that ctdb manages" },
1106         { "process-exists",  control_process_exists,    true,  "check if a process exists on a node",  "<pid>"},
1107         { "getdbmap",        control_getdbmap,          true,  "show the database map" },
1108         { "catdb",           control_catdb,             true,  "dump a database" ,                     "<dbname>"},
1109         { "getmonmode",      control_getmonmode,        true,  "show monitoring mode" },
1110         { "setdebug",        control_setdebug,          true,  "set debug level",                      "<debuglevel>" },
1111         { "getdebug",        control_getdebug,          true,  "get debug level" },
1112         { "attach",          control_attach,            true,  "attach to a database",                 "<dbname>" },
1113         { "dumpmemory",      control_dumpmemory,        true,  "dump memory map to stdout" },
1114         { "getpid",          control_getpid,            true,  "get ctdbd process ID" },
1115         { "disable",         control_disable,           true,  "disable a nodes public IP" },
1116         { "enable",          control_enable,            true,  "enable a nodes public IP" },
1117         { "ban",             control_ban,               true,  "ban a node from the cluster",          "<bantime|0>"},
1118         { "unban",           control_unban,             true,  "unban a node from the cluster" },
1119         { "shutdown",        control_shutdown,          true,  "shutdown ctdbd" },
1120         { "recover",         control_recover,           true,  "force recovery" },
1121         { "freeze",          control_freeze,            true,  "freeze all databases" },
1122         { "thaw",            control_thaw,              true,  "thaw all databases" },
1123         { "isnotrecmaster",  control_isnotrecmaster,    false,  "check if the local node is recmaster or not" },
1124         { "killtcp",         kill_tcp,                  false, "kill a tcp connection.", "<srcip:port> <dstip:port>" },
1125         { "gratiousarp",     control_gratious_arp,      false, "send a gratious arp", "<ip> <interface>" },
1126         { "tickle",          tickle_tcp,                false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
1127         { "gettickles",      control_get_tickles,       false, "get the list of tickles registered for this ip", "<ip>" },
1128
1129         { "regsrvid",        regsrvid,                  false, "register a server id", "<pnn> <type> <id>" },
1130         { "unregsrvid",      unregsrvid,                false, "unregister a server id", "<pnn> <type> <id>" },
1131         { "chksrvid",        chksrvid,                  false, "check if a server id exists", "<pnn> <type> <id>" },
1132         { "getsrvids",       getsrvids,                 false, "get a list of all server ids"},
1133         { "vacuum",          ctdb_vacuum,               false, "vacuum the databases of empty records", "[max_records]"},
1134         { "repack",          ctdb_repack,               false, "repack all databases", "[max_freelist]"},
1135 };
1136
1137 /*
1138   show usage message
1139  */
1140 static void usage(void)
1141 {
1142         int i;
1143         printf(
1144 "Usage: ctdb [options] <control>\n" \
1145 "Options:\n" \
1146 "   -n <node>          choose node number, or 'all' (defaults to local node)\n"
1147 "   -Y                 generate machinereadable output\n"
1148 "   -t <timelimit>     set timelimit for control in seconds (default %u)\n", options.timelimit);
1149         printf("Controls:\n");
1150         for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
1151                 printf("  %-15s %-27s  %s\n", 
1152                        ctdb_commands[i].name, 
1153                        ctdb_commands[i].args?ctdb_commands[i].args:"",
1154                        ctdb_commands[i].msg);
1155         }
1156         exit(1);
1157 }
1158
1159
1160 static void ctdb_alarm(int sig)
1161 {
1162         printf("Maximum runtime exceeded - exiting\n");
1163         _exit(0);
1164 }
1165
1166 /*
1167   main program
1168 */
1169 int main(int argc, const char *argv[])
1170 {
1171         struct ctdb_context *ctdb;
1172         char *nodestring = NULL;
1173         struct poptOption popt_options[] = {
1174                 POPT_AUTOHELP
1175                 POPT_CTDB_CMDLINE
1176                 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
1177                 { "node",      'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
1178                 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
1179                 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
1180                 POPT_TABLEEND
1181         };
1182         int opt;
1183         const char **extra_argv;
1184         int extra_argc = 0;
1185         int ret=-1, i;
1186         poptContext pc;
1187         struct event_context *ev;
1188         const char *control;
1189
1190         setlinebuf(stdout);
1191         
1192         /* set some defaults */
1193         options.maxruntime = 0;
1194         options.timelimit = 3;
1195         options.pnn = CTDB_CURRENT_NODE;
1196
1197         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
1198
1199         while ((opt = poptGetNextOpt(pc)) != -1) {
1200                 switch (opt) {
1201                 default:
1202                         DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n", 
1203                                 poptBadOption(pc, 0), poptStrerror(opt)));
1204                         exit(1);
1205                 }
1206         }
1207
1208         /* setup the remaining options for the main program to use */
1209         extra_argv = poptGetArgs(pc);
1210         if (extra_argv) {
1211                 extra_argv++;
1212                 while (extra_argv[extra_argc]) extra_argc++;
1213         }
1214
1215         if (extra_argc < 1) {
1216                 usage();
1217         }
1218
1219         if (options.maxruntime != 0) {
1220                 signal(SIGALRM, ctdb_alarm);
1221                 alarm(options.maxruntime);
1222         }
1223
1224         /* setup the node number to contact */
1225         if (nodestring != NULL) {
1226                 if (strcmp(nodestring, "all") == 0) {
1227                         options.pnn = CTDB_BROADCAST_ALL;
1228                 } else {
1229                         options.pnn = strtoul(nodestring, NULL, 0);
1230                 }
1231         }
1232
1233         control = extra_argv[0];
1234
1235         ev = event_context_init(NULL);
1236
1237         /* initialise ctdb */
1238         ctdb = ctdb_cmdline_client(ev);
1239         if (ctdb == NULL) {
1240                 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
1241                 exit(1);
1242         }
1243
1244         for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
1245                 if (strcmp(control, ctdb_commands[i].name) == 0) {
1246                         int j;
1247
1248                         if (options.pnn == CTDB_CURRENT_NODE) {
1249                                 int pnn;
1250                                 pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);         
1251                                 if (pnn == -1) {
1252                                         return -1;
1253                                 }
1254                                 options.pnn = pnn;
1255                         }
1256
1257                         if (ctdb_commands[i].auto_all && 
1258                             options.pnn == CTDB_BROADCAST_ALL) {
1259                                 uint32_t *nodes;
1260                                 uint32_t num_nodes;
1261                                 ret = 0;
1262
1263                                 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
1264                                 CTDB_NO_MEMORY(ctdb, nodes);
1265         
1266                                 for (j=0;j<num_nodes;j++) {
1267                                         options.pnn = nodes[j];
1268                                         ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
1269                                 }
1270                                 talloc_free(nodes);
1271                         } else {
1272                                 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
1273                         }
1274                         break;
1275                 }
1276         }
1277
1278         if (i == ARRAY_SIZE(ctdb_commands)) {
1279                 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
1280                 exit(1);
1281         }
1282
1283         return ret;
1284 }