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