Skip to content

Commit

Permalink
Show all found files in the file_reader UI
Browse files Browse the repository at this point in the history
UI let's you select a file to start reading from the beginning.
Unfortunately, the list got broken after wildcards were added.

Instead of using the configuration list, the browser part now gets the
current watched list directly from the agent during initialization.
  • Loading branch information
souryogurt committed Jan 19, 2023
1 parent e5b4f2f commit d3519d2
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 39 deletions.
20 changes: 18 additions & 2 deletions file_reader/1.0.0/bmodule/main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
<el-tabs tab-position="left" v-model="leftTab">
<el-tab-pane name="api" :label="locale[$i18n.locale]['api']" v-if="viewMode === 'agent'">
<div class="layout-margin-xl limit-length">
<el-input :key="file.filepath" v-for="file in module.current_config.log_files" :value="file.filepath" readonly>
<el-input :key="file" v-for="file in files" :value="file" readonly>
<el-button
slot="append"
icon="el-icon-s-promotion"
class="layout-row-none"
@click="readFromBegining(file.filepath)"
@click="readFromBegining(file)"
>{{ module.locale.actions['frd_rewind_logfile'][$i18n.locale].title }}
</el-button>
</el-input>
Expand All @@ -33,6 +33,7 @@ module.exports = {
leftTab: undefined,
connection: {},
lines: [],
files: [],
locale: {
ru: {
api: "VX API",
Expand All @@ -54,6 +55,8 @@ module.exports = {
connection => {
const date = new Date().toLocaleTimeString();
this.connection = connection;
this.connection.subscribe(this.recvData, "data");
this.requestFiles();
this.$root.NotificationsService.success(`${date} ${this.locale[this.$i18n.locale]['connected']}`);
},
error => {
Expand All @@ -67,6 +70,19 @@ module.exports = {
this.leftTab = this.viewMode === 'agent' ? 'api' : undefined;
},
methods: {
requestFiles() {
let data = JSON.stringify({
type: "update_file_list_req",
});
this.connection.sendData(data);
},
recvData(msg) {
let data = new TextDecoder("utf-8").decode(msg.content.data);
let msg_data = JSON.parse(data);
if (msg_data.type == "update_file_list_resp") {
this.files = msg_data.files
}
},
readFromBegining(logfile) {
const date = new Date();
const date_ms = date.toLocaleTimeString() + `.${date.getMilliseconds()}`;
Expand Down
20 changes: 18 additions & 2 deletions file_reader/1.0.0/cmodule/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ local function configure()
end
q_e_stop:clear()
q_e_quit:clear()
current_files = {}
for _, fl in ipairs(module_config.log_files or {}) do
local filepath = fl["filepath"]
if fs_notify.is_glob_pattern(filepath) then
Expand Down Expand Up @@ -327,11 +328,26 @@ end

__api.add_cbs({

-- data = function(src, data)
-- file = function(src, path, name)
-- text = function(src, text, name)
-- msg = function(src, msg, mtype)
-- action = function(src, data, name)

data = function(src, data)
__log.debugf("received data message from '%s' with data %s", src, data)
local msg = cjson.decode(data) or {}
if msg.type == "update_file_list_req" then
local files = {}
for file in pairs(current_files) do
table.insert(files, file)
end
table.sort(files)
__api.send_data_to(src, cjson.encode({
["type"] = "update_file_list_resp",
["retaddr"] = msg.retaddr,
["files"] = files,
}))
end
end,

action = function(src, data, name)
__log.infof("receive action '%s' from '%s' with data %s", name, src, data)
Expand Down
75 changes: 40 additions & 35 deletions file_reader/1.0.0/smodule/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ local limit_nums = tonumber(__args["sender_limits"][2])
local target_path, target_sock, target_proto, target_host, target_port, target_connect
local log_lines = {}

-- variables to cache data into lua state
local agents_cache = require("lrucache")()
agents_cache.initialized = true
agents_cache.max_size = tonumber(__args["agents_cache_size"][1])

-- return number value that present events amount inside
local function get_log_lines(num)
local size = 0
Expand All @@ -41,23 +36,15 @@ local function get_option_config(opt)
end

-- getting agent ID by dst token and agent type
local function get_agent_id_by_dst(dst)
local val = agents_cache:get(dst)
if type(val) == "table" then
return val._value.id, val._value.hostname
end

local function get_agent_id_by_dst(dst, atype)
for client_id, client_info in pairs(__agents.get_by_dst(dst)) do
if client_id == dst then
local aid, hostname = tostring(client_info.ID), nil
if client_info.Info ~= nil and client_info.Info.Net ~= nil then
hostname = tostring(client_info.Info.Net.Hostname)
if tostring(client_info.Type) == atype then
return tostring(client_info.ID), client_info
end
agents_cache:put(dst, {_value = {hostname = hostname, id = aid}})
return aid, hostname
end
end
return "", nil
return "", {}
end

-- getting agent source token by ID and agent type
Expand Down Expand Up @@ -185,28 +172,46 @@ configure_socket()

__api.add_cbs({
data = function(src, data)
local aid, hostname = get_agent_id_by_dst(src)
local msg_data = cjson.decode(data)
if type(msg_data["data"]) ~= "table" then
msg_data["data"] = {}
end
msg_data["data"]["hostname"] = hostname or aid or src
if msg_data["type"] == "log" and type(msg_data["message"]) == "table" then
__metric.add_int_counter("frd_server_events_recv_count", #msg_data["message"])
__metric.add_int_counter("frd_server_events_recv_size", #data)
if target_path ~= "" then
for _, msg in ipairs(msg_data["message"]) do
table.insert(log_lines, {
message = msg,
data = msg_data.data,
})
local vxagent_id, client_info = get_agent_id_by_dst(src, "VXAgent")
local bagent_id = get_agent_id_by_dst(src, "Browser")
local eagent_id = get_agent_id_by_dst(src, "External")
if vxagent_id ~= "" then
if type(msg_data["data"]) ~= "table" then
msg_data["data"] = {}
end
local aid, hostname = tostring(client_info.ID), nil
if client_info.Info ~= nil and client_info.Info.Net ~= nil then
hostname = tostring(client_info.Info.Net.Hostname)
end
msg_data["data"]["hostname"] = hostname or aid or src
if msg_data["type"] == "log" and type(msg_data["message"]) == "table" then
__metric.add_int_counter("frd_server_events_recv_count", #msg_data["message"])
__metric.add_int_counter("frd_server_events_recv_size", #data)
if target_path ~= "" then
for _, msg in ipairs(msg_data["message"]) do
table.insert(log_lines, {
message = msg,
data = msg_data.data,
})
end
else
__log.debugf("receive log message (events amount): '%d'", #msg_data["message"])
end
elseif msg_data["type"] == "update_file_list_resp" then
local browser_id = msg_data.retaddr
msg_data.retaddr = nil
__api.send_data_to(browser_id, cjson.encode(msg_data))
else
__log.debugf("receive log message (events amount): '%d'", #msg_data["message"])
__log.debugf("receive enexpected message: '%s' and type '%s'",
msg_data["type"], type(msg_data["message"]))
end
else
__log.debugf("receive enexpected message: '%s' and type '%s'",
msg_data["type"], type(msg_data["message"]))
-- msg from browser or external...
local agent_id = bagent_id ~= "" and bagent_id or eagent_id
local dst, _ = get_agent_src_by_id(agent_id, "VXAgent")
msg_data.retaddr = src
__api.send_data_to(dst, cjson.encode(msg_data))
end
return true
end,
Expand All @@ -217,7 +222,7 @@ __api.add_cbs({
local action_data = cjson.decode(data)
assert(type(action_data) == "table", "input action data type is invalid")
action_data.retaddr = src
local id, _ = get_agent_id_by_dst(src)
local id, _ = get_agent_id_by_dst(src, "Browser")
local dst, _ = get_agent_src_by_id(id, "VXAgent")
if dst ~= "" then
__log.debugf("send action request to '%s'", dst)
Expand Down

0 comments on commit d3519d2

Please sign in to comment.