- added monitoring of rpc ports for nfs, and of Samba ports and directories
[metze/ctdb/wip.git] / common / ctdb_tunables.c
1 /* 
2    ctdb tunables code
3
4    Copyright (C) Andrew Tridgell  2007
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20 #include "includes.h"
21 #include "../include/ctdb_private.h"
22
23 static const struct {
24         const char *name;
25         uint32_t default_v;
26         size_t offset;  
27 } tunable_map[] = {
28         { "MaxRedirectCount",  3,  offsetof(struct ctdb_tunable, max_redirect_count) },
29         { "SeqnumFrequency",   1,  offsetof(struct ctdb_tunable, seqnum_frequency) },
30         { "ControlTimeout",    60, offsetof(struct ctdb_tunable, control_timeout) },
31         { "TraverseTimeout",   20, offsetof(struct ctdb_tunable, traverse_timeout) },
32         { "KeepaliveInterval", 2,  offsetof(struct ctdb_tunable, keepalive_interval) },
33         { "KeepaliveLimit",    3,  offsetof(struct ctdb_tunable, keepalive_limit) },
34         { "MaxLACount",        7,  offsetof(struct ctdb_tunable, max_lacount) },
35         { "RecoverTimeout",    5,  offsetof(struct ctdb_tunable, recover_timeout) },
36         { "RecoverInterval",   1,  offsetof(struct ctdb_tunable, recover_interval) },
37         { "ElectionTimeout",   3,  offsetof(struct ctdb_tunable, election_timeout) },
38         { "TakeoverTimeout",   5,  offsetof(struct ctdb_tunable, takeover_timeout) },
39         { "MonitorInterval",  15,  offsetof(struct ctdb_tunable, monitor_interval) },
40 };
41
42 /*
43   set all tunables to defaults
44  */
45 void ctdb_tunables_set_defaults(struct ctdb_context *ctdb)
46 {
47         int i;
48         for (i=0;i<ARRAY_SIZE(tunable_map);i++) {
49                 *(uint32_t *)(tunable_map[i].offset + (uint8_t*)&ctdb->tunable) = tunable_map[i].default_v;
50         }
51 }
52
53
54 /*
55   get a tunable
56  */
57 int32_t ctdb_control_get_tunable(struct ctdb_context *ctdb, TDB_DATA indata, 
58                                  TDB_DATA *outdata)
59 {
60         struct ctdb_control_get_tunable *t = 
61                 (struct ctdb_control_get_tunable *)indata.dptr;
62         char *name;
63         uint32_t val;
64         int i;
65
66         if (indata.dsize < sizeof(*t) ||
67             t->length > indata.dsize - offsetof(struct ctdb_control_get_tunable, name)) {
68                 DEBUG(0,("Bad indata in ctdb_control_get_tunable\n"));
69                 return -1;
70         }
71
72         name = talloc_strndup(ctdb, (char*)t->name, t->length);
73         CTDB_NO_MEMORY(ctdb, name);
74
75         for (i=0;i<ARRAY_SIZE(tunable_map);i++) {
76                 if (strcasecmp(name, tunable_map[i].name) == 0) break;
77         }
78         talloc_free(name);
79         
80         if (i == ARRAY_SIZE(tunable_map)) {
81                 return -1;
82         }
83
84         val = *(uint32_t *)(tunable_map[i].offset + (uint8_t*)&ctdb->tunable);
85
86         outdata->dptr = (uint8_t *)talloc(outdata, uint32_t);
87         CTDB_NO_MEMORY(ctdb, outdata->dptr);
88
89         *(uint32_t *)outdata->dptr = val;
90         outdata->dsize = sizeof(uint32_t);
91
92         return 0;
93 }
94
95
96 /*
97   set a tunable
98  */
99 int32_t ctdb_control_set_tunable(struct ctdb_context *ctdb, TDB_DATA indata)
100 {
101         struct ctdb_control_set_tunable *t = 
102                 (struct ctdb_control_set_tunable *)indata.dptr;
103         char *name;
104         int i;
105
106         if (indata.dsize < sizeof(*t) ||
107             t->length > indata.dsize - offsetof(struct ctdb_control_set_tunable, name)) {
108                 DEBUG(0,("Bad indata in ctdb_control_set_tunable\n"));
109                 return -1;
110         }
111
112         name = talloc_strndup(ctdb, (char *)t->name, t->length);
113         CTDB_NO_MEMORY(ctdb, name);
114
115         for (i=0;i<ARRAY_SIZE(tunable_map);i++) {
116                 if (strcasecmp(name, tunable_map[i].name) == 0) break;
117         }
118
119         talloc_free(name);
120         
121         if (i == ARRAY_SIZE(tunable_map)) {
122                 return -1;
123         }
124         
125         *(uint32_t *)(tunable_map[i].offset + (uint8_t*)&ctdb->tunable) = t->value;
126
127         return 0;
128 }
129
130 /*
131   list tunables
132  */
133 int32_t ctdb_control_list_tunables(struct ctdb_context *ctdb, TDB_DATA *outdata)
134 {
135         char *list = NULL;
136         int i;
137         struct ctdb_control_list_tunable *t;
138
139         list = talloc_strdup(outdata, tunable_map[0].name);
140         CTDB_NO_MEMORY(ctdb, list);
141
142         for (i=1;i<ARRAY_SIZE(tunable_map);i++) {
143                 list = talloc_asprintf_append(list, ":%s", tunable_map[i].name);
144                 CTDB_NO_MEMORY(ctdb, list);             
145         }
146
147         outdata->dsize = offsetof(struct ctdb_control_list_tunable, data) + 
148                 strlen(list) + 1;
149         outdata->dptr = talloc_size(outdata, outdata->dsize);
150         CTDB_NO_MEMORY(ctdb, outdata->dptr);
151
152         t = (struct ctdb_control_list_tunable *)outdata->dptr;
153         t->length = strlen(list)+1;
154
155         memcpy(t->data, list, t->length);
156         talloc_free(list);
157
158         return 0;       
159 }