1/* Licensed to the Apache Software Foundation (ASF) under one or more 2 * contributor license agreements. See the NOTICE file distributed with 3 * this work for additional information regarding copyright ownership. 4 * The ASF licenses this file to You under the Apache License, Version 2.0 5 * (the "License"); you may not use this file except in compliance with 6 * the License. You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include "httpd.h" 18#include "http_config.h" 19#include "http_log.h" 20#include "apr_strings.h" 21 22#include "ap_mpm.h" 23#include "scoreboard.h" 24#include "mod_watchdog.h" 25 26#ifndef HEARTBEAT_INTERVAL 27#define HEARTBEAT_INTERVAL (1) 28#endif 29 30module AP_MODULE_DECLARE_DATA heartbeat_module; 31 32typedef struct hb_ctx_t 33{ 34 int active; 35 apr_sockaddr_t *mcast_addr; 36 int server_limit; 37 int thread_limit; 38 apr_status_t status; 39} hb_ctx_t; 40 41static const char *msg_format = "v=%u&ready=%u&busy=%u"; 42 43#define MSG_VERSION (1) 44 45static int hb_monitor(hb_ctx_t *ctx, apr_pool_t *p) 46{ 47 apr_size_t len; 48 apr_socket_t *sock = NULL; 49 char buf[256]; 50 int i, j; 51 apr_uint32_t ready = 0; 52 apr_uint32_t busy = 0; 53 ap_generation_t mpm_generation; 54 55 ap_mpm_query(AP_MPMQ_GENERATION, &mpm_generation); 56 57 for (i = 0; i < ctx->server_limit; i++) { 58 process_score *ps; 59 ps = ap_get_scoreboard_process(i); 60 61 for (j = 0; j < ctx->thread_limit; j++) { 62 int res; 63 64 worker_score *ws = NULL; 65 66 ws = &ap_scoreboard_image->servers[i][j]; 67 68 res = ws->status; 69 70 if (res == SERVER_READY && ps->generation == mpm_generation) { 71 ready++; 72 } 73 else if (res != SERVER_DEAD && 74 res != SERVER_STARTING && res != SERVER_IDLE_KILL && 75 ps->generation == mpm_generation) { 76 busy++; 77 } 78 } 79 } 80 81 len = apr_snprintf(buf, sizeof(buf), msg_format, MSG_VERSION, ready, busy); 82 83 do { 84 apr_status_t rv; 85 rv = apr_socket_create(&sock, ctx->mcast_addr->family, 86 SOCK_DGRAM, APR_PROTO_UDP, p); 87 if (rv) { 88 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, 89 NULL, APLOGNO(02097) "Heartbeat: apr_socket_create failed"); 90 break; 91 } 92 93 rv = apr_mcast_loopback(sock, 1); 94 if (rv) { 95 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, 96 NULL, APLOGNO(02098) "Heartbeat: apr_mcast_loopback failed"); 97 break; 98 } 99 100 rv = apr_socket_sendto(sock, ctx->mcast_addr, 0, buf, &len); 101 if (rv) { 102 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, 103 NULL, APLOGNO(02099) "Heartbeat: apr_socket_sendto failed"); 104 break; 105 } 106 } while (0); 107 108 if (sock) { 109 apr_socket_close(sock); 110 } 111 112 return OK; 113} 114 115static int hb_watchdog_init(server_rec *s, const char *name, apr_pool_t *pool) 116{ 117 hb_ctx_t *ctx = ap_get_module_config(s->module_config, &heartbeat_module); 118 119 ap_mpm_query(AP_MPMQ_HARD_LIMIT_THREADS, &ctx->thread_limit); 120 ap_mpm_query(AP_MPMQ_HARD_LIMIT_DAEMONS, &ctx->server_limit); 121 122 return OK; 123} 124 125static int hb_watchdog_exit(server_rec *s, const char *name, apr_pool_t *pool) 126{ 127 return OK; 128} 129 130static int hb_watchdog_step(server_rec *s, const char *name, apr_pool_t *pool) 131{ 132 hb_ctx_t *ctx = ap_get_module_config(s->module_config, &heartbeat_module); 133 134 if (!ctx->active || strcmp(name, AP_WATCHDOG_SINGLETON)) { 135 return OK; 136 } 137 return hb_monitor(ctx, pool); 138} 139 140static int hb_watchdog_need(server_rec *s, const char *name, 141 int parent, int singleton) 142{ 143 hb_ctx_t *ctx = ap_get_module_config(s->module_config, &heartbeat_module); 144 145 if (ctx->active && singleton && !strcmp(name, AP_WATCHDOG_SINGLETON)) 146 return OK; 147 else 148 return DECLINED; 149} 150 151static void hb_register_hooks(apr_pool_t *p) 152{ 153 ap_hook_watchdog_need(hb_watchdog_need, NULL, NULL, APR_HOOK_MIDDLE); 154 ap_hook_watchdog_init(hb_watchdog_init, NULL, NULL, APR_HOOK_MIDDLE); 155 ap_hook_watchdog_step(hb_watchdog_step, NULL, NULL, APR_HOOK_MIDDLE); 156 ap_hook_watchdog_exit(hb_watchdog_exit, NULL, NULL, APR_HOOK_MIDDLE); 157} 158 159static void *hb_create_config(apr_pool_t *p, server_rec *s) 160{ 161 hb_ctx_t *cfg = (hb_ctx_t *) apr_pcalloc(p, sizeof(hb_ctx_t)); 162 163 return cfg; 164} 165 166static const char *cmd_hb_address(cmd_parms *cmd, 167 void *dconf, const char *addr) 168{ 169 apr_status_t rv; 170 char *host_str; 171 char *scope_id; 172 apr_port_t port = 0; 173 apr_pool_t *p = cmd->pool; 174 hb_ctx_t *ctx = 175 (hb_ctx_t *) ap_get_module_config(cmd->server->module_config, 176 &heartbeat_module); 177 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY); 178 179 if (err != NULL) { 180 return err; 181 } 182 183 if (!ctx->active) { 184 ctx->active = 1; 185 } 186 else { 187 return "HeartbeatAddress: May only be specified once."; 188 } 189 190 rv = apr_parse_addr_port(&host_str, &scope_id, &port, addr, cmd->temp_pool); 191 192 if (rv) { 193 return "HeartbeatAddress: Unable to parse address."; 194 } 195 196 if (host_str == NULL) { 197 return "HeartbeatAddress: No host provided in address"; 198 } 199 200 if (port == 0) { 201 return "HeartbeatAddress: No port provided in address"; 202 } 203 204 rv = apr_sockaddr_info_get(&ctx->mcast_addr, host_str, APR_INET, port, 0, 205 p); 206 207 if (rv) { 208 return "HeartbeatAddress: apr_sockaddr_info_get failed."; 209 } 210 211 return NULL; 212} 213 214static const command_rec hb_cmds[] = { 215 AP_INIT_TAKE1("HeartbeatAddress", cmd_hb_address, NULL, RSRC_CONF, 216 "Address to send heartbeat requests"), 217 {NULL} 218}; 219 220AP_DECLARE_MODULE(heartbeat) = { 221 STANDARD20_MODULE_STUFF, 222 NULL, /* create per-directory config structure */ 223 NULL, /* merge per-directory config structures */ 224 hb_create_config, /* create per-server config structure */ 225 NULL, /* merge per-server config structures */ 226 hb_cmds, /* command apr_table_t */ 227 hb_register_hooks 228}; 229