fa642fb3fd7269a98a9267a5c58cbbf61976a86f
[sahlberg/ctdb.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/tevent/tevent.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 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 = ctdb_fork(ctdb);
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                 debug_extra = talloc_asprintf(NULL, "notification-%s:", event);
95                 ret = ctdb_run_notification_script_child(ctdb, event);
96                 if (ret != 0) {
97                         DEBUG(DEBUG_ERR,(__location__ " Notification script failed\n"));
98                 }
99                 _exit(0);
100         }
101
102         return;
103 }
104
105 /*
106   called when a health monitoring event script finishes
107  */
108 static void ctdb_health_callback(struct ctdb_context *ctdb, int status, void *p)
109 {
110         struct ctdb_node *node = ctdb->nodes[ctdb->pnn];
111         TDB_DATA data;
112         struct ctdb_node_flag_change c;
113         uint32_t next_interval;
114         int ret;
115         TDB_DATA rddata;
116         struct takeover_run_reply rd;
117
118         c.pnn = ctdb->pnn;
119         c.old_flags = node->flags;
120
121         rd.pnn   = ctdb->pnn;
122         rd.srvid = CTDB_SRVID_TAKEOVER_RUN_RESPONSE;
123
124         rddata.dptr = (uint8_t *)&rd;
125         rddata.dsize = sizeof(rd);
126
127         if (status == -ETIME) {
128                 ctdb->event_script_timeouts++;
129
130                 if (ctdb->event_script_timeouts >= ctdb->tunable.script_timeout_count) {
131                         DEBUG(DEBUG_ERR, ("Maximum timeout count %u reached for eventscript. Making node unhealthy\n", ctdb->tunable.script_timeout_count));
132                 } else {
133                         /* We pretend this is OK. */
134                         goto after_change_status;
135                 }
136         }
137
138         if (status != 0 && !(node->flags & NODE_FLAGS_UNHEALTHY)) {
139                 DEBUG(DEBUG_NOTICE,("monitor event failed - disabling node\n"));
140                 node->flags |= NODE_FLAGS_UNHEALTHY;
141                 ctdb->monitor->next_interval = 5;
142
143                 ctdb_run_notification_script(ctdb, "unhealthy");
144
145                 /* ask the recmaster to reallocate all addresses */
146                 DEBUG(DEBUG_ERR,("Node became UNHEALTHY. Ask recovery master %u to perform ip reallocation\n", ctdb->recovery_master));
147                 ret = ctdb_daemon_send_message(ctdb, ctdb->recovery_master, CTDB_SRVID_TAKEOVER_RUN, rddata);
148                 if (ret != 0) {
149                         DEBUG(DEBUG_ERR,(__location__ " Failed to send ip takeover run request message to %u\n", ctdb->recovery_master));
150                 }
151
152         } else if (status == 0 && (node->flags & NODE_FLAGS_UNHEALTHY)) {
153                 DEBUG(DEBUG_NOTICE,("monitor event OK - node re-enabled\n"));
154                 node->flags &= ~NODE_FLAGS_UNHEALTHY;
155                 ctdb->monitor->next_interval = 5;
156
157                 ctdb_run_notification_script(ctdb, "healthy");
158
159                 /* ask the recmaster to reallocate all addresses */
160                 DEBUG(DEBUG_ERR,("Node became HEALTHY. Ask recovery master %u to perform ip reallocation\n", ctdb->recovery_master));
161                 ret = ctdb_daemon_send_message(ctdb, ctdb->recovery_master, CTDB_SRVID_TAKEOVER_RUN, rddata);
162                 if (ret != 0) {
163                         DEBUG(DEBUG_ERR,(__location__ " Failed to send ip takeover run request message to %u\n", ctdb->recovery_master));
164                 }
165
166         }
167
168 after_change_status:
169         next_interval = ctdb->monitor->next_interval;
170
171         ctdb->monitor->next_interval *= 2;
172         if (ctdb->monitor->next_interval > ctdb->tunable.monitor_interval) {
173                 ctdb->monitor->next_interval = ctdb->tunable.monitor_interval;
174         }
175
176         event_add_timed(ctdb->ev, ctdb->monitor->monitor_context, 
177                                 timeval_current_ofs(next_interval, 0), 
178                                 ctdb_check_health, ctdb);
179
180         if (c.old_flags == node->flags) {
181                 return;
182         }
183
184         c.new_flags = node->flags;
185
186         data.dptr = (uint8_t *)&c;
187         data.dsize = sizeof(c);
188
189         /* ask the recovery daemon to push these changes out to all nodes */
190         ctdb_daemon_send_message(ctdb, ctdb->pnn,
191                                  CTDB_SRVID_PUSH_NODE_FLAGS, data);
192
193 }
194
195
196 /*
197   called when the startup event script finishes
198  */
199 static void ctdb_startup_callback(struct ctdb_context *ctdb, int status, void *p)
200 {
201         if (status != 0) {
202                 DEBUG(DEBUG_ERR,("startup event failed\n"));
203         } else if (status == 0) {
204                 DEBUG(DEBUG_NOTICE,("startup event OK - enabling monitoring\n"));
205                 ctdb->done_startup = true;
206                 ctdb->monitor->next_interval = 2;
207                 ctdb_run_notification_script(ctdb, "startup");
208         }
209
210         event_add_timed(ctdb->ev, ctdb->monitor->monitor_context, 
211                         timeval_current_ofs(ctdb->monitor->next_interval, 0),
212                         ctdb_check_health, ctdb);
213 }
214
215
216 /*
217   wait until we have finished initial recoveries before we start the
218   monitoring events
219  */
220 static void ctdb_wait_until_recovered(struct event_context *ev, struct timed_event *te, 
221                               struct timeval t, void *private_data)
222 {
223         struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
224         int ret;
225
226         DEBUG(DEBUG_NOTICE,("CTDB_WAIT_UNTIL_RECOVERED\n"));
227
228         if (ctdb->vnn_map->generation == INVALID_GENERATION) {
229                 ctdb->db_persistent_startup_generation = INVALID_GENERATION;
230
231                 DEBUG(DEBUG_NOTICE,(__location__ " generation is INVALID. Wait one more second\n"));
232                 event_add_timed(ctdb->ev, ctdb->monitor->monitor_context,
233                                      timeval_current_ofs(1, 0), 
234                                      ctdb_wait_until_recovered, ctdb);
235                 return;
236         }
237
238         if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
239                 ctdb->db_persistent_startup_generation = INVALID_GENERATION;
240
241                 DEBUG(DEBUG_NOTICE,(__location__ " in recovery. Wait one more second\n"));
242                 event_add_timed(ctdb->ev, ctdb->monitor->monitor_context,
243                                      timeval_current_ofs(1, 0), 
244                                      ctdb_wait_until_recovered, ctdb);
245                 return;
246         }
247
248
249         if (!fast_start && timeval_elapsed(&ctdb->last_recovery_finished) < (ctdb->tunable.rerecovery_timeout + 3)) {
250                 ctdb->db_persistent_startup_generation = INVALID_GENERATION;
251
252                 DEBUG(DEBUG_NOTICE,(__location__ " wait for pending recoveries to end. Wait one more second.\n"));
253
254                 event_add_timed(ctdb->ev, ctdb->monitor->monitor_context,
255                                      timeval_current_ofs(1, 0), 
256                                      ctdb_wait_until_recovered, ctdb);
257                 return;
258         }
259
260         if (ctdb->vnn_map->generation == ctdb->db_persistent_startup_generation) {
261                 DEBUG(DEBUG_INFO,(__location__ " skip ctdb_recheck_persistent_health() "
262                                   "until the next recovery\n"));
263                 event_add_timed(ctdb->ev, ctdb->monitor->monitor_context,
264                                      timeval_current_ofs(1, 0),
265                                      ctdb_wait_until_recovered, ctdb);
266                 return;
267         }
268
269         ctdb->db_persistent_startup_generation = ctdb->vnn_map->generation;
270         ret = ctdb_recheck_persistent_health(ctdb);
271         if (ret != 0) {
272                 ctdb->db_persistent_check_errors++;
273                 if (ctdb->db_persistent_check_errors < ctdb->max_persistent_check_errors) {
274                         DEBUG(ctdb->db_persistent_check_errors==1?DEBUG_ERR:DEBUG_WARNING,
275                               (__location__ "ctdb_recheck_persistent_health() "
276                               "failed (%llu of %llu times) - retry later\n",
277                               (unsigned long long)ctdb->db_persistent_check_errors,
278                               (unsigned long long)ctdb->max_persistent_check_errors));
279                         event_add_timed(ctdb->ev,
280                                         ctdb->monitor->monitor_context,
281                                         timeval_current_ofs(1, 0),
282                                         ctdb_wait_until_recovered, ctdb);
283                         return;
284                 }
285                 DEBUG(DEBUG_ALERT,(__location__
286                                   "ctdb_recheck_persistent_health() failed (%llu times) - prepare shutdown\n",
287                                   (unsigned long long)ctdb->db_persistent_check_errors));
288                 ctdb_stop_recoverd(ctdb);
289                 ctdb_stop_keepalive(ctdb);
290                 ctdb_stop_monitoring(ctdb);
291                 ctdb_release_all_ips(ctdb);
292                 if (ctdb->methods != NULL) {
293                         ctdb->methods->shutdown(ctdb);
294                 }
295                 ctdb_event_script(ctdb, CTDB_EVENT_SHUTDOWN);
296                 DEBUG(DEBUG_ALERT,("ctdb_recheck_persistent_health() failed - Stopping CTDB daemon\n"));
297                 exit(11);
298         }
299         ctdb->db_persistent_check_errors = 0;
300         DEBUG(DEBUG_NOTICE,(__location__
301                            "ctdb_start_monitoring: ctdb_recheck_persistent_health() OK\n"));
302
303         DEBUG(DEBUG_NOTICE,(__location__ " Recoveries finished. Running the \"startup\" event.\n"));
304         event_add_timed(ctdb->ev, ctdb->monitor->monitor_context,
305                              timeval_current(),
306                              ctdb_check_health, ctdb);
307 }
308
309
310 /*
311   see if the event scripts think we are healthy
312  */
313 static void ctdb_check_health(struct event_context *ev, struct timed_event *te, 
314                               struct timeval t, void *private_data)
315 {
316         struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
317         int ret = 0;
318
319         if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL ||
320             (ctdb->monitor->monitoring_mode == CTDB_MONITORING_DISABLED && ctdb->done_startup)) {
321                 event_add_timed(ctdb->ev, ctdb->monitor->monitor_context,
322                                 timeval_current_ofs(ctdb->monitor->next_interval, 0), 
323                                 ctdb_check_health, ctdb);
324                 return;
325         }
326         
327         if (!ctdb->done_startup) {
328                 ret = ctdb_event_script_callback(ctdb, 
329                                                  ctdb->monitor->monitor_context, ctdb_startup_callback, 
330                                                  ctdb, false,
331                                                  CTDB_EVENT_STARTUP, "%s", "");
332         } else {
333                 int i;
334                 int skip_monitoring = 0;
335                 
336                 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
337                         skip_monitoring = 1;
338                         DEBUG(DEBUG_ERR,("Skip monitoring during recovery\n"));
339                 }
340                 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
341                         if (ctdb->freeze_handles[i] != NULL) {
342                                 DEBUG(DEBUG_ERR,("Skip monitoring since databases are frozen\n"));
343                                 skip_monitoring = 1;
344                                 break;
345                         }
346                 }
347                 if (skip_monitoring != 0) {
348                         event_add_timed(ctdb->ev, ctdb->monitor->monitor_context,
349                                         timeval_current_ofs(ctdb->monitor->next_interval, 0), 
350                                         ctdb_check_health, ctdb);
351                         return;
352                 } else {
353                         ret = ctdb_event_script_callback(ctdb, 
354                                         ctdb->monitor->monitor_context, ctdb_health_callback,
355                                         ctdb, false,
356                                         CTDB_EVENT_MONITOR, "%s", "");
357                 }
358         }
359
360         if (ret != 0) {
361                 DEBUG(DEBUG_ERR,("Unable to launch monitor event script\n"));
362                 ctdb->monitor->next_interval = 5;
363                 event_add_timed(ctdb->ev, ctdb->monitor->monitor_context, 
364                         timeval_current_ofs(5, 0), 
365                         ctdb_check_health, ctdb);
366         }
367 }
368
369 /* 
370   (Temporaily) Disabling monitoring will stop the monitor event scripts
371   from running   but node health checks will still occur
372 */
373 void ctdb_disable_monitoring(struct ctdb_context *ctdb)
374 {
375         ctdb->monitor->monitoring_mode = CTDB_MONITORING_DISABLED;
376         DEBUG(DEBUG_INFO,("Monitoring has been disabled\n"));
377 }
378
379 /* 
380    Re-enable running monitor events after they have been disabled
381  */
382 void ctdb_enable_monitoring(struct ctdb_context *ctdb)
383 {
384         ctdb->monitor->monitoring_mode  = CTDB_MONITORING_ACTIVE;
385         ctdb->monitor->next_interval = 5;
386         DEBUG(DEBUG_INFO,("Monitoring has been enabled\n"));
387 }
388
389 /* stop any monitoring 
390    this should only be done when shutting down the daemon
391 */
392 void ctdb_stop_monitoring(struct ctdb_context *ctdb)
393 {
394         talloc_free(ctdb->monitor->monitor_context);
395         ctdb->monitor->monitor_context = NULL;
396
397         ctdb->monitor->monitoring_mode  = CTDB_MONITORING_DISABLED;
398         ctdb->monitor->next_interval = 5;
399         DEBUG(DEBUG_NOTICE,("Monitoring has been stopped\n"));
400 }
401
402 /*
403   start watching for nodes that might be dead
404  */
405 void ctdb_start_monitoring(struct ctdb_context *ctdb)
406 {
407         if (ctdb->monitor != NULL) {
408                 return;
409         }
410
411         ctdb->monitor = talloc(ctdb, struct ctdb_monitor_state);
412         CTDB_NO_MEMORY_FATAL(ctdb, ctdb->monitor);
413
414         ctdb->monitor->next_interval = 5;
415
416         ctdb->monitor->monitor_context = talloc_new(ctdb->monitor);
417         CTDB_NO_MEMORY_FATAL(ctdb, ctdb->monitor->monitor_context);
418
419         event_add_timed(ctdb->ev, ctdb->monitor->monitor_context,
420                              timeval_current_ofs(1, 0), 
421                              ctdb_wait_until_recovered, ctdb);
422
423         ctdb->monitor->monitoring_mode  = CTDB_MONITORING_ACTIVE;
424         DEBUG(DEBUG_NOTICE,("Monitoring has been started\n"));
425 }
426
427
428 /*
429   modify flags on a node
430  */
431 int32_t ctdb_control_modflags(struct ctdb_context *ctdb, TDB_DATA indata)
432 {
433         struct ctdb_node_flag_change *c = (struct ctdb_node_flag_change *)indata.dptr;
434         struct ctdb_node *node;
435         uint32_t old_flags;
436         int i;
437
438         if (c->pnn >= ctdb->num_nodes) {
439                 DEBUG(DEBUG_ERR,(__location__ " Node %d is invalid, num_nodes :%d\n", c->pnn, ctdb->num_nodes));
440                 return -1;
441         }
442
443         node         = ctdb->nodes[c->pnn];
444         old_flags    = node->flags;
445         if (c->pnn != ctdb->pnn) {
446                 c->old_flags  = node->flags;
447         }
448         node->flags   = c->new_flags & ~NODE_FLAGS_DISCONNECTED;
449         node->flags  |= (c->old_flags & NODE_FLAGS_DISCONNECTED);
450
451         /* we dont let other nodes modify our STOPPED status */
452         if (c->pnn == ctdb->pnn) {
453                 node->flags &= ~NODE_FLAGS_STOPPED;
454                 if (old_flags & NODE_FLAGS_STOPPED) {
455                         node->flags |= NODE_FLAGS_STOPPED;
456                 }
457         }
458
459         /* we dont let other nodes modify our BANNED status */
460         if (c->pnn == ctdb->pnn) {
461                 node->flags &= ~NODE_FLAGS_BANNED;
462                 if (old_flags & NODE_FLAGS_BANNED) {
463                         node->flags |= NODE_FLAGS_BANNED;
464                 }
465         }
466
467         if (node->flags == c->old_flags) {
468                 DEBUG(DEBUG_INFO, ("Control modflags on node %u - Unchanged - flags 0x%x\n", c->pnn, node->flags));
469                 return 0;
470         }
471
472         DEBUG(DEBUG_INFO, ("Control modflags on node %u - flags now 0x%x\n", c->pnn, node->flags));
473
474         if (node->flags == 0 && !ctdb->done_startup) {
475                 DEBUG(DEBUG_ERR, (__location__ " Node %u became healthy - force recovery for startup\n",
476                                   c->pnn));
477                 ctdb->recovery_mode = CTDB_RECOVERY_ACTIVE;
478         }
479
480         /* tell the recovery daemon something has changed */
481         ctdb_daemon_send_message(ctdb, ctdb->pnn,
482                                  CTDB_SRVID_SET_NODE_FLAGS, indata);
483
484         /* if we have become banned, we should go into recovery mode */
485         if ((node->flags & NODE_FLAGS_BANNED) && !(c->old_flags & NODE_FLAGS_BANNED) && (node->pnn == ctdb->pnn)) {
486                 /* make sure we are frozen */
487                 DEBUG(DEBUG_NOTICE,("This node has been banned - forcing freeze and recovery\n"));
488                 /* Reset the generation id to 1 to make us ignore any
489                    REQ/REPLY CALL/DMASTER someone sends to us.
490                    We are now banned so we shouldnt service database calls
491                    anymore.
492                 */
493                 ctdb->vnn_map->generation = INVALID_GENERATION;
494
495                 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
496                         if (ctdb_start_freeze(ctdb, i) != 0) {
497                                 DEBUG(DEBUG_ERR,(__location__ " Failed to freeze db priority %u\n", i));
498                         }
499                 }
500                 ctdb_release_all_ips(ctdb);
501                 ctdb->recovery_mode = CTDB_RECOVERY_ACTIVE;
502         }
503         
504         return 0;
505 }
506
507 /*
508   return the monitoring mode
509  */
510 int32_t ctdb_monitoring_mode(struct ctdb_context *ctdb)
511 {
512         if (ctdb->monitor == NULL) {
513                 return CTDB_MONITORING_DISABLED;
514         }
515         return ctdb->monitor->monitoring_mode;
516 }
517