|
Packit |
90a5c9 |
/* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
Packit |
90a5c9 |
* contributor license agreements. See the NOTICE file distributed with
|
|
Packit |
90a5c9 |
* this work for additional information regarding copyright ownership.
|
|
Packit |
90a5c9 |
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
Packit |
90a5c9 |
* (the "License"); you may not use this file except in compliance with
|
|
Packit |
90a5c9 |
* the License. You may obtain a copy of the License at
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* Unless required by applicable law or agreed to in writing, software
|
|
Packit |
90a5c9 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
Packit |
90a5c9 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
Packit |
90a5c9 |
* See the License for the specific language governing permissions and
|
|
Packit |
90a5c9 |
* limitations under the License.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#include "httpd.h"
|
|
Packit |
90a5c9 |
#include "http_config.h"
|
|
Packit |
90a5c9 |
#include "http_log.h"
|
|
Packit |
90a5c9 |
#include "apr_strings.h"
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#include "ap_mpm.h"
|
|
Packit |
90a5c9 |
#include "scoreboard.h"
|
|
Packit |
90a5c9 |
#include "mod_watchdog.h"
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#ifndef HEARTBEAT_INTERVAL
|
|
Packit |
90a5c9 |
#define HEARTBEAT_INTERVAL (1)
|
|
Packit |
90a5c9 |
#endif
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
module AP_MODULE_DECLARE_DATA heartbeat_module;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
typedef struct hb_ctx_t
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
int active;
|
|
Packit |
90a5c9 |
apr_sockaddr_t *mcast_addr;
|
|
Packit |
90a5c9 |
int server_limit;
|
|
Packit |
90a5c9 |
int thread_limit;
|
|
Packit |
90a5c9 |
apr_status_t status;
|
|
Packit |
90a5c9 |
} hb_ctx_t;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const char *msg_format = "v=%u&ready=%u&busy=%u";
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#define MSG_VERSION (1)
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int hb_monitor(hb_ctx_t *ctx, apr_pool_t *p)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
apr_size_t len;
|
|
Packit |
90a5c9 |
apr_socket_t *sock = NULL;
|
|
Packit |
90a5c9 |
char buf[256];
|
|
Packit |
90a5c9 |
int i, j;
|
|
Packit |
90a5c9 |
apr_uint32_t ready = 0;
|
|
Packit |
90a5c9 |
apr_uint32_t busy = 0;
|
|
Packit |
90a5c9 |
ap_generation_t mpm_generation;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
ap_mpm_query(AP_MPMQ_GENERATION, &mpm_generation);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
for (i = 0; i < ctx->server_limit; i++) {
|
|
Packit |
90a5c9 |
process_score *ps;
|
|
Packit |
90a5c9 |
ps = ap_get_scoreboard_process(i);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
for (j = 0; j < ctx->thread_limit; j++) {
|
|
Packit |
90a5c9 |
int res;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
worker_score *ws = NULL;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
ws = &ap_scoreboard_image->servers[i][j];
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
res = ws->status;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (res == SERVER_READY && ps->generation == mpm_generation) {
|
|
Packit |
90a5c9 |
ready++;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else if (res != SERVER_DEAD &&
|
|
Packit |
90a5c9 |
res != SERVER_STARTING && res != SERVER_IDLE_KILL &&
|
|
Packit |
90a5c9 |
ps->generation == mpm_generation) {
|
|
Packit |
90a5c9 |
busy++;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
len = apr_snprintf(buf, sizeof(buf), msg_format, MSG_VERSION, ready, busy);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
do {
|
|
Packit |
90a5c9 |
apr_status_t rv;
|
|
Packit |
90a5c9 |
rv = apr_socket_create(&sock, ctx->mcast_addr->family,
|
|
Packit |
90a5c9 |
SOCK_DGRAM, APR_PROTO_UDP, p);
|
|
Packit |
90a5c9 |
if (rv) {
|
|
Packit |
90a5c9 |
ap_log_error(APLOG_MARK, APLOG_WARNING, rv,
|
|
Packit |
90a5c9 |
NULL, APLOGNO(02097) "Heartbeat: apr_socket_create failed");
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
rv = apr_mcast_loopback(sock, 1);
|
|
Packit |
90a5c9 |
if (rv) {
|
|
Packit |
90a5c9 |
ap_log_error(APLOG_MARK, APLOG_WARNING, rv,
|
|
Packit |
90a5c9 |
NULL, APLOGNO(02098) "Heartbeat: apr_mcast_loopback failed");
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
rv = apr_socket_sendto(sock, ctx->mcast_addr, 0, buf, &len;;
|
|
Packit |
90a5c9 |
if (rv) {
|
|
Packit |
90a5c9 |
ap_log_error(APLOG_MARK, APLOG_WARNING, rv,
|
|
Packit |
90a5c9 |
NULL, APLOGNO(02099) "Heartbeat: apr_socket_sendto failed");
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
} while (0);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (sock) {
|
|
Packit |
90a5c9 |
apr_socket_close(sock);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return OK;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int hb_watchdog_init(server_rec *s, const char *name, apr_pool_t *pool)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
hb_ctx_t *ctx = ap_get_module_config(s->module_config, &heartbeat_module);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
ap_mpm_query(AP_MPMQ_HARD_LIMIT_THREADS, &ctx->thread_limit);
|
|
Packit |
90a5c9 |
ap_mpm_query(AP_MPMQ_HARD_LIMIT_DAEMONS, &ctx->server_limit);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return OK;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int hb_watchdog_exit(server_rec *s, const char *name, apr_pool_t *pool)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
return OK;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int hb_watchdog_step(server_rec *s, const char *name, apr_pool_t *pool)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
hb_ctx_t *ctx = ap_get_module_config(s->module_config, &heartbeat_module);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!ctx->active || strcmp(name, AP_WATCHDOG_SINGLETON)) {
|
|
Packit |
90a5c9 |
return OK;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
return hb_monitor(ctx, pool);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int hb_watchdog_need(server_rec *s, const char *name,
|
|
Packit |
90a5c9 |
int parent, int singleton)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
hb_ctx_t *ctx = ap_get_module_config(s->module_config, &heartbeat_module);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (ctx->active && singleton && !strcmp(name, AP_WATCHDOG_SINGLETON))
|
|
Packit |
90a5c9 |
return OK;
|
|
Packit |
90a5c9 |
else
|
|
Packit |
90a5c9 |
return DECLINED;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void hb_register_hooks(apr_pool_t *p)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
ap_hook_watchdog_need(hb_watchdog_need, NULL, NULL, APR_HOOK_MIDDLE);
|
|
Packit |
90a5c9 |
ap_hook_watchdog_init(hb_watchdog_init, NULL, NULL, APR_HOOK_MIDDLE);
|
|
Packit |
90a5c9 |
ap_hook_watchdog_step(hb_watchdog_step, NULL, NULL, APR_HOOK_MIDDLE);
|
|
Packit |
90a5c9 |
ap_hook_watchdog_exit(hb_watchdog_exit, NULL, NULL, APR_HOOK_MIDDLE);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void *hb_create_config(apr_pool_t *p, server_rec *s)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
hb_ctx_t *cfg = (hb_ctx_t *) apr_pcalloc(p, sizeof(hb_ctx_t));
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return cfg;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const char *cmd_hb_address(cmd_parms *cmd,
|
|
Packit |
90a5c9 |
void *dconf, const char *addr)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
apr_status_t rv;
|
|
Packit |
90a5c9 |
char *host_str;
|
|
Packit |
90a5c9 |
char *scope_id;
|
|
Packit |
90a5c9 |
apr_port_t port = 0;
|
|
Packit |
90a5c9 |
apr_pool_t *p = cmd->pool;
|
|
Packit |
90a5c9 |
hb_ctx_t *ctx =
|
|
Packit |
90a5c9 |
(hb_ctx_t *) ap_get_module_config(cmd->server->module_config,
|
|
Packit |
90a5c9 |
&heartbeat_module);
|
|
Packit |
90a5c9 |
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (err != NULL) {
|
|
Packit |
90a5c9 |
return err;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!ctx->active) {
|
|
Packit |
90a5c9 |
ctx->active = 1;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
return "HeartbeatAddress: May only be specified once.";
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
rv = apr_parse_addr_port(&host_str, &scope_id, &port, addr, cmd->temp_pool);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (rv) {
|
|
Packit |
90a5c9 |
return "HeartbeatAddress: Unable to parse address.";
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (host_str == NULL) {
|
|
Packit |
90a5c9 |
return "HeartbeatAddress: No host provided in address";
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (port == 0) {
|
|
Packit |
90a5c9 |
return "HeartbeatAddress: No port provided in address";
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
rv = apr_sockaddr_info_get(&ctx->mcast_addr, host_str, APR_INET, port, 0,
|
|
Packit |
90a5c9 |
p);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (rv) {
|
|
Packit |
90a5c9 |
return "HeartbeatAddress: apr_sockaddr_info_get failed.";
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return NULL;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const command_rec hb_cmds[] = {
|
|
Packit |
90a5c9 |
AP_INIT_TAKE1("HeartbeatAddress", cmd_hb_address, NULL, RSRC_CONF,
|
|
Packit |
90a5c9 |
"Address to send heartbeat requests"),
|
|
Packit |
90a5c9 |
{NULL}
|
|
Packit |
90a5c9 |
};
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
AP_DECLARE_MODULE(heartbeat) = {
|
|
Packit |
90a5c9 |
STANDARD20_MODULE_STUFF,
|
|
Packit |
90a5c9 |
NULL, /* create per-directory config structure */
|
|
Packit |
90a5c9 |
NULL, /* merge per-directory config structures */
|
|
Packit |
90a5c9 |
hb_create_config, /* create per-server config structure */
|
|
Packit |
90a5c9 |
NULL, /* merge per-server config structures */
|
|
Packit |
90a5c9 |
hb_cmds, /* command apr_table_t */
|
|
Packit |
90a5c9 |
hb_register_hooks
|
|
Packit |
90a5c9 |
};
|