2 monitoring links to all other nodes to detect dead nodes
5 Copyright (C) Ronnie Sahlberg 2007
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.
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.
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/>.
22 #include "lib/tevent/tevent.h"
23 #include "system/filesys.h"
24 #include "system/wait.h"
25 #include "../include/ctdb_private.h"
27 struct ctdb_monitor_state {
28 uint32_t monitoring_mode;
29 TALLOC_CTX *monitor_context;
30 uint32_t next_interval;
33 static void ctdb_check_health(struct event_context *ev, struct timed_event *te,
34 struct timeval t, void *private_data);
37 setup the notification script
39 int ctdb_set_notification_script(struct ctdb_context *ctdb, const char *script)
41 ctdb->notification_script = talloc_strdup(ctdb, script);
42 CTDB_NO_MEMORY(ctdb, ctdb->notification_script);
46 static int ctdb_run_notification_script_child(struct ctdb_context *ctdb, const char *event)
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));
56 if (!(st.st_mode & S_IXUSR)) {
57 DEBUG(DEBUG_ERR,("Notification script %s is not executable.\n", ctdb->notification_script));
61 cmd = talloc_asprintf(ctdb, "%s %s\n", ctdb->notification_script, event);
62 CTDB_NO_MEMORY(ctdb, cmd);
65 /* if the system() call was successful, translate ret into the
66 return code from the command
69 ret = WEXITSTATUS(ret);
72 DEBUG(DEBUG_ERR,("Notification script \"%s\" failed with error %d\n", cmd, ret));
78 void ctdb_run_notification_script(struct ctdb_context *ctdb, const char *event)
82 if (ctdb->notification_script == NULL) {
86 child = ctdb_fork(ctdb);
87 if (child == (pid_t)-1) {
88 DEBUG(DEBUG_ERR,("Failed to fork() a notification child process\n"));
94 debug_extra = talloc_asprintf(NULL, "notification-%s:", event);
95 ret = ctdb_run_notification_script_child(ctdb, event);
97 DEBUG(DEBUG_ERR,(__location__ " Notification script failed\n"));
106 called when a health monitoring event script finishes
108 static void ctdb_health_callback(struct ctdb_context *ctdb, int status, void *p)
110 struct ctdb_node *node = ctdb->nodes[ctdb->pnn];
112 struct ctdb_node_flag_change c;
113 uint32_t next_interval;
116 struct takeover_run_reply rd;
117 const char *state_str = NULL;
120 c.old_flags = node->flags;
123 rd.srvid = CTDB_SRVID_TAKEOVER_RUN_RESPONSE;
125 rddata.dptr = (uint8_t *)&rd;
126 rddata.dsize = sizeof(rd);
128 if (status == -ETIME) {
129 ctdb->event_script_timeouts++;
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));
134 /* We pretend this is OK. */
135 goto after_change_status;
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;
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;
150 ctdb_run_notification_script(ctdb, "healthy");
154 next_interval = ctdb->monitor->next_interval;
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;
161 event_add_timed(ctdb->ev, ctdb->monitor->monitor_context,
162 timeval_current_ofs(next_interval, 0),
163 ctdb_check_health, ctdb);
165 if (c.old_flags == node->flags) {
169 c.new_flags = node->flags;
171 data.dptr = (uint8_t *)&c;
172 data.dsize = sizeof(c);
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);
178 if (c.new_flags & NODE_FLAGS_UNHEALTHY) {
179 state_str = "UNHEALTHY";
181 state_str = "HEALTHY";
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);
189 DEBUG(DEBUG_ERR,(__location__ " Failed to send ip takeover run request message to %u\n", ctdb->recovery_master));
195 called when the startup event script finishes
197 static void ctdb_startup_callback(struct ctdb_context *ctdb, int status, void *p)
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");
208 event_add_timed(ctdb->ev, ctdb->monitor->monitor_context,
209 timeval_current_ofs(ctdb->monitor->next_interval, 0),
210 ctdb_check_health, ctdb);
215 wait until we have finished initial recoveries before we start the
218 static void ctdb_wait_until_recovered(struct event_context *ev, struct timed_event *te,
219 struct timeval t, void *private_data)
221 struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
223 static int count = 0;
227 if (count < 60 || count%600 == 0) {
228 DEBUG(DEBUG_NOTICE,("CTDB_WAIT_UNTIL_RECOVERED\n"));
229 if (ctdb->nodes[ctdb->pnn]->flags & NODE_FLAGS_STOPPED) {
230 DEBUG(DEBUG_NOTICE,("Node is STOPPED. Node will NOT recover.\n"));
234 if (ctdb->vnn_map->generation == INVALID_GENERATION) {
235 ctdb->db_persistent_startup_generation = INVALID_GENERATION;
237 event_add_timed(ctdb->ev, ctdb->monitor->monitor_context,
238 timeval_current_ofs(1, 0),
239 ctdb_wait_until_recovered, ctdb);
243 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
244 ctdb->db_persistent_startup_generation = INVALID_GENERATION;
246 DEBUG(DEBUG_NOTICE,(__location__ " in recovery. Wait one more second\n"));
247 event_add_timed(ctdb->ev, ctdb->monitor->monitor_context,
248 timeval_current_ofs(1, 0),
249 ctdb_wait_until_recovered, ctdb);
254 if (!fast_start && timeval_elapsed(&ctdb->last_recovery_finished) < (ctdb->tunable.rerecovery_timeout + 3)) {
255 ctdb->db_persistent_startup_generation = INVALID_GENERATION;
257 DEBUG(DEBUG_NOTICE,(__location__ " wait for pending recoveries to end. Wait one more second.\n"));
259 event_add_timed(ctdb->ev, ctdb->monitor->monitor_context,
260 timeval_current_ofs(1, 0),
261 ctdb_wait_until_recovered, ctdb);
265 if (ctdb->vnn_map->generation == ctdb->db_persistent_startup_generation) {
266 DEBUG(DEBUG_INFO,(__location__ " skip ctdb_recheck_persistent_health() "
267 "until the next recovery\n"));
268 event_add_timed(ctdb->ev, ctdb->monitor->monitor_context,
269 timeval_current_ofs(1, 0),
270 ctdb_wait_until_recovered, ctdb);
274 ctdb->db_persistent_startup_generation = ctdb->vnn_map->generation;
275 ret = ctdb_recheck_persistent_health(ctdb);
277 ctdb->db_persistent_check_errors++;
278 if (ctdb->db_persistent_check_errors < ctdb->max_persistent_check_errors) {
279 DEBUG(ctdb->db_persistent_check_errors==1?DEBUG_ERR:DEBUG_WARNING,
280 (__location__ "ctdb_recheck_persistent_health() "
281 "failed (%llu of %llu times) - retry later\n",
282 (unsigned long long)ctdb->db_persistent_check_errors,
283 (unsigned long long)ctdb->max_persistent_check_errors));
284 event_add_timed(ctdb->ev,
285 ctdb->monitor->monitor_context,
286 timeval_current_ofs(1, 0),
287 ctdb_wait_until_recovered, ctdb);
290 DEBUG(DEBUG_ALERT,(__location__
291 "ctdb_recheck_persistent_health() failed (%llu times) - prepare shutdown\n",
292 (unsigned long long)ctdb->db_persistent_check_errors));
293 ctdb_stop_recoverd(ctdb);
294 ctdb_stop_keepalive(ctdb);
295 ctdb_stop_monitoring(ctdb);
296 ctdb_release_all_ips(ctdb);
297 if (ctdb->methods != NULL) {
298 ctdb->methods->shutdown(ctdb);
300 ctdb_event_script(ctdb, CTDB_EVENT_SHUTDOWN);
301 DEBUG(DEBUG_ALERT,("ctdb_recheck_persistent_health() failed - Stopping CTDB daemon\n"));
304 ctdb->db_persistent_check_errors = 0;
306 DEBUG(DEBUG_NOTICE,(__location__ " Recoveries finished. Running the \"startup\" event.\n"));
307 event_add_timed(ctdb->ev, ctdb->monitor->monitor_context,
309 ctdb_check_health, ctdb);
314 see if the event scripts think we are healthy
316 static void ctdb_check_health(struct event_context *ev, struct timed_event *te,
317 struct timeval t, void *private_data)
319 struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
322 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL ||
323 (ctdb->monitor->monitoring_mode == CTDB_MONITORING_DISABLED && ctdb->done_startup)) {
324 event_add_timed(ctdb->ev, ctdb->monitor->monitor_context,
325 timeval_current_ofs(ctdb->monitor->next_interval, 0),
326 ctdb_check_health, ctdb);
330 if (!ctdb->done_startup) {
331 ret = ctdb_event_script_callback(ctdb,
332 ctdb->monitor->monitor_context, ctdb_startup_callback,
334 CTDB_EVENT_STARTUP, "%s", "");
337 int skip_monitoring = 0;
339 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
341 DEBUG(DEBUG_ERR,("Skip monitoring during recovery\n"));
343 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
344 if (ctdb->freeze_handles[i] != NULL) {
345 DEBUG(DEBUG_ERR,("Skip monitoring since databases are frozen\n"));
350 if (skip_monitoring != 0) {
351 event_add_timed(ctdb->ev, ctdb->monitor->monitor_context,
352 timeval_current_ofs(ctdb->monitor->next_interval, 0),
353 ctdb_check_health, ctdb);
356 ret = ctdb_event_script_callback(ctdb,
357 ctdb->monitor->monitor_context, ctdb_health_callback,
359 CTDB_EVENT_MONITOR, "%s", "");
364 DEBUG(DEBUG_ERR,("Unable to launch monitor event script\n"));
365 ctdb->monitor->next_interval = 5;
366 event_add_timed(ctdb->ev, ctdb->monitor->monitor_context,
367 timeval_current_ofs(5, 0),
368 ctdb_check_health, ctdb);
373 (Temporaily) Disabling monitoring will stop the monitor event scripts
374 from running but node health checks will still occur
376 void ctdb_disable_monitoring(struct ctdb_context *ctdb)
378 ctdb->monitor->monitoring_mode = CTDB_MONITORING_DISABLED;
379 DEBUG(DEBUG_INFO,("Monitoring has been disabled\n"));
383 Re-enable running monitor events after they have been disabled
385 void ctdb_enable_monitoring(struct ctdb_context *ctdb)
387 ctdb->monitor->monitoring_mode = CTDB_MONITORING_ACTIVE;
388 ctdb->monitor->next_interval = 5;
389 DEBUG(DEBUG_INFO,("Monitoring has been enabled\n"));
392 /* stop any monitoring
393 this should only be done when shutting down the daemon
395 void ctdb_stop_monitoring(struct ctdb_context *ctdb)
397 talloc_free(ctdb->monitor->monitor_context);
398 ctdb->monitor->monitor_context = NULL;
400 ctdb->monitor->monitoring_mode = CTDB_MONITORING_DISABLED;
401 ctdb->monitor->next_interval = 5;
402 DEBUG(DEBUG_NOTICE,("Monitoring has been stopped\n"));
406 start watching for nodes that might be dead
408 void ctdb_start_monitoring(struct ctdb_context *ctdb)
410 if (ctdb->monitor != NULL) {
414 ctdb->monitor = talloc(ctdb, struct ctdb_monitor_state);
415 CTDB_NO_MEMORY_FATAL(ctdb, ctdb->monitor);
417 ctdb->monitor->next_interval = 5;
419 ctdb->monitor->monitor_context = talloc_new(ctdb->monitor);
420 CTDB_NO_MEMORY_FATAL(ctdb, ctdb->monitor->monitor_context);
422 event_add_timed(ctdb->ev, ctdb->monitor->monitor_context,
423 timeval_current_ofs(1, 0),
424 ctdb_wait_until_recovered, ctdb);
426 ctdb->monitor->monitoring_mode = CTDB_MONITORING_ACTIVE;
427 DEBUG(DEBUG_NOTICE,("Monitoring has been started\n"));
432 modify flags on a node
434 int32_t ctdb_control_modflags(struct ctdb_context *ctdb, TDB_DATA indata)
436 struct ctdb_node_flag_change *c = (struct ctdb_node_flag_change *)indata.dptr;
437 struct ctdb_node *node;
440 if (c->pnn >= ctdb->num_nodes) {
441 DEBUG(DEBUG_ERR,(__location__ " Node %d is invalid, num_nodes :%d\n", c->pnn, ctdb->num_nodes));
445 node = ctdb->nodes[c->pnn];
446 old_flags = node->flags;
447 if (c->pnn != ctdb->pnn) {
448 c->old_flags = node->flags;
450 node->flags = c->new_flags & ~NODE_FLAGS_DISCONNECTED;
451 node->flags |= (c->old_flags & NODE_FLAGS_DISCONNECTED);
453 /* we dont let other nodes modify our STOPPED status */
454 if (c->pnn == ctdb->pnn) {
455 node->flags &= ~NODE_FLAGS_STOPPED;
456 if (old_flags & NODE_FLAGS_STOPPED) {
457 node->flags |= NODE_FLAGS_STOPPED;
461 /* we dont let other nodes modify our BANNED status */
462 if (c->pnn == ctdb->pnn) {
463 node->flags &= ~NODE_FLAGS_BANNED;
464 if (old_flags & NODE_FLAGS_BANNED) {
465 node->flags |= NODE_FLAGS_BANNED;
469 if (node->flags == c->old_flags) {
470 DEBUG(DEBUG_INFO, ("Control modflags on node %u - Unchanged - flags 0x%x\n", c->pnn, node->flags));
474 DEBUG(DEBUG_INFO, ("Control modflags on node %u - flags now 0x%x\n", c->pnn, node->flags));
476 if (node->flags == 0 && !ctdb->done_startup) {
477 DEBUG(DEBUG_ERR, (__location__ " Node %u became healthy - force recovery for startup\n",
479 ctdb->recovery_mode = CTDB_RECOVERY_ACTIVE;
482 /* tell the recovery daemon something has changed */
483 ctdb_daemon_send_message(ctdb, ctdb->pnn,
484 CTDB_SRVID_SET_NODE_FLAGS, indata);
486 /* if we have become banned, we should go into recovery mode */
487 if ((node->flags & NODE_FLAGS_BANNED) && !(c->old_flags & NODE_FLAGS_BANNED) && (node->pnn == ctdb->pnn)) {
488 return ctdb_local_node_got_banned(ctdb);
495 return the monitoring mode
497 int32_t ctdb_monitoring_mode(struct ctdb_context *ctdb)
499 if (ctdb->monitor == NULL) {
500 return CTDB_MONITORING_DISABLED;
502 return ctdb->monitor->monitoring_mode;