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 --- conf.d/cmake/config.cmake | 2 +- conf.d/project/etc/xds-config.json | 27 ------ conf.d/project/etc/xds-supervisor-config.json | 39 +++++++++ conf.d/project/lua.d/CMakeLists.txt | 32 +++++++ conf.d/project/lua.d/xds-supervisor.lua | 119 ++++++++++++++++++++++++++ 5 files changed, 191 insertions(+), 28 deletions(-) delete mode 100644 conf.d/project/etc/xds-config.json create mode 100644 conf.d/project/etc/xds-supervisor-config.json create mode 100644 conf.d/project/lua.d/CMakeLists.txt create mode 100644 conf.d/project/lua.d/xds-supervisor.lua (limited to 'conf.d') diff --git a/conf.d/cmake/config.cmake b/conf.d/cmake/config.cmake index 8ffa90d..3c17187 100644 --- a/conf.d/cmake/config.cmake +++ b/conf.d/cmake/config.cmake @@ -197,7 +197,7 @@ set(AFB_REMPORT "5678" CACHE PATH "Default binder listening port") # Print a helper message when every thing is finished # ---------------------------------------------------- -set(CLOSING_MESSAGE "Typical binding launch: afb-daemon --port=${AFB_REMPORT} --name=afb-xds --workdir=${CMAKE_BINARY_DIR}/package --ldpaths=lib --roothttp=htdocs --token=\"${AFB_TOKEN}\" --verbose --ws-client=unix:/tmp/supervisor --ws-client=unix:/tmp/harvester") +set(CLOSING_MESSAGE "Typical binding launch: afb-daemon --port=${AFB_REMPORT} --name=afb-xds --workdir=${CMAKE_BINARY_DIR}/package --ldpaths=lib --roothttp=htdocs --token=\"${AFB_TOKEN}\" --ws-client=unix:/tmp/supervisor --ws-client=unix:/tmp/harvester -vv ") set(PACKAGE_MESSAGE "Install widget file using in the target : afm-util install ${PROJECT_NAME}.wgt") # Optional schema validator about now only XML, LUA and JSON diff --git a/conf.d/project/etc/xds-config.json b/conf.d/project/etc/xds-config.json deleted file mode 100644 index 3646d3f..0000000 --- a/conf.d/project/etc/xds-config.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "$schema": "http://iot.bzh/download/public/schema/json/ctl-schema.json", - "metadata": { - "uid": "XDS", - "version": "1.0", - "api": "xds", - "info": "XDS Data collection binding" - }, - "plugins": [{ - "uid": "supervisor", - "info": "Plugin to handle interface with supervisor", - "spath": "lib/plugins", - "libs": "supervisor.ctlso" - }], - - "onload": [], - - "controls": [{ - "uid": "list", - "action": "plugin://supervisor#list" - }, - { - "uid": "trace", - "action": "plugin://supervisor#trace" - } - ] -} diff --git a/conf.d/project/etc/xds-supervisor-config.json b/conf.d/project/etc/xds-supervisor-config.json new file mode 100644 index 0000000..39309bb --- /dev/null +++ b/conf.d/project/etc/xds-supervisor-config.json @@ -0,0 +1,39 @@ +{ + "$schema": "http://iot.bzh/download/public/schema/json/ctl-schema.json", + "metadata": { + "uid": "XDS", + "version": "1.0", + "api": "xds", + "info": "XDS Data collection binding" + }, + "plugins": [{ + "uid": "supervisor", + "info": "Plugin to handle interface with supervisor", + "spath": "./lib/plugins:./var", + "libs": [ + "supervisor.ctlso", + "xds-supervisor.lua" + ] + }], + + "onload": [], + + "controls": [{ + "uid": "list", + "privileges": "urn:AGL:permission::platform:can:list ", + "action": "plugin://supervisor#list" + }, + { + "uid": "trace", + "privileges": "urn:AGL:permission::platform:can:trace ", + "action": "plugin://supervisor#trace" + } + ], + + "events": [{ + "uid": "supervisor/xds-trace", + "action": "lua://supervisor#_trace_events_" + } + ] + +} diff --git a/conf.d/project/lua.d/CMakeLists.txt b/conf.d/project/lua.d/CMakeLists.txt new file mode 100644 index 0000000..1e64cf2 --- /dev/null +++ b/conf.d/project/lua.d/CMakeLists.txt @@ -0,0 +1,32 @@ +########################################################################### +# Copyright 2018 IoT.bzh +# +# author: Fulup Ar Foll +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +########################################################################### + + +################################################## +# XDS Lua Scripts +################################################## +PROJECT_TARGET_ADD(xds-lua) + + file(GLOB LUA_FILES "*.lua") + + add_input_files("${LUA_FILES}") + + SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES + LABELS "DATA" + OUTPUT_NAME ${TARGET_NAME} + ) diff --git a/conf.d/project/lua.d/xds-supervisor.lua b/conf.d/project/lua.d/xds-supervisor.lua new file mode 100644 index 0000000..747d33d --- /dev/null +++ b/conf.d/project/lua.d/xds-supervisor.lua @@ -0,0 +1,119 @@ +--[[ + Copyright (C) 2018 "IoT.bzh" + Author Sebastien Douheret + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + + NOTE: strict mode: every global variables should be prefixed by '_' +--]] + +-- return serialised version of printable table +function ToJson(o) + if type(o) == "table" then + local s = "{" + local i = 0 + for k, v in pairs(o) do + if (i > 0) then + s = s .. "," + end + i = i + 1 + if type(k) ~= "number" then + k = '"' .. k .. '"' + end + s = s .. k .. ":" .. ToJson(v) + end + return s .. "}" + else + return '"' .. tostring(o) .. '"' + end +end + +function _trace_hello_events_(source, args, event) + -- TODO nothing for now +end + +function _trace_Async_CB(source, response, context) + --AFB:debug(source, "--InLua-- _trace_Async_CB response=%s context=%s\n", Dump_Table(response), Dump_Table(context)) + + if (response["request"]["status"] ~= "success") then + AFB:error(source, "--InLua-- _trace_Async_CB response=%s context=%s", Dump_Table(response), Dump_Table(context)) + return + end +end + +function _trace_events_(source, args, event) + --AFB:notice(source, "--InLua-- ENTER _trace_events_ event=%s\n", Dump_Table(event)) + + local query = { + ["host"] = "localhost", + ["port"] = 8086, + ["metric"] = { + { + ["name"] = "supervisor/trace", + ["metadata"] = { + ["identity"] = "xds supervisor", + ["tag"] = event["tag"], + }, + ["values"] = { + ["id"] = event["id"] + }, + ["timestamp"] = event["time"] + } + } + } + + if event.request then + local request = event.request + -- Filter out some traces + -- if (request.action == "begin" and request.action == "end" and request.action == "json") then + -- AFB:debug(source, "--InLua-- _trace_events_ IGNORED event=%s\n", Dump_Table(event)) + -- return + -- end + AFB:notice(source, ">>> PROCESS request %s", request) + query.metric[1].metadata.api = request.api + query.metric[1].metadata.verb = request.verb + query.metric[1].metadata.action = request.action + query.metric[1].metadata.session = request.session + query.metric[1].metadata.req_index = tostring(request.index) + if event.data then + query.metric[1].values.data = ToJson(event.data) + end + + elseif event.event then + local evt = event.event + AFB:notice(source, ">>> PROCESS event %s", evt) + query.metric[1].metadata.id = evt.id + query.metric[1].metadata.name = evt.name + query.metric[1].metadata.action = evt.action + if event.data then + query.metric[1].values.data = ToJson(event.data) + end + + else + AFB:warning(source, "--InLua-- UNKNOWN _trace_events_ event type: %s\n", Dump_Table(event)) + return + end + + AFB:debug(source, "CALL harvester write query=%s", Dump_Table(query)) + local err, response = AFB:service(source, "harvester", "write", query, "_trace_Async_CB", query) + + -- FIXME SEB : still true ? + -- Note: in current version controls only return a status. Also we may safely ignore API response + if (err) then + AFB:error(source, "--LUA:_trace_events_ harvester write refuse response=%s", response) + return 1 + end + + return 0 +end -- cgit 1.2.3-korg