diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index d5b0902..443f795 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -1,6 +1,6 @@ -add_library(common src/session_table.cpp src/packet.cpp src/control_packet.cpp src/bfd.cpp src/utils.cpp src/vxlan.cpp src/log.cpp src/timestamp.cpp src/mpack.cpp src/kafka.cpp) +add_library(common src/session_table.cpp src/packet.cpp src/control_packet.cpp src/bfd.cpp src/utils.cpp src/vxlan.cpp src/log.cpp src/timestamp.cpp src/mpack.cpp src/kafka.cpp src/http_healthcheck.cpp) target_link_libraries(common PUBLIC cjson) -target_link_libraries(common PUBLIC MESA_handle_logger rdkafka uuid) +target_link_libraries(common PUBLIC MESA_handle_logger MESA_prof_load rdkafka uuid libevent-static) target_include_directories(common PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include) diff --git a/common/include/http_healthcheck.h b/common/include/http_healthcheck.h new file mode 100644 index 0000000..a883984 --- /dev/null +++ b/common/include/http_healthcheck.h @@ -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 diff --git a/common/src/http_healthcheck.cpp b/common/src/http_healthcheck.cpp new file mode 100644 index 0000000..30d1fd5 --- /dev/null +++ b/common/src/http_healthcheck.cpp @@ -0,0 +1,66 @@ +#include +#include +#include + +#include "event2/event.h" +#include "event2/buffer.h" +#include "event2/http.h" +#include "http_healthcheck.h" +#include + +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); + } +} diff --git a/common/test/CMakeLists.txt b/common/test/CMakeLists.txt index f31c21e..b06a0fa 100644 --- a/common/test/CMakeLists.txt +++ b/common/test/CMakeLists.txt @@ -46,6 +46,14 @@ add_executable(gtest_health_check_table gtest_health_check_table.cpp) target_include_directories(gtest_health_check_table PUBLIC ${CMAKE_SOURCE_DIR}/common/include ${CMAKE_SOURCE_DIR}/platform/include) target_link_libraries(gtest_health_check_table common gtest platform) +############################################################################### +# gtest_http_healthcheck +############################################################################### + +add_executable(gtest_http_healthcheck gtest_http_healthcheck.cpp) +target_include_directories(gtest_http_healthcheck PUBLIC ${CMAKE_SOURCE_DIR}/common/include ${CMAKE_SOURCE_DIR}/platform/include) +target_link_libraries(gtest_http_healthcheck common gtest platform) + ############################################################################### # gtest_discover_tests ############################################################################### @@ -57,3 +65,5 @@ gtest_discover_tests(gtest_packet) gtest_discover_tests(gtest_utils) gtest_discover_tests(gtest_vxlan) gtest_discover_tests(gtest_health_check_table) + +file(COPY ./conf/ DESTINATION ./conf/) \ No newline at end of file diff --git a/common/test/conf/http_healthcheck.conf b/common/test/conf/http_healthcheck.conf new file mode 100644 index 0000000..ac2f87f --- /dev/null +++ b/common/test/conf/http_healthcheck.conf @@ -0,0 +1,5 @@ +[http_healthcheck_server] +enable=1 +port=8080 +addr="0.0.0.0" +path="/status" diff --git a/common/test/gtest_http_healthcheck.cpp b/common/test/gtest_http_healthcheck.cpp new file mode 100644 index 0000000..48d5f52 --- /dev/null +++ b/common/test/gtest_http_healthcheck.cpp @@ -0,0 +1,15 @@ +#include +#include + +#include "http_healthcheck.h" + +int main(int argc, char **argv) +{ + http_healthcheck_server_start("./conf/http_healthcheck.conf"); + + sleep(30); + + http_healthcheck_server_stop(); + + return 0; +} \ No newline at end of file diff --git a/conf/sce.conf b/conf/sce.conf index 6b21efe..b90789f 100644 --- a/conf/sce.conf +++ b/conf/sce.conf @@ -83,4 +83,10 @@ enable_debug=0 brokerlist=192.168.40.224:9092 sasl_username=admin sasl_passwd=galaxy2019 -topic_name=POLICY-RULE-METRIC \ No newline at end of file +topic_name=POLICY-RULE-METRIC + +[http_healthcheck_server] +enable=1 +port=8080 +addr="0.0.0.0" +path="/status" diff --git a/platform/src/main.cpp b/platform/src/main.cpp index f6c2241..d87cc78 100644 --- a/platform/src/main.cpp +++ b/platform/src/main.cpp @@ -10,6 +10,7 @@ #include "utils.h" #include "sf_metrics.h" #include "global_metrics.h" +#include "http_healthcheck.h" struct breakpad_instance *g_breakpad = NULL; static int is_need_stop = 0; @@ -212,6 +213,8 @@ int main(int argc, char **argv) } } + http_healthcheck_server_start(profile); + timestamp_update(ctx->ts); ts_update_interval = timestamp_update_interval_ms(ctx->ts); g_metrics_last_send_ts = timestamp_get_msec(ctx->ts); @@ -230,6 +233,7 @@ int main(int argc, char **argv) } error_out: + http_healthcheck_server_stop(); for (int i = 0; i < ctx->nr_worker_threads; i++) { while (1) diff --git a/vendor/CMakeLists.txt b/vendor/CMakeLists.txt index 6c41481..6be2ce6 100644 --- a/vendor/CMakeLists.txt +++ b/vendor/CMakeLists.txt @@ -36,6 +36,23 @@ add_dependencies(cjson cJSON) set_property(TARGET cjson PROPERTY IMPORTED_LOCATION ${INSTALL_DIR}/lib64/libcjson.a) set_property(TARGET cjson 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= --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) diff --git a/vendor/libevent-2.1.8-stable.tar.gz b/vendor/libevent-2.1.8-stable.tar.gz new file mode 100644 index 0000000..2004f84 Binary files /dev/null and b/vendor/libevent-2.1.8-stable.tar.gz differ