From 75eb2161c641f85dded64a99cb862abfab64eff7 Mon Sep 17 00:00:00 2001 From: Sebastien Douheret Date: Tue, 12 Jun 2018 17:52:40 +0200 Subject: Improved supervisor requests & events recording Change-Id: I4eb52820d2bec4ca4f2e3e455db7eb79d1a09d12 Signed-off-by: Sebastien Douheret --- src/plugins/supervisor-api.c | 157 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 127 insertions(+), 30 deletions(-) (limited to 'src/plugins/supervisor-api.c') diff --git a/src/plugins/supervisor-api.c b/src/plugins/supervisor-api.c index 0c1a5bc..c21cd8d 100644 --- a/src/plugins/supervisor-api.c +++ b/src/plugins/supervisor-api.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include "ctl-plugin.h" @@ -44,6 +45,7 @@ CTLP_CAPI(list, source, argsJ, eventJ) return ERROR; } + AFB_ApiInfo(source->api, "Build response (nb daemons %d)", daemons->count); result = json_object_new_array(); for (int i = 0; i < daemons->count; i++) { @@ -58,31 +60,43 @@ CTLP_CAPI(list, source, argsJ, eventJ) //, "config", daemons->daemons[i]->config); json_object_array_add(result, item); } - AFB_ReqSucess(source->request, result, NULL); + + AFB_ApiInfo(source->api, "Send response"); + AFB_ReqSuccess(source->request, result, NULL); return 0; } -CTLP_CAPI(trace, source, argsJ, eventJ) +CTLP_CAPI(trace, source, argsJ, queryJ) { int rc; json_object* result = NULL; DAEMONS_T* daemons = NULL; - const char* ws_name; - const char* wsn; + const char* ws_name = ""; + const char* level = NULL; + pid_t pid = -1; - if (wrap_json_unpack(argsJ, "{s:?s}", "ws", &ws_name)) { + if (wrap_json_unpack(queryJ, "{s:?i s:?s s:?s}", + "pid", &pid, "ws", &ws_name, "level", &level)) { AFB_ReqFail(source->request, "Failed", "Error processing arguments."); return ERROR; } - AFB_ApiNotice(source->api, "Trace ws: %s", ws_name); + + if (pid == -1 && strlen(ws_name) == 0) { + AFB_ReqFail(source->request, "failed", "one of pid or ws parameter must be set"); + return ERROR; + } + + AFB_ApiDebug(source->api, "Trace pid: %d ws: %s", pid, ws_name); getDaemons(source->api, &daemons); if (daemons == NULL || daemons->count <= 0) { AFB_ReqFail(source->request, "failed", "No daemon found"); + return ERROR; } // search server and client pid DAEMON_T *pid_s = NULL, *pid_c = NULL; + const char* wsn; for (int i = 0; i < daemons->count; i++) { AFB_ApiDebug(source->api, "_DEBUG_ svr %s", json_object_to_json_string(daemons->daemons[i]->ws_servers)); @@ -109,7 +123,7 @@ CTLP_CAPI(trace, source, argsJ, eventJ) } if (pid_s != NULL && pid_c != NULL) { - if ((rc = trace_exchange(source->api, pid_s, pid_c)) < 0) { + if ((rc = trace_exchange(source->api, pid_s, pid_c, level)) < 0) { AFB_ReqFailF(source->request, "failed", "Trace error %d", rc); } break; @@ -121,44 +135,127 @@ CTLP_CAPI(trace, source, argsJ, eventJ) return ERROR; } - AFB_ReqSucessF(source->request, result, "Tracing Server pid=%d <-> Client pid=%d", pid_s->pid, pid_c->pid); + AFB_ReqSuccessF(source->request, result, "Tracing Server pid=%d <-> Client pid=%d", pid_s->pid, pid_c->pid); + + return 0; +} + +uint64_t get_ts() +{ + struct timespec ts; + clock_gettime(CLOCK_REALTIME, &ts); + + return (uint64_t)(ts.tv_sec) * (uint64_t)1000000000 + (uint64_t)(ts.tv_nsec); +} + +static void cb_harvester_write(void* closure, int status, struct json_object* result, struct afb_dynapi* dynapi) +{ + AFB_ApiDebug(dynapi, "SEB cb_harvester_write"); +} + +/* SEB Cleanup if move in lua */ +static int harvester_post_data(CtlSourceT* source, METRIC_T* metric) +{ + int rc; + json_object *j_res, *j_query, *j_metric; + + if (!metric->timestamp) { + metric->timestamp = get_ts(); + } + + // To be REWORK + if (metric->dataType == SPVR_DATA_STRING) { + rc = wrap_json_pack(&j_metric, "{s:s s:{s:s s:s} s:{s:s} s:I }", + "name", metric->name, + "metadata", "source", "my_source", "identity", source->uid, + "values", "value", metric->data, + "timestamp", metric->timestamp); + } else if (metric->dataType == SPVR_DATA_INT) { + rc = wrap_json_pack(&j_metric, "{s:s s:{s:s s:s} s:{s:i} s:I }", + "name", metric->name, + "metadata", "source", "my_source", "identity", source->uid, + "values", "value", metric->data, + "timestamp", metric->timestamp); + } else { + AFB_ApiError(source->api, "Unsupported dataType"); + return ERROR; + } + + if (rc < 0) { + AFB_ApiError(source->api, "Error packing metric, rc=%d", rc); + return rc; + } + + rc = wrap_json_pack(&j_query, "{s:s s:i s:o }", "host", + "localhost", "port", 8086, "metric", j_metric); + if (rc < 0) { + AFB_ApiError(source->api, "Error packing query, rc=%d", rc); + return rc; + } + + AFB_ApiDebug(source->api, "%s write: %s", SRV_HARVESTER_NAME, + json_object_to_json_string(j_query)); + + /* SEB + rc = AFB_ServiceSync(source->api, SRV_HARVESTER_NAME, "write", j_query, &j_res); + if (rc < 0) { + AFB_ApiError(source->api, "Error %s write : rc=%d, j_res=%s", SRV_HARVESTER_NAME, rc, + json_object_to_json_string(j_res)); + return rc; + } +*/ + AFB_ServiceCall(source->api, SRV_HARVESTER_NAME, "write", j_query, cb_harvester_write, &j_res); return 0; } -/* SEB TODO -void xds_event_cb(const char* evtname, json_object* j_event) +CTLP_CAPI(tracing_events, source, argsJ, eventJ) { int rc; - METRIC_T metric; + METRIC_T metric = { 0 }; const char* type = NULL; struct json_object* request = NULL; - AFB_NOTICE("RECV Event %s : %s", evtname, - json_object_to_json_string(j_event)); + //struct signalCBT* ctx = (struct signalCBT*)source->context; + + AFB_ApiDebug(source->api, ">>> RECV Event uid %s : %s", source->uid, + json_object_to_json_string(eventJ)); - if (strcmp(evtname, "supervisor/trace") != 0) { - return; + if (strcmp(source->uid, "supervisor/trace") != 0) { + AFB_ApiNotice(source->api, "WARNING: un-handle uid '%s'", source->uid); + return 0; } - if ((rc = wrap_json_unpack(j_event, "{s:?s}", "type", &type)) < 0) { - AFB_ERROR("Cannot decode event type"); - return; + if ((rc = wrap_json_unpack(eventJ, "{s:?s}", "type", &type)) < 0) { + AFB_ApiError(source->api, "Cannot decode event type"); + return ERROR; } - if (strcmp(type, "request") == 0) { + if (strcmp(type, "request") != 0) { + AFB_ApiError(source->api, "Cannot retrieve request"); + return ERROR; + } + if (!json_object_object_get_ex(eventJ, "request", &request)) { + AFB_ApiError(source->api, "Cannot decode event request"); + return ERROR; + } - if (!json_object_object_get_ex(j_event, "request", &request)) { - AFB_ERROR("Cannot decode event request"); - return; - } - metric.name = "trace"; - metric.data = request; + // TODO: decode request and build trace - rc = harvester_post_data(&metric); - if (rc < 0) { - AFB_ERROR("ERROR harvester_post_data: rc %d", rc); - } + metric.name = "trace"; + /* FIXME string KO + metric.dataType = SPVR_DATA_STRING; + metric.data = "test1234"; + */ + metric.dataType = SPVR_DATA_INT; + int val = 54321; + metric.data = &val; + + rc = harvester_post_data(source, &metric); + if (rc < 0) { + AFB_ApiError(source->api, "ERROR harvester_post_data: rc %d", rc); + return rc; } + + return 0; } -*/ -- cgit 1.2.3-korg