首先,我不是真正的程序员,但需要让它正常工作。
genmapping-ls.lua依赖aliasMatching运行时报错:
./genmapping-ls.lua别名匹配
lua: ./genmapping-ls.lua:26: 尝试索引一个 nil 值(全局“命令”) 堆栈回溯: ./genmapping-ls.lua:26: 在函数“updateModule”中 ./genmapping-ls.lua:70: 在主块中 [C]: 在 ?
文件看起来像这样,谁能帮我改正一下?非常感谢!
`genmapping-ls.lua:
> #!/usr/bin/env lua
>
> Requires you to have Synergy and Jeffrey Friedl's JSON module installed
> package.path = package.path .. ';/opt/synergy/share/?.lua;/opt/lua-json/?.lua'
>
> io = require('io')
> JSON = require('JSON')
> require ('extra')
>
>
> ```]
Extracts hosts from the livestatus database
function lsQuery()
query = io.popen('/usr/bin/mon query ls hosts -c name,alias,display_name,address -O json')
output = query:read('*all')
query:close()
Strips trailing newline and decodes JSON
output = string.gsub(output, "\n", "")
return JSON:decode(output)
end
Updates the Trapper module with new host mappings
function updateModule(moduleName, mapping)
print(mapping)
command = io.popen('echo ' .. mapping .. '| traped update ' .. moduleName)
output = command:read('*all')
print(output)
command:close()
Traped don't use exit codes properly - checking output instead
if output == '' then
return true
else
return false
end
end
Name for module in Trapper to update with mapping
moduleName = arg[1]
if moduleName == nil then
print('Usage: genmapping-ls "nameOfModuleInTrapper"\n')
do return end
end
Runs the Livestatus query and catches any errors
status, result = pcall(lsQuery)
if not status then
print('Failed to query Livestatus for host mapping')
do return end
end
Serializes the JSON to a Lua table
status, mapping = pcall(serialize, result)
if not status then
print('Failed to serialize JSON response from Livestatus')
do return end
end
Updates the Trapper module specied in the commnd line argument
mapping = 'mapping = ' .. mapping
status = updateModule(moduleName, mapping)
if not status then
print('Failed to update mapping module in Trapper')
else
print(mapping)
print('Successfully updated mapping module in Trapper')
end
aliasMatching:
> --[[
aliasMatching.lua -
This module includes functions to match the trap host to a host or alias.
It requires the "mapping" table generated by the "genmapping-ls.lua" script.
Usage example in trap handler:
> """
include "aliasMatching"
Tries to match host against name and address, but not alias or display name
hostName = matchHost(trap.host, true, false, false, true)
Returns host name if successfully matched or false otherwise
if hostName then
result.host = hostName
else
result.host = trap.host
end
> """
> --]]
Tries to match input host against host
function matchInput(input, name, alias, display, address)
if input == name then
log('Input host matched name for host ' .. name)
return {host = name, match = 'name'}
elseif input == alias then
log('Input host matched alias for host ' .. name)
return {host = name, match = 'alias'}
elseif input == display then
log('Input host matched display name for host ' .. name)
return {host = name, match = 'display'}
elseif input == address then
log('Input host matched address for host ' .. name)
return {host = name, match = 'address'}
else
> --log('Input host did not match any alias for host ' .. name)
return {host = false}
end
end
Checks matching complies with the matching rules
function matchPolicy(match, name, alias, display, address)
if match == 'name' and name then
log('Input host complied with matching rule "name"')
return true
elseif match == 'alias' and alias then
log('Input host complied with matching rule "alias"')
return true
elseif match == 'display' and display then
log('Input host complied with matching rule "display"')
return true
elseif match == 'address' and address then
log('Input host complied with matching rule "address"')
return true
else
log('Input host did not comply with any matching rule')
return false
end
end
Takes input host, matching policies and tries to match against host
function matchHost(input, matchName, matchAlias, matchDisplay, matchAddress)
log('Trying to find match for input host ' .. input)
Checks that the mapping table generated by "genmapping-ls.lua" exists
if not mapping then
log('Could not find host mapping table!')
return false
end
Tries to find matching host in host mapping
for host, alias in ipairs(mapping) do
matchObject = matchInput(input, alias[1], alias[2], alias[3], alias[4])
if matchObject.host then
break
end
end
Exits if no matching host was found
if not matchObject.host then
log('Could not find any matching host for ' .. input)
return false
end
Checks if the match complies with the matching policy
complyPolicy = matchPolicy(matchObject.match, matchName, matchAlias, matchDisplay, matchAddress)
if complyPolicy then
return matchObject.host
else
return false
end
end`