Skip to content

Commit

Permalink
lua: use inspect module
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Stephens <pat@chronosphere.io>
  • Loading branch information
patrick-stephens committed Mar 14, 2024
1 parent 41a5313 commit d2c57fb
Show file tree
Hide file tree
Showing 4 changed files with 380 additions and 23 deletions.
2 changes: 1 addition & 1 deletion fluent-bit-config/cmd_fix_permissions.bat
Original file line number Diff line number Diff line change
@@ -1 +1 @@
echo chmod a+w testfile.txt > %LOG_DIR%}%/fix_permissions.lua.out
echo chmod a+w testfile.txt
3 changes: 1 addition & 2 deletions fluent-bit-config/cmd_fix_permissions.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
#!/bin/sh
mkdir -p "${LOG_DIR}"
echo chmod a+w testfile.txt > "${LOG_DIR}"/fix.lua.out
echo chmod a+w testfile.txt
27 changes: 7 additions & 20 deletions fluent-bit-config/demo.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local inspect = require 'inspect'

--[[ Determine which OS we're running on--]]
function getOS()
local osname
Expand All @@ -14,20 +16,6 @@ function getOS()
return osname or "Windows"
end

--[[ Pretty printer util for outputting the payload - here to help with diagnostics if needed--]]
function printRecord(record)
for key, value in pairs(record) do
local elementType = type(value)
if (elementType == "table") then
print(string.format("%s { %s = ", key))
printDetails(value .. " ")
print("}")
else
print(string.format("%s %s = %s --> %s", " ", key, tostring(value), elementType))
end
end
end

--[[ -This is the main function in the script and needs to be called. It retrieves from the
payload the cmd attribute which will tell us which script to invoke.
The script is assumed to be the cmd's value prefixed by cmd_ and post fixed with .sh or .bat
Expand All @@ -37,13 +25,12 @@ For diagnostics the printRecord method can be used
--]]
function cb_osCommand(tag, timestamp, record)
local code = 0
local commadAttribute = "command"
local commandAttribute = "command"
local command = ""
--[[printRecord(record)--]]
printRecord(record)
print(inspect(record))

if (record[commadAttribute] ~= nil) then
command = record[commadAttribute]
if (record[commandAttribute] ~= nil) then
command = record[commandAttribute]

print("Will execute " .. command)
if (getOS() == "Windows") then
Expand All @@ -55,7 +42,7 @@ function cb_osCommand(tag, timestamp, record)
print("Lua no command identified")
end

local fullCommand = command
local fullCommand = command .. " > " .. os.getenv("LOG_DIR") .. "/remoteCommand.lua.out"
print("running: " .. fullCommand)
local runCommandResult = os.execute(fullCommand)
print("response from exe command:" .. runCommandResult)
Expand Down
Loading

0 comments on commit d2c57fb

Please sign in to comment.