Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions modules-contrib/http_health/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Build through the top-level Makefile.
include ../../Makefile.defs
auto_gen=
NAME=http_health.so

include ../../Makefile.modules
41 changes: 41 additions & 0 deletions modules-contrib/http_health/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# HTTP health probes

The `http_health` module adds two GET endpoints to the `httpd` listener:

| Endpoint | Response |
| --- | --- |
| `/healthz` | `200 ok` while the HTTP handler can respond, including during draining. |
| `/readyz` | `200 ready` when the core is running and the configured readiness group has a positive status; otherwise `503 not ready`. |

Use `/healthz` for liveness and `/readyz` for readiness. Liveness does not check
SIP worker responsiveness. Neither endpoint changes SIP routing or OPTIONS replies.

Only GET requests to the exact paths are supported. A trailing slash or other
path suffix returns `404`; other methods are rejected.

## Configuration

Load `httpd` and `http_health` to enable the endpoints. They share the listener
configured by `httpd`.

```opensips
loadmodule "httpd.so"
modparam("httpd", "ip", "127.0.0.1")
modparam("httpd", "port", 8888)

loadmodule "http_health.so"
modparam("http_health", "readiness_group", "drain")
```

`readiness_group` names a Status/Report group and checks its default identifier.
It defaults to an empty string, which checks only the core status. A configured
group that is missing or has a non-positive status makes `/readyz` return `503`.

To use `drain` as shown above, also load and configure the `drain` module and its
dependencies. Enabling drain makes `/readyz` return `503`; disabling drain restores
`200` once the core is running. `/healthz` continues to return `200` in both cases.

Keep the HTTP listener accessible only to trusted health checkers, particularly
if other loaded modules expose management operations on the same listener.

If `httpd` authentication is enabled, health checkers must send the credentials.
146 changes: 146 additions & 0 deletions modules-contrib/http_health/http_health.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
* HTTP health and readiness probes for OpenSIPS
*
* Copyright (C) 2026 OpenSIPS Project
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* opensips is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <string.h>

#include "../../sr_module.h"
#include "../../status_report.h"
#include "../../modules/httpd/httpd_load.h"

static int mod_init(void);
static str health_root = str_init("healthz");
static str ready_root = str_init("readyz");
static str readiness_group = str_init("");

static const param_export_t params[] = {
{"readiness_group", STR_PARAM, &readiness_group.s},
{0, 0, 0}
};

static const dep_export_t deps = {
{
{MOD_TYPE_DEFAULT, "httpd", DEP_ABORT},
{MOD_TYPE_NULL, NULL, 0},
},
{
{NULL, NULL},
},
};

struct module_exports exports = {
"http_health",
MOD_TYPE_DEFAULT,
MODULE_VERSION,
DEFAULT_DLFLAGS,
0, /* load function */
&deps,
NULL, /* exported functions */
NULL, /* exported async functions */
params,
NULL, /* exported statistics */
NULL, /* exported MI functions */
NULL, /* exported PV */
NULL, /* exported transformations */
0, /* extra processes */
NULL, /* pre-initialization */
mod_init,
NULL, /* response handling */
NULL, /* destroy */
NULL, /* per-child initialization */
NULL /* reload confirmation */
};

static int probe_response(const char *url, const char *method, str *page,
int readiness)
{
void *group;

if (*url != '\0') {
*page = str_init("not found\n");
return 404;
}
if (strcmp(method, "GET") != 0) {
*page = str_init("method not allowed\n");
return 405;
}

if (readiness) {
if (sr_get_core_status() != STATE_RUNNING)
goto not_ready;

if (readiness_group.len) {
group = sr_get_group_by_name(readiness_group.s,
readiness_group.len);
if (!group || w_sr_check_status(NULL, group, NULL) <= 0)
goto not_ready;
}
*page = str_init("ready\n");
} else {
*page = str_init("ok\n");
}
return 200;

not_ready:
*page = str_init("not ready\n");
return 503;
}

static int health_response(void *cls, void *connection,
const char *url, const char *method, const char *version,
const char *upload_data, size_t upload_data_size, void **con_cls,
str *buffer, str *page, union sockaddr_union *cl_socket)
{
return probe_response(url, method, page, 0);
}

static int ready_response(void *cls, void *connection,
const char *url, const char *method, const char *version,
const char *upload_data, size_t upload_data_size, void **con_cls,
str *buffer, str *page, union sockaddr_union *cl_socket)
{
return probe_response(url, method, page, 1);
}

static ssize_t flush_response(void *cls, uint64_t pos, char *buf, size_t max)
{
return -1;
}

static int mod_init(void)
{
httpd_api_t httpd;

readiness_group.len = strlen(readiness_group.s);
if (load_httpd_api(&httpd) < 0) {
LM_ERR("cannot load httpd API\n");
return -1;
}

if (httpd.register_httpdcb(exports.name, &health_root,
health_response, flush_response, HTTPD_TEXT_PLAIN_TYPE, NULL) < 0
|| httpd.register_httpdcb(exports.name, &ready_root,
ready_response, flush_response, HTTPD_TEXT_PLAIN_TYPE, NULL) < 0) {
LM_ERR("cannot register HTTP probes\n");
return -1;
}
return 0;
}