diff options
Diffstat (limited to 'switchctl.c')
-rw-r--r-- | switchctl.c | 121 |
1 files changed, 47 insertions, 74 deletions
diff --git a/switchctl.c b/switchctl.c index 7ccfabd..f4a8b0e 100644 --- a/switchctl.c +++ b/switchctl.c @@ -358,6 +358,48 @@ int process_cmd_channel(const char* cmd, const char* param, int fd, cmd_t **cmd_ return 0; } +int send_health_status(int fd, client_t* client_lst) +{ + bool mc, sc, hmc, hsc; + mc = sc = hmc = hsc = 0; + + client_t* client; + for(client = client_lst; client; client = client->next) { + switch(client->type) { + case MASTER: mc=1; break; + case STANDBY: sc=1; break; + case HB_MASTER: hmc=1; break; + case HB_STANDBY: hsc=1; break; + default: break; + } + } + + char buf[4][50]; + snprintf(buf[0], 50, "Master: %s", (mc) ? "connected" : "offline"); + snprintf(buf[1], 50, "Standby: %s", (sc) ? "connected" : "offline"); + snprintf(buf[2], 50, "Hearbeat Master: %s,%s", (hmc) ? "connected" : "offline", (state_.hb_state_master_) ? "present" : "offline"); + snprintf(buf[3], 50, "Hearbeat Standby: %s,%s", (hsc) ? "connected" : "offline", (state_.hb_state_standby_) ? "present" : "offline"); + + if(fd >= 0) { + send_response(fd, buf[0]); + send_response(fd, buf[1]); + send_response(fd, buf[2]); + send_response(fd, buf[3]); + } + + int listener_cnt = 0; + for(client = client_lst; client; client = client->next) + if(client->health_listener && client->fd != fd) { + send_response(client->fd, buf[0]); + send_response(client->fd, buf[1]); + send_response(client->fd, buf[2]); + send_response(client->fd, buf[3]); + listener_cnt++; + } + log_printf(DEBUG, "sent health info to %d additional listeners", listener_cnt); + return 0; +} + void process_cmd_type(const char* param, int fd, client_t* client_lst) { if(param) { @@ -382,6 +424,7 @@ void process_cmd_type(const char* param, int fd, client_t* client_lst) return; } log_printf(DEBUG, "client %d type set to %s", fd, param); + send_health_status(-1, client_lst); } else { log_printf(ERROR, "unable to set client type for %d: type already set to %s", fd, client_type_tostring(client->type)); @@ -490,28 +533,13 @@ int process_cmd_heartbeat(const char* param, int fd, cmd_t **cmd_q, client_t* cl if(param) { client_t* client = client_find(client_lst, fd); if(client) { - char* message = NULL; switch(client->type) { case HB_MASTER: { - bool old_state = state_.hb_state_master_; state_.hb_state_master_ = (param[0] == '1') ? TRUE : FALSE; - if(old_state != state_.hb_state_master_) { - if(state_.hb_state_master_) - message = "Health: master heartbeat is back!"; - else - message = "Health: master heartbeat timed out!"; - } break; } case HB_STANDBY: { - bool old_state = state_.hb_state_standby_; state_.hb_state_standby_ = (param[0] == '1') ? TRUE : FALSE; - if(old_state != state_.hb_state_standby_) { - if(state_.hb_state_standby_) - message = "Health: standby heartbeat is back!"; - else - message = "Health: standby heartbeat timed out!"; - } break; } default: { @@ -520,34 +548,22 @@ int process_cmd_heartbeat(const char* param, int fd, cmd_t **cmd_q, client_t* cl break; } } + send_health_status(-1, client_lst); switchctl_mode_t old_mode = state_.mode_; if(state_.mode_ == MODE_MASTER) { if(!state_.hb_state_master_) { if(state_.hb_state_standby_) { state_.mode_ = MODE_STANDBY; return change_mode(old_mode, fd, cmd_q, opt, client_lst); - } else - message = "Health: both heartbeats are offline!"; + } } } else { if(!state_.hb_state_standby_) { if(state_.hb_state_master_) { state_.mode_ = MODE_MASTER; return change_mode(old_mode, fd, cmd_q, opt, client_lst); - } else - message = "Health: both heartbeats are offline!"; - } - } - if(message) { - log_printf(ERROR, message); - client_t* client; - int listener_cnt = 0; - for(client = client_lst; client; client = client->next) - if(client->health_listener && client->fd != fd) { - send_response(client->fd, message); - listener_cnt++; } - log_printf(DEBUG, "sent health info to %d additional listeners", listener_cnt); + } } } else { @@ -562,51 +578,8 @@ int process_cmd_heartbeat(const char* param, int fd, cmd_t **cmd_q, client_t* cl return 0; } -int send_health_status(int fd, client_t* client_lst) -{ - bool mc, sc, hmc, hsc; - mc = sc = hmc = hsc = 0; - - client_t* client; - for(client = client_lst; client; client = client->next) { - switch(client->type) { - case MASTER: mc=1; break; - case STANDBY: sc=1; break; - case HB_MASTER: hmc=1; break; - case HB_STANDBY: hsc=1; break; - default: break; - } - } - - char buf[4][50]; - snprintf(buf[0], 50, "Master: %s", (mc) ? "connected" : "offline"); - snprintf(buf[1], 50, "Standby: %s", (sc) ? "connected" : "offline"); - snprintf(buf[2], 50, "Hearbeat Master: %s,%s", (hmc) ? "connected" : "offline", (state_.hb_state_master_) ? "present" : "offline"); - snprintf(buf[3], 50, "Hearbeat Standby: %s,%s", (hsc) ? "connected" : "offline", (state_.hb_state_standby_) ? "present" : "offline"); - - if(fd >= 0) { - send_response(fd, buf[0]); - send_response(fd, buf[1]); - send_response(fd, buf[2]); - send_response(fd, buf[3]); - } - - int listener_cnt = 0; - for(client = client_lst; client; client = client->next) - if(client->health_listener && client->fd != fd) { - send_response(client->fd, buf[0]); - send_response(client->fd, buf[1]); - send_response(client->fd, buf[2]); - send_response(client->fd, buf[3]); - listener_cnt++; - } - log_printf(DEBUG, "sent health info to %d additional listeners", listener_cnt); - return 0; -} - int process_cmd_health(const char* param, int fd, cmd_t **cmd_q, client_t* client_lst, options_t* opt) { - send_response(fd, "EEE: health not yet implemented!"); return send_health_status(fd, client_lst); } @@ -979,11 +952,11 @@ int main_loop(int switch_fd, int cmd_listen_fd, options_t* opt) return_value = nonblock_recvline(&(lst->buffer), lst->fd, &cmd_q, client_lst, opt); if(return_value == 2) { log_printf(DEBUG, "removing closed command connection (fd=%d)", lst->fd); - send_health_status(-1, client_lst); client_t* deletee = lst; lst = lst->next; FD_CLR(deletee->fd, &readfds); client_remove(&client_lst, deletee->fd); + send_health_status(-1, client_lst); return_value = 0; continue; } |