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/events/events.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 static void ctdb_run_notification_script(struct ctdb_context *ctdb, const char *event)
82 if (ctdb->notification_script == NULL) {
87 if (child == (pid_t)-1) {
88 DEBUG(DEBUG_ERR,("Failed to fork() a notification child process\n"));
94 ret = ctdb_run_notification_script_child(ctdb, event);
96 DEBUG(DEBUG_ERR,(__location__ " Notification script failed\n"));
105 called when a health monitoring event script finishes
107 static void ctdb_health_callback(struct ctdb_context *ctdb, int status, void *p)
109 struct ctdb_node *node = ctdb->nodes[ctdb->pnn];
111 struct ctdb_node_flag_change c;
112 uint32_t next_interval;
115 struct takeover_run_reply rd;
118 c.old_flags = node->flags;
121 rd.srvid = CTDB_SRVID_TAKEOVER_RUN_RESPONSE;
123 rddata.dptr = (uint8_t *)&rd;
124 rddata.dsize = sizeof(rd);
126 if (status == -ETIME) {
127 ctdb->event_script_timeouts++;
129 if (ctdb->event_script_timeouts > ctdb->tunable.script_ban_count) {
130 DEBUG(DEBUG_ERR, ("Maximum timeout count %u reached for eventscript. Making node unhealthy\n", ctdb->tunable.script_ban_count));
132 /* We pretend this is OK. */
133 goto after_change_status;
137 if (status != 0 && !(node->flags & NODE_FLAGS_UNHEALTHY)) {
138 DEBUG(DEBUG_NOTICE,("monitor event failed - disabling node\n"));
139 node->flags |= NODE_FLAGS_UNHEALTHY;
140 ctdb->monitor->next_interval = 5;
141 if (ctdb->tunable.disable_when_unhealthy != 0) {
142 DEBUG(DEBUG_INFO, ("DISABLING node since it became unhealthy\n"));
143 node->flags |= NODE_FLAGS_DISABLED;
146 ctdb_run_notification_script(ctdb, "unhealthy");
148 /* ask the recmaster to reallocate all addresses */
149 DEBUG(DEBUG_ERR,("Node became UNHEALTHY. Ask recovery master %u to perform ip reallocation\n", ctdb->recovery_master));
150 ret = ctdb_daemon_send_message(ctdb, ctdb->recovery_master, CTDB_SRVID_TAKEOVER_RUN, rddata);
152 DEBUG(DEBUG_ERR,(__location__ " Failed to send ip takeover run request message to %u\n", ctdb->recovery_master));
155 } else if (status == 0 && (node->flags & NODE_FLAGS_UNHEALTHY)) {
156 DEBUG(DEBUG_NOTICE,("monitor event OK - node re-enabled\n"));
157 node->flags &= ~NODE_FLAGS_UNHEALTHY;
158 ctdb->monitor->next_interval = 5;
160 ctdb_run_notification_script(ctdb, "healthy");
162 /* ask the recmaster to reallocate all addresses */
163 DEBUG(DEBUG_ERR,("Node became HEALTHY. Ask recovery master %u to perform ip reallocation\n", ctdb->recovery_master));
164 ret = ctdb_daemon_send_message(ctdb, ctdb->recovery_master, CTDB_SRVID_TAKEOVER_RUN, rddata);
166 DEBUG(DEBUG_ERR,(__location__ " Failed to send ip takeover run request message to %u\n", ctdb->recovery_master));
172 next_interval = ctdb->monitor->next_interval;
174 ctdb->monitor->next_interval *= 2;
175 if (ctdb->monitor->next_interval > ctdb->tunable.monitor_interval) {
176 ctdb->monitor->next_interval = ctdb->tunable.monitor_interval;
179 event_add_timed(ctdb->ev, ctdb->monitor->monitor_context,
180 timeval_current_ofs(next_interval, 0),
181 ctdb_check_health, ctdb);
183 if (c.old_flags == node->flags) {
187 c.new_flags = node->flags;
189 data.dptr = (uint8_t *)&c;
190 data.dsize = sizeof(c);
192 /* ask the recovery daemon to push these changes out to all nodes */
193 ctdb_daemon_send_message(ctdb, ctdb->pnn,
194 CTDB_SRVID_PUSH_NODE_FLAGS, data);
200 called when the startup event script finishes
202 static void ctdb_startup_callback(struct ctdb_context *ctdb, int status, void *p)
205 DEBUG(DEBUG_ERR,("startup event failed\n"));
206 } else if (status == 0) {
207 DEBUG(DEBUG_NOTICE,("startup event OK - enabling monitoring\n"));
208 ctdb->done_startup = true;
209 ctdb->monitor->next_interval = 5;
210 ctdb_run_notification_script(ctdb, "startup");
213 event_add_timed(ctdb->ev, ctdb->monitor->monitor_context,
214 timeval_current_ofs(ctdb->monitor->next_interval, 0),
215 ctdb_check_health, ctdb);
220 wait until we have finished initial recoveries before we start the
223 static void ctdb_wait_until_recovered(struct event_context *ev, struct timed_event *te,
224 struct timeval t, void *private_data)
226 struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
228 DEBUG(DEBUG_NOTICE,("CTDB_WAIT_UNTIL_RECOVERED\n"));
230 if (ctdb->vnn_map->generation == INVALID_GENERATION) {
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);
238 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
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);
247 if (timeval_elapsed(&ctdb->last_recovery_finished) < (ctdb->tunable.rerecovery_timeout + 3)) {
248 DEBUG(DEBUG_NOTICE,(__location__ " wait for pending recoveries to end. Wait one more second.\n"));
250 event_add_timed(ctdb->ev, ctdb->monitor->monitor_context,
251 timeval_current_ofs(1, 0),
252 ctdb_wait_until_recovered, ctdb);
257 DEBUG(DEBUG_NOTICE,(__location__ " Recoveries finished. Running the \"startup\" event.\n"));
258 event_add_timed(ctdb->ev, ctdb->monitor->monitor_context,
259 timeval_current_ofs(1, 0),
260 ctdb_check_health, ctdb);
265 see if the event scripts think we are healthy
267 static void ctdb_check_health(struct event_context *ev, struct timed_event *te,
268 struct timeval t, void *private_data)
270 struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
273 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL ||
274 (ctdb->monitor->monitoring_mode == CTDB_MONITORING_DISABLED && ctdb->done_startup)) {
275 event_add_timed(ctdb->ev, ctdb->monitor->monitor_context,
276 timeval_current_ofs(ctdb->monitor->next_interval, 0),
277 ctdb_check_health, ctdb);
281 if (!ctdb->done_startup) {
282 ret = ctdb_event_script_callback(ctdb,
283 ctdb->monitor->monitor_context, ctdb_startup_callback,
285 CTDB_EVENT_STARTUP, "%s", "");
288 int skip_monitoring = 0;
290 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
292 DEBUG(DEBUG_ERR,("Skip monitoring during recovery\n"));
294 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
295 if (ctdb->freeze_handles[i] != NULL) {
296 DEBUG(DEBUG_ERR,("Skip monitoring since databases are frozen\n"));
301 if (skip_monitoring != 0) {
302 event_add_timed(ctdb->ev, ctdb->monitor->monitor_context,
303 timeval_current_ofs(ctdb->monitor->next_interval, 0),
304 ctdb_check_health, ctdb);
307 ret = ctdb_event_script_callback(ctdb,
308 ctdb->monitor->monitor_context, ctdb_health_callback,
310 CTDB_EVENT_MONITOR, "%s", "");
315 DEBUG(DEBUG_ERR,("Unable to launch monitor event script\n"));
316 ctdb->monitor->next_interval = 5;
317 event_add_timed(ctdb->ev, ctdb->monitor->monitor_context,
318 timeval_current_ofs(5, 0),
319 ctdb_check_health, ctdb);
324 (Temporaily) Disabling monitoring will stop the monitor event scripts
325 from running but node health checks will still occur
327 void ctdb_disable_monitoring(struct ctdb_context *ctdb)
329 ctdb->monitor->monitoring_mode = CTDB_MONITORING_DISABLED;
330 DEBUG(DEBUG_INFO,("Monitoring has been disabled\n"));
334 Re-enable running monitor events after they have been disabled
336 void ctdb_enable_monitoring(struct ctdb_context *ctdb)
338 ctdb->monitor->monitoring_mode = CTDB_MONITORING_ACTIVE;
339 ctdb->monitor->next_interval = 5;
340 DEBUG(DEBUG_INFO,("Monitoring has been enabled\n"));
343 /* stop any monitoring
344 this should only be done when shutting down the daemon
346 void ctdb_stop_monitoring(struct ctdb_context *ctdb)
348 talloc_free(ctdb->monitor->monitor_context);
349 ctdb->monitor->monitor_context = NULL;
351 ctdb->monitor->monitoring_mode = CTDB_MONITORING_DISABLED;
352 ctdb->monitor->next_interval = 5;
353 DEBUG(DEBUG_NOTICE,("Monitoring has been stopped\n"));
357 start watching for nodes that might be dead
359 void ctdb_start_monitoring(struct ctdb_context *ctdb)
361 if (ctdb->monitor != NULL) {
365 ctdb->monitor = talloc(ctdb, struct ctdb_monitor_state);
366 CTDB_NO_MEMORY_FATAL(ctdb, ctdb->monitor);
368 ctdb->monitor->next_interval = 5;
370 ctdb->monitor->monitor_context = talloc_new(ctdb->monitor);
371 CTDB_NO_MEMORY_FATAL(ctdb, ctdb->monitor->monitor_context);
373 event_add_timed(ctdb->ev, ctdb->monitor->monitor_context,
374 timeval_current_ofs(1, 0),
375 ctdb_wait_until_recovered, ctdb);
377 ctdb->monitor->monitoring_mode = CTDB_MONITORING_ACTIVE;
378 DEBUG(DEBUG_NOTICE,("Monitoring has been started\n"));
383 modify flags on a node
385 int32_t ctdb_control_modflags(struct ctdb_context *ctdb, TDB_DATA indata)
387 struct ctdb_node_flag_change *c = (struct ctdb_node_flag_change *)indata.dptr;
388 struct ctdb_node *node;
392 if (c->pnn >= ctdb->num_nodes) {
393 DEBUG(DEBUG_ERR,(__location__ " Node %d is invalid, num_nodes :%d\n", c->pnn, ctdb->num_nodes));
397 node = ctdb->nodes[c->pnn];
398 old_flags = node->flags;
399 if (c->pnn != ctdb->pnn) {
400 c->old_flags = node->flags;
402 node->flags = c->new_flags & ~NODE_FLAGS_DISCONNECTED;
403 node->flags |= (c->old_flags & NODE_FLAGS_DISCONNECTED);
405 /* we dont let other nodes modify our STOPPED status */
406 if (c->pnn == ctdb->pnn) {
407 node->flags &= ~NODE_FLAGS_STOPPED;
408 if (old_flags & NODE_FLAGS_STOPPED) {
409 node->flags |= NODE_FLAGS_STOPPED;
413 /* we dont let other nodes modify our BANNED status */
414 if (c->pnn == ctdb->pnn) {
415 node->flags &= ~NODE_FLAGS_BANNED;
416 if (old_flags & NODE_FLAGS_BANNED) {
417 node->flags |= NODE_FLAGS_BANNED;
421 if (node->flags == c->old_flags) {
422 DEBUG(DEBUG_INFO, ("Control modflags on node %u - Unchanged - flags 0x%x\n", c->pnn, node->flags));
426 DEBUG(DEBUG_INFO, ("Control modflags on node %u - flags now 0x%x\n", c->pnn, node->flags));
429 /* tell the recovery daemon something has changed */
430 ctdb_daemon_send_message(ctdb, ctdb->pnn,
431 CTDB_SRVID_SET_NODE_FLAGS, indata);
433 /* if we have become banned, we should go into recovery mode */
434 if ((node->flags & NODE_FLAGS_BANNED) && !(c->old_flags & NODE_FLAGS_BANNED) && (node->pnn == ctdb->pnn)) {
435 /* make sure we are frozen */
436 DEBUG(DEBUG_NOTICE,("This node has been banned - forcing freeze and recovery\n"));
437 /* Reset the generation id to 1 to make us ignore any
438 REQ/REPLY CALL/DMASTER someone sends to us.
439 We are now banned so we shouldnt service database calls
442 ctdb->vnn_map->generation = INVALID_GENERATION;
444 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
445 if (ctdb_start_freeze(ctdb, i) != 0) {
446 DEBUG(DEBUG_ERR,(__location__ " Failed to freeze db priority %u\n", i));
449 ctdb_release_all_ips(ctdb);
450 ctdb->recovery_mode = CTDB_RECOVERY_ACTIVE;
457 return the monitoring mode
459 int32_t ctdb_monitoring_mode(struct ctdb_context *ctdb)
461 if (ctdb->monitor == NULL) {
462 return CTDB_MONITORING_DISABLED;
464 return ctdb->monitor->monitoring_mode;