eventscript: introduce enum for different event script calls.
[metze/ctdb/wip.git] / server / ctdb_monitor.c
1 /* 
2    monitoring links to all other nodes to detect dead nodes
3
4
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/filesys.h"
24 #include "system/wait.h"
25 #include "../include/ctdb_private.h"
26
27 struct ctdb_monitor_state {
28         uint32_t monitoring_mode;
29         TALLOC_CTX *monitor_context;
30         uint32_t next_interval;
31 };
32
33 static void ctdb_check_health(struct event_context *ev, struct timed_event *te, 
34                               struct timeval t, void *private_data);
35
36 /*
37   setup the notification script
38 */
39 int ctdb_set_notification_script(struct ctdb_context *ctdb, const char *script)
40 {
41         ctdb->notification_script = talloc_strdup(ctdb, script);
42         CTDB_NO_MEMORY(ctdb, ctdb->notification_script);
43         return 0;
44 }
45
46 static int ctdb_run_notification_script_child(struct ctdb_context *ctdb, const char *event)
47 {
48         struct stat st;
49         int ret;
50         char *cmd;
51
52         if (stat(ctdb->notification_script, &st) != 0) {
53                 DEBUG(DEBUG_ERR,("Could not stat notification script %s. Can not send notifications.\n", ctdb->notification_script));
54                 return -1;
55         }
56         if (!(st.st_mode & S_IXUSR)) {
57                 DEBUG(DEBUG_ERR,("Notification script %s is not executable.\n", ctdb->notification_script));
58                 return -1;
59         }
60
61         cmd = talloc_asprintf(ctdb, "%s %s\n", ctdb->notification_script, event);
62         CTDB_NO_MEMORY(ctdb, cmd);
63
64         ret = system(cmd);
65         /* if the system() call was successful, translate ret into the
66            return code from the command
67         */
68         if (ret != -1) {
69                 ret = WEXITSTATUS(ret);
70         }
71         if (ret != 0) {
72                 DEBUG(DEBUG_ERR,("Notification script \"%s\" failed with error %d\n", cmd, ret));
73         }
74
75         return ret;
76 }
77
78 static void ctdb_run_notification_script(struct ctdb_context *ctdb, const char *event)
79 {
80         pid_t child;
81
82         if (ctdb->notification_script == NULL) {
83                 return;
84         }
85
86         child = fork();
87         if (child == (pid_t)-1) {
88                 DEBUG(DEBUG_ERR,("Failed to fork() a notification child process\n"));
89                 return;
90         }
91         if (child == 0) {
92                 int ret;
93
94                 ret = ctdb_run_notification_script_child(ctdb, event);
95                 if (ret != 0) {
96                         DEBUG(DEBUG_ERR,(__location__ " Notification script failed\n"));
97                 }
98                 _exit(0);
99         }
100
101         return;
102 }
103
104 /*
105   called when a health monitoring event script finishes
106  */
107 static void ctdb_health_callback(struct ctdb_context *ctdb, int status, void *p)
108 {
109         struct ctdb_node *node = ctdb->nodes[ctdb->pnn];
110         TDB_DATA data;
111         struct ctdb_node_flag_change c;
112         uint32_t next_interval;
113         int ret;
114         TDB_DATA rddata;
115         struct takeover_run_reply rd;
116
117         c.pnn = ctdb->pnn;
118         c.old_flags = node->flags;
119
120         rd.pnn   = ctdb->pnn;
121         rd.srvid = CTDB_SRVID_TAKEOVER_RUN_RESPONSE;
122
123         rddata.dptr = (uint8_t *)&rd;
124         rddata.dsize = sizeof(rd);
125
126         if (status != 0 && !(node->flags & NODE_FLAGS_UNHEALTHY)) {
127                 DEBUG(DEBUG_NOTICE,("monitor event failed - disabling node\n"));
128                 node->flags |= NODE_FLAGS_UNHEALTHY;
129                 ctdb->monitor->next_interval = 5;
130                 if (ctdb->tunable.disable_when_unhealthy != 0) {
131                         DEBUG(DEBUG_INFO, ("DISABLING node since it became unhealthy\n"));
132                         node->flags |= NODE_FLAGS_DISABLED;
133                 }
134
135                 ctdb_run_notification_script(ctdb, "unhealthy");
136
137                 /* ask the recmaster to reallocate all addresses */
138                 DEBUG(DEBUG_ERR,("Node became UNHEALTHY. Ask recovery master %u to perform ip reallocation\n", ctdb->recovery_master));
139                 ret = ctdb_daemon_send_message(ctdb, ctdb->recovery_master, CTDB_SRVID_TAKEOVER_RUN, rddata);
140                 if (ret != 0) {
141                         DEBUG(DEBUG_ERR,(__location__ " Failed to send ip takeover run request message to %u\n", ctdb->recovery_master));
142                 }
143
144         } else if (status == 0 && (node->flags & NODE_FLAGS_UNHEALTHY)) {
145                 DEBUG(DEBUG_NOTICE,("monitor event OK - node re-enabled\n"));
146                 node->flags &= ~NODE_FLAGS_UNHEALTHY;
147                 ctdb->monitor->next_interval = 5;
148
149                 ctdb_run_notification_script(ctdb, "healthy");
150
151                 /* ask the recmaster to reallocate all addresses */
152                 DEBUG(DEBUG_ERR,("Node became HEALTHY. Ask recovery master %u to perform ip reallocation\n", ctdb->recovery_master));
153                 ret = ctdb_daemon_send_message(ctdb, ctdb->recovery_master, CTDB_SRVID_TAKEOVER_RUN, rddata);
154                 if (ret != 0) {
155                         DEBUG(DEBUG_ERR,(__location__ " Failed to send ip takeover run request message to %u\n", ctdb->recovery_master));
156                 }
157
158         }
159
160         next_interval = ctdb->monitor->next_interval;
161
162         ctdb->monitor->next_interval *= 2;
163         if (ctdb->monitor->next_interval > ctdb->tunable.monitor_interval) {
164                 ctdb->monitor->next_interval = ctdb->tunable.monitor_interval;
165         }
166
167         event_add_timed(ctdb->ev, ctdb->monitor->monitor_context, 
168                                 timeval_current_ofs(next_interval, 0), 
169                                 ctdb_check_health, ctdb);
170
171         if (c.old_flags == node->flags) {
172                 return;
173         }
174
175         c.new_flags = node->flags;
176
177         data.dptr = (uint8_t *)&c;
178         data.dsize = sizeof(c);
179
180         /* ask the recovery daemon to push these changes out to all nodes */
181         ctdb_daemon_send_message(ctdb, ctdb->pnn,
182                                  CTDB_SRVID_PUSH_NODE_FLAGS, data);
183
184 }
185
186
187 /*
188   called when the startup event script finishes
189  */
190 static void ctdb_startup_callback(struct ctdb_context *ctdb, int status, void *p)
191 {
192         if (status != 0) {
193                 DEBUG(DEBUG_ERR,("startup event failed\n"));
194         } else if (status == 0) {
195                 DEBUG(DEBUG_NOTICE,("startup event OK - enabling monitoring\n"));
196                 ctdb->done_startup = true;
197                 ctdb->monitor->next_interval = 5;
198                 ctdb_run_notification_script(ctdb, "startup");
199         }
200
201         event_add_timed(ctdb->ev, ctdb->monitor->monitor_context, 
202                         timeval_current_ofs(ctdb->monitor->next_interval, 0),
203                         ctdb_check_health, ctdb);
204 }
205
206
207 /*
208   see if the event scripts think we are healthy
209  */
210 static void ctdb_check_health(struct event_context *ev, struct timed_event *te, 
211                               struct timeval t, void *private_data)
212 {
213         struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
214         int ret = 0;
215
216         if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL ||
217             (ctdb->monitor->monitoring_mode == CTDB_MONITORING_DISABLED && ctdb->done_startup)) {
218                 event_add_timed(ctdb->ev, ctdb->monitor->monitor_context,
219                                 timeval_current_ofs(ctdb->monitor->next_interval, 0), 
220                                 ctdb_check_health, ctdb);
221                 return;
222         }
223         
224         if (!ctdb->done_startup) {
225                 ret = ctdb_event_script_callback(ctdb, 
226                                                  ctdb->monitor->monitor_context, ctdb_startup_callback, 
227                                                  ctdb, CTDB_EVENT_STARTUP, "%s", "");
228         } else {
229                 int i;
230                 int skip_monitoring = 0;
231                 
232                 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
233                         skip_monitoring = 1;
234                         DEBUG(DEBUG_ERR,("Skip monitoring during recovery\n"));
235                 }
236                 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
237                         if (ctdb->freeze_handles[i] != NULL) {
238                                 DEBUG(DEBUG_ERR,("Skip monitoring since databases are frozen\n"));
239                                 skip_monitoring = 1;
240                                 break;
241                         }
242                 }
243                 if (skip_monitoring != 0) {
244                         event_add_timed(ctdb->ev, ctdb->monitor->monitor_context,
245                                         timeval_current_ofs(ctdb->monitor->next_interval, 0), 
246                                         ctdb_check_health, ctdb);
247                         return;
248                 } else {
249                         ret = ctdb_event_script_callback(ctdb, 
250                                         ctdb->monitor->monitor_context, ctdb_health_callback,
251                                         ctdb, CTDB_EVENT_MONITOR, "%s", "");
252                 }
253         }
254
255         if (ret != 0) {
256                 DEBUG(DEBUG_ERR,("Unable to launch monitor event script\n"));
257                 ctdb->monitor->next_interval = 5;
258                 event_add_timed(ctdb->ev, ctdb->monitor->monitor_context, 
259                         timeval_current_ofs(5, 0), 
260                         ctdb_check_health, ctdb);
261         }
262 }
263
264 /* 
265   (Temporaily) Disabling monitoring will stop the monitor event scripts
266   from running   but node health checks will still occur
267 */
268 void ctdb_disable_monitoring(struct ctdb_context *ctdb)
269 {
270         ctdb->monitor->monitoring_mode = CTDB_MONITORING_DISABLED;
271         DEBUG(DEBUG_INFO,("Monitoring has been disabled\n"));
272 }
273
274 /* 
275    Re-enable running monitor events after they have been disabled
276  */
277 void ctdb_enable_monitoring(struct ctdb_context *ctdb)
278 {
279         ctdb->monitor->monitoring_mode  = CTDB_MONITORING_ACTIVE;
280         ctdb->monitor->next_interval = 5;
281         DEBUG(DEBUG_INFO,("Monitoring has been enabled\n"));
282 }
283
284 /* stop any monitoring 
285    this should only be done when shutting down the daemon
286 */
287 void ctdb_stop_monitoring(struct ctdb_context *ctdb)
288 {
289         talloc_free(ctdb->monitor->monitor_context);
290         ctdb->monitor->monitor_context = NULL;
291
292         ctdb->monitor->monitoring_mode  = CTDB_MONITORING_DISABLED;
293         ctdb->monitor->next_interval = 5;
294         DEBUG(DEBUG_NOTICE,("Monitoring has been stopped\n"));
295 }
296
297 /*
298   start watching for nodes that might be dead
299  */
300 void ctdb_start_monitoring(struct ctdb_context *ctdb)
301 {
302         struct timed_event *te;
303
304         if (ctdb->monitor != NULL) {
305                 return;
306         }
307
308         ctdb->monitor = talloc(ctdb, struct ctdb_monitor_state);
309         CTDB_NO_MEMORY_FATAL(ctdb, ctdb->monitor);
310
311         ctdb->monitor->next_interval = 5;
312
313         ctdb->monitor->monitor_context = talloc_new(ctdb->monitor);
314         CTDB_NO_MEMORY_FATAL(ctdb, ctdb->monitor->monitor_context);
315
316         te = event_add_timed(ctdb->ev, ctdb->monitor->monitor_context,
317                              timeval_current_ofs(1, 0), 
318                              ctdb_check_health, ctdb);
319         CTDB_NO_MEMORY_FATAL(ctdb, te);
320
321         ctdb->monitor->monitoring_mode  = CTDB_MONITORING_ACTIVE;
322         DEBUG(DEBUG_NOTICE,("Monitoring has been started\n"));
323 }
324
325
326 /*
327   modify flags on a node
328  */
329 int32_t ctdb_control_modflags(struct ctdb_context *ctdb, TDB_DATA indata)
330 {
331         struct ctdb_node_flag_change *c = (struct ctdb_node_flag_change *)indata.dptr;
332         struct ctdb_node *node;
333         uint32_t old_flags;
334         int i;
335
336         if (c->pnn >= ctdb->num_nodes) {
337                 DEBUG(DEBUG_ERR,(__location__ " Node %d is invalid, num_nodes :%d\n", c->pnn, ctdb->num_nodes));
338                 return -1;
339         }
340
341         node         = ctdb->nodes[c->pnn];
342         old_flags    = node->flags;
343         if (c->pnn != ctdb->pnn) {
344                 c->old_flags  = node->flags;
345         }
346         node->flags   = c->new_flags & ~NODE_FLAGS_DISCONNECTED;
347         node->flags  |= (c->old_flags & NODE_FLAGS_DISCONNECTED);
348
349         /* we dont let other nodes modify our STOPPED status */
350         if (c->pnn == ctdb->pnn) {
351                 node->flags &= ~NODE_FLAGS_STOPPED;
352                 if (old_flags & NODE_FLAGS_STOPPED) {
353                         node->flags |= NODE_FLAGS_STOPPED;
354                 }
355         }
356
357         /* we dont let other nodes modify our BANNED status */
358         if (c->pnn == ctdb->pnn) {
359                 node->flags &= ~NODE_FLAGS_BANNED;
360                 if (old_flags & NODE_FLAGS_BANNED) {
361                         node->flags |= NODE_FLAGS_BANNED;
362                 }
363         }
364
365         if (node->flags == c->old_flags) {
366                 DEBUG(DEBUG_INFO, ("Control modflags on node %u - Unchanged - flags 0x%x\n", c->pnn, node->flags));
367                 return 0;
368         }
369
370         DEBUG(DEBUG_INFO, ("Control modflags on node %u - flags now 0x%x\n", c->pnn, node->flags));
371
372
373         /* tell the recovery daemon something has changed */
374         ctdb_daemon_send_message(ctdb, ctdb->pnn,
375                                  CTDB_SRVID_SET_NODE_FLAGS, indata);
376
377         /* if we have become banned, we should go into recovery mode */
378         if ((node->flags & NODE_FLAGS_BANNED) && !(c->old_flags & NODE_FLAGS_BANNED) && (node->pnn == ctdb->pnn)) {
379                 /* make sure we are frozen */
380                 DEBUG(DEBUG_NOTICE,("This node has been banned - forcing freeze and recovery\n"));
381                 /* Reset the generation id to 1 to make us ignore any
382                    REQ/REPLY CALL/DMASTER someone sends to us.
383                    We are now banned so we shouldnt service database calls
384                    anymore.
385                 */
386                 ctdb->vnn_map->generation = INVALID_GENERATION;
387
388                 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
389                         if (ctdb_start_freeze(ctdb, i) != 0) {
390                                 DEBUG(DEBUG_ERR,(__location__ " Failed to freeze db priority %u\n", i));
391                         }
392                 }
393                 ctdb_release_all_ips(ctdb);
394                 ctdb->recovery_mode = CTDB_RECOVERY_ACTIVE;
395         }
396         
397         return 0;
398 }
399
400 /*
401   return the monitoring mode
402  */
403 int32_t ctdb_monitoring_mode(struct ctdb_context *ctdb)
404 {
405         if (ctdb->monitor == NULL) {
406                 return CTDB_MONITORING_DISABLED;
407         }
408         return ctdb->monitor->monitoring_mode;
409 }
410