Merge pull request #829 from eeff/eeff/dev

feat(monitor): Add dummy metric handler
This commit is contained in:
fengzeroz
2022-10-11 09:36:35 +08:00
committed by GitHub
7 changed files with 212 additions and 9 deletions
+1
View File
@@ -122,6 +122,7 @@ set(NEURON_SOURCES
plugins/restful/proxy.c
plugins/restful/websocket.c
plugins/monitor/monitor.c
plugins/monitor/metric_handle.c
${PERSIST_SOURCES})
set(CMAKE_BUILD_RPATH ./)
+93
View File
@@ -0,0 +1,93 @@
/**
* NEURON IIoT System for Industry 4.0
* Copyright (C) 2020-2022 EMQ Technologies Co., Ltd All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser 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 "utils/asprintf.h"
#include "utils/log.h"
#include "metric_handle.h"
#include "restful/handle.h"
#include "restful/http.h"
#define METRIC_GLOBAL_TMPL \
"# HELP core_dumped Whether there is any core dump\n" \
"# TYPE core_dumped counter\n" \
"core_dumped %d\n" \
"# HELP uptime_seconds Uptime in seconds\n" \
"# TYPE uptime_seconds counter\n" \
"uptime_seconds %" PRIu64 "\n" \
"# HELP north_nodes Number of north nodes\n" \
"# TYPE north_nodes counter\n" \
"north_nodes %zu\n" \
"# HELP north_running_nodes Number of north nodes in running state\n" \
"# TYPE north_running_nodes counter\n" \
"north_running_nodes %zu\n" \
"# HELP north_disconnected_nodes Number of north nodes disconnected\n" \
"# TYPE north_disconnected_nodes counter\n" \
"north_disconnected_nodes %zu\n" \
"# HELP south_nodes Number of south nodes\n" \
"# TYPE south_nodes counter\n" \
"south_nodes %zu\n" \
"# HELP south_running_nodes Number of south nodes in running state\n" \
"# TYPE south_running_nodes counter\n" \
"south_running_nodes %zu\n" \
"# HELP south_disconnected_nodes Number of south nodes disconnected\n" \
"# TYPE south_disconnected_nodes counter\n" \
"south_disconnected_nodes %zu\n"
static int response(nng_aio *aio, char *content, enum nng_http_status status)
{
nng_http_res *res = NULL;
nng_http_res_alloc(&res);
nng_http_res_set_header(res, "Content-Type", "text/plain");
nng_http_res_set_header(res, "Access-Control-Allow-Origin", "*");
nng_http_res_set_header(res, "Access-Control-Allow-Methods",
"POST,GET,PUT,DELETE,OPTIONS");
nng_http_res_set_header(res, "Access-Control-Allow-Headers", "*");
if (content != NULL && strlen(content) > 0) {
nng_http_res_copy_data(res, content, strlen(content));
}
nng_http_res_set_status(res, status);
nng_http_req *nng_req = nng_aio_get_input(aio, 0);
nlog_notice("%s %s [%d]", nng_http_req_get_method(nng_req),
nng_http_req_get_uri(nng_req), status);
nng_aio_set_output(aio, 0, res);
nng_aio_finish(aio, 0);
return 0;
}
void handle_get_metric(nng_aio *aio)
{
char *result = NULL;
neu_asprintf(&result, METRIC_GLOBAL_TMPL, 0, 0, 0, 0, 0, 0, 0, 0);
if (NULL == result) {
nng_aio_finish(aio, NNG_EINTERNAL);
return;
}
response(aio, result, NNG_HTTP_STATUS_OK);
free(result);
}
+27
View File
@@ -0,0 +1,27 @@
/**
* NEURON IIoT System for Industry 4.0
* Copyright (C) 2020-2022 EMQ Technologies Co., Ltd All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser 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.
**/
#ifndef NEU_PLUGIN_MONITOR_METRIC_H
#define NEU_PLUGIN_MONITOR_METRIC_H
#include <nng/nng.h>
void handle_get_metric(nng_aio *aio);
#endif
+78
View File
@@ -22,22 +22,98 @@
#include <string.h>
#include <nng/nng.h>
#include <nng/supplemental/http/http.h>
#include "metric_handle.h"
#include "monitor.h"
#include "restful/handle.h"
#include "utils/log.h"
#define neu_plugin_module default_monitor_plugin_module
struct neu_plugin {
neu_plugin_common_t common;
nng_http_server * api_server;
};
static nng_http_server *server_init()
{
nng_url * url = NULL;
nng_http_server *server = NULL;
const char * host_port = "http://0.0.0.0:7001";
nlog_info("monitor bind url: %s", host_port);
int ret = nng_url_parse(&url, host_port);
if (0 != ret) {
return NULL;
}
ret = nng_http_server_hold(&server, url);
if (0 != ret) {
nlog_error("monitor server bind error: %d", ret);
}
nng_url_free(url);
return server;
}
static struct neu_rest_handler api_handlers[] = {
{
.method = NEU_REST_METHOD_GET,
.type = NEU_REST_HANDLER_FUNCTION,
.url = "/api/v2/metrics",
.value.handler = handle_get_metric,
},
};
static int add_handlers(nng_http_server *server)
{
struct neu_rest_handler cors = {
.method = NEU_REST_METHOD_OPTIONS,
.type = NEU_REST_HANDLER_FUNCTION,
.value.handler = neu_rest_handle_cors,
};
for (size_t i = 0; i < sizeof(api_handlers) / sizeof(api_handlers[0]);
++i) {
if (0 != neu_rest_add_handler(server, &api_handlers[0])) {
return -1;
}
cors.url = api_handlers[i].url;
if (0 != neu_rest_add_handler(server, &cors)) {
return -1;
}
}
return 0;
}
static neu_plugin_t *monitor_plugin_open(void)
{
neu_plugin_t *plugin = calloc(1, sizeof(neu_plugin_t));
if (NULL == plugin) {
return NULL;
}
neu_plugin_common_init(&plugin->common);
plugin->api_server = server_init();
if (NULL == plugin->api_server) {
nlog_error("fail to create monitor server");
free(plugin);
return NULL;
}
if (0 != add_handlers(plugin->api_server) ||
0 != nng_http_server_start(plugin->api_server)) {
nlog_error("fail to start monitor server");
nng_http_server_release(plugin->api_server);
free(plugin);
return NULL;
}
return plugin;
}
@@ -45,6 +121,8 @@ static int monitor_plugin_close(neu_plugin_t *plugin)
{
int rv = 0;
nng_http_server_stop(plugin->api_server);
nng_http_server_release(plugin->api_server);
free(plugin);
nlog_info("Success to free plugin: %s", neu_plugin_module.module_name);
+2 -4
View File
@@ -52,8 +52,6 @@ struct neu_rest_handler web_handlers[] = {
},
};
static void cors(nng_aio *aio);
struct neu_rest_handler cors_handler[] = {
{
.url = "/api/v2/ping",
@@ -338,7 +336,7 @@ void neu_rest_api_cors_handler(const struct neu_rest_handler **handlers,
for (uint32_t i = 0; i < *size; i++) {
cors_handler[i].method = NEU_REST_METHOD_OPTIONS;
cors_handler[i].type = NEU_REST_HANDLER_FUNCTION;
cors_handler[i].value.handler = cors;
cors_handler[i].value.handler = neu_rest_handle_cors;
}
}
@@ -360,7 +358,7 @@ void *neu_rest_get_plugin()
return rest_ctx->plugin;
}
static void cors(nng_aio *aio)
void neu_rest_handle_cors(nng_aio *aio)
{
nng_http_res *res = NULL;
+6
View File
@@ -22,6 +22,9 @@
#include <stdlib.h>
#include <nng/nng.h>
#include <nng/supplemental/http/http.h>
#include "plugin.h"
#include "utils/neu_jwt.h"
#include "json/neu_json_error.h"
@@ -106,10 +109,13 @@ neu_rest_handle_ctx_t *neu_rest_init_ctx(void *plugin);
void neu_rest_free_ctx(neu_rest_handle_ctx_t *ctx);
void * neu_rest_get_plugin();
int neu_rest_add_handler(nng_http_server * server,
const struct neu_rest_handler *rest_handler);
void neu_rest_web_handler(const struct neu_rest_handler **handlers,
uint32_t * size);
void neu_rest_api_handler(const struct neu_rest_handler **handlers,
uint32_t * size);
void neu_rest_api_cors_handler(const struct neu_rest_handler **handlers,
uint32_t * size);
void neu_rest_handle_cors(nng_aio *aio);
#endif
+5 -5
View File
@@ -49,8 +49,8 @@ struct neu_plugin {
neu_rest_handle_ctx_t *handle_ctx;
};
static int rest_add_handler(nng_http_server * server,
const struct neu_rest_handler *rest_handler)
int neu_rest_add_handler(nng_http_server * server,
const struct neu_rest_handler *rest_handler)
{
nng_http_handler *handler;
int ret = -1;
@@ -166,11 +166,11 @@ static neu_plugin_t *dashb_plugin_open(void)
neu_rest_api_handler(&rest_handlers, &n_handler);
for (uint32_t i = 0; i < n_handler; i++) {
rest_add_handler(plugin->api_server, &rest_handlers[i]);
neu_rest_add_handler(plugin->api_server, &rest_handlers[i]);
}
neu_rest_api_cors_handler(&cors, &n_handler);
for (uint32_t i = 0; i < n_handler; i++) {
rest_add_handler(plugin->api_server, &cors[i]);
neu_rest_add_handler(plugin->api_server, &cors[i]);
}
if ((rv = nng_http_server_start(plugin->api_server)) != 0) {
nlog_error("Failed to start api server, error=%d", rv);
@@ -179,7 +179,7 @@ static neu_plugin_t *dashb_plugin_open(void)
neu_rest_web_handler(&rest_handlers, &n_handler);
for (uint32_t i = 0; i < n_handler; i++) {
rest_add_handler(plugin->web_server, &rest_handlers[i]);
neu_rest_add_handler(plugin->web_server, &rest_handlers[i]);
}
if ((rv = nng_http_server_start(plugin->web_server)) != 0) {