TSG-23844 请packet adapter提供健康状态查询的HTTP服务

This commit is contained in:
luwenpeng
2024-11-20 14:41:30 +08:00
parent 1942f792e7
commit a51344bba3
7 changed files with 120 additions and 1 deletions

View File

@@ -2,10 +2,12 @@ add_library(common
src/log.cpp
src/packet_io.cpp
src/packet_parser.cpp
src/packet_inject.cpp)
src/packet_inject.cpp
src/http_healthcheck.cpp)
target_include_directories(common PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include)
target_link_libraries(common PUBLIC mrzcpd)
target_link_libraries(common PUBLIC MESA_prof_load)
target_link_libraries(common PUBLIC MESA_handle_logger)
target_link_libraries(common PUBLIC libevent-static)
add_subdirectory(test)

View File

@@ -0,0 +1,24 @@
#ifndef _HTTP_HEALTHCHECK_H
#define _HTTP_HEALTHCHECK_H
#ifdef __cplusplus
extern "C"
{
#endif
/*
* [http_healthcheck_server]
* enable=1
* port=8080
* addr="0.0.0.0"
* path="/status"
*/
void http_healthcheck_server_start(const char *profile);
void http_healthcheck_server_stop();
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,66 @@
#include <string.h>
#include <limits.h>
#include <pthread.h>
#include "event2/event.h"
#include "event2/buffer.h"
#include "event2/http.h"
#include "http_healthcheck.h"
#include <MESA/MESA_prof_load.h>
struct http_status_ctx
{
pthread_t pthread;
struct event_base *base;
unsigned int enable;
unsigned int port;
char addr[PATH_MAX];
char path[PATH_MAX];
uint64_t is_error;
} g_ctx = {0};
static void http_callback(struct evhttp_request *req, void *arg)
{
evhttp_send_reply(req, HTTP_OK, "", NULL);
}
static void *http_healthcheck_thread(void *arg)
{
printf("http_healthcheck_thread running (%s:%d/%s)\n", g_ctx.addr, g_ctx.port, g_ctx.path);
g_ctx.base = event_base_new();
struct evhttp *http = evhttp_new(g_ctx.base);
evhttp_set_cb(http, g_ctx.path, http_callback, NULL);
evhttp_bind_socket(http, g_ctx.addr, g_ctx.port);
event_base_dispatch(g_ctx.base);
evhttp_free(http);
event_base_free(g_ctx.base);
printf("http_healthcheck_thread exit\n");
return NULL;
}
void http_healthcheck_server_start(const char *profile)
{
MESA_load_profile_uint_def(profile, "http_healthcheck_server", "enable", &g_ctx.enable, 0);
MESA_load_profile_uint_def(profile, "http_healthcheck_server", "port", &g_ctx.port, 8080);
MESA_load_profile_string_def(profile, "http_healthcheck_server", "addr", g_ctx.addr, sizeof(g_ctx.addr), "127.0.0.1");
MESA_load_profile_string_def(profile, "http_healthcheck_server", "path", g_ctx.path, sizeof(g_ctx.path), "/status");
if (!g_ctx.enable)
{
return;
}
pthread_create(&g_ctx.pthread, NULL, http_healthcheck_thread, (void *)&g_ctx);
}
void http_healthcheck_server_stop()
{
if (g_ctx.enable)
{
event_base_loopbreak(g_ctx.base);
pthread_join(g_ctx.pthread, NULL);
}
}

View File

@@ -9,3 +9,9 @@ app_device=eth_nf_packet_adapter
[stat]
output_file=log/packet_adapter.fs2
statsd_cycle=2
[http_healthcheck_server]
enable=1
port=8080
addr="0.0.0.0"
path="/status"

View File

@@ -10,6 +10,7 @@
#include "packet_io.h"
#include "packet_stat.h"
#include "packet_handle.h"
#include "http_healthcheck.h"
#define LOG_MAIN "PacketAdapter"
@@ -225,6 +226,8 @@ int main(int argc, char **argv)
}
}
http_healthcheck_server_start(profile);
while (!runtime->need_stop)
{
packet_stat_flush(runtime->stat, &runtime->metrics);
@@ -232,6 +235,7 @@ int main(int argc, char **argv)
}
error:
http_healthcheck_server_stop();
packet_stat_destory(runtime->stat);
packet_io_destory(runtime->handle);
LOG_CLOSE();

17
vendor/CMakeLists.txt vendored
View File

@@ -20,6 +20,23 @@ add_dependencies(gmock googletest)
set_property(TARGET gmock PROPERTY IMPORTED_LOCATION ${INSTALL_DIR}/lib/libgmock.a)
set_property(TARGET gmock PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${INSTALL_DIR}/include)
# Libevent 2.1.8
ExternalProject_Add(libevent PREFIX libevent
URL ${CMAKE_CURRENT_SOURCE_DIR}/libevent-2.1.8-stable.tar.gz
URL_MD5 f3eeaed018542963b7d2416ef1135ecc
CONFIGURE_COMMAND ./configure --prefix=<INSTALL_DIR> --disable-shared --disable-samples
BUILD_COMMAND make LDFLAGS="-ldl"
BUILD_IN_SOURCE 1s)
ExternalProject_Get_Property(libevent INSTALL_DIR)
file(MAKE_DIRECTORY ${INSTALL_DIR}/include)
add_library(libevent-static STATIC IMPORTED GLOBAL)
add_dependencies(libevent-static libevent)
set_property(TARGET libevent-static PROPERTY IMPORTED_LOCATION ${INSTALL_DIR}/lib/libevent.a)
set_property(TARGET libevent-static PROPERTY IMPORTED_INTERFACE_LINK_LIBRARIES pthread)
set_property(TARGET libevent-static PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${INSTALL_DIR}/include)
# MESA Framework
set(MESA_FRAMEWORK_LIB_DIR /opt/MESA/lib)
set(MESA_FRAMEWORK_INCLUDE_DIR /opt/MESA/include)

BIN
vendor/libevent-2.1.8-stable.tar.gz vendored Normal file

Binary file not shown.