update
This commit is contained in:
39
bibliotheque/files/nsclient/scripts/lua/check_cpu_ex.lua
Normal file
39
bibliotheque/files/nsclient/scripts/lua/check_cpu_ex.lua
Normal file
@ -0,0 +1,39 @@
|
||||
function install()
|
||||
-- Used to install this script
|
||||
local conf = nscp.Settings()
|
||||
conf:set_string('/modules', 'CheckSystem', 'enabled')
|
||||
conf:set_string('/modules', 'CheckHelpers', 'enabled')
|
||||
conf:set_string('/modules', 'LUAScript', 'enabled')
|
||||
conf:set_string('/settings/lua/scripts', 'check_cpu_ex', 'check_cpu_ex')
|
||||
conf:save()
|
||||
end
|
||||
|
||||
function setup()
|
||||
-- register our function
|
||||
local reg = nscp.Registry()
|
||||
reg:simple_query('check_cpu_ex', check_cpu_ex, 'Check CPU version which returns top consumers')
|
||||
end
|
||||
|
||||
function check_cpu_ex(command, arguments)
|
||||
local core = nscp.Core()
|
||||
cpu_result, cpu_message, cpu_perf = core:simple_query('check_cpu', arguments)
|
||||
if cpu_result == 'UNKNOWN' then
|
||||
core:log('error', string.format('Invalid return from check_cpu: %s', cpu_result))
|
||||
return cpu_result, cpu_message, cpu_perf
|
||||
end
|
||||
-- Status is good, lets execute check_process and filter_perf.
|
||||
proc_result, proc_message, proc_perf = core:simple_query('filter_perf', {'command=check_process', 'sort=normal', 'limit=5', 'arguments', 'delta=true', 'warn=time>0', 'filter=time>0'})
|
||||
return cpu_result, 'Top preformers: ' .. proc_perf, cpu_perf
|
||||
end
|
||||
|
||||
setup()
|
||||
|
||||
function main(args)
|
||||
cmd = args[0] or ''
|
||||
if cmd == 'install' then
|
||||
install()
|
||||
return 'ok', 'Script installed'
|
||||
else
|
||||
return 'error', 'Usage: .. install'
|
||||
end
|
||||
end
|
29
bibliotheque/files/nsclient/scripts/lua/default_check_mk.lua
Normal file
29
bibliotheque/files/nsclient/scripts/lua/default_check_mk.lua
Normal file
@ -0,0 +1,29 @@
|
||||
|
||||
function client_process(packet)
|
||||
cnt = packet:size_section()
|
||||
nscp.print('Got packets: ' .. cnt)
|
||||
for i = 1, cnt do
|
||||
s = packet:get_section(i)
|
||||
ln = s:size_line()
|
||||
sz = s:get_title()
|
||||
nscp.print(' + ' .. ln .. ': ' .. sz)
|
||||
for j = 1, s:size_line() do
|
||||
ln = s:get_line(j)
|
||||
nscp.print(' + ' .. ln:get_line())
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function server_process(packet)
|
||||
s = nscp.section()
|
||||
s:set_title("check_mk")
|
||||
s:add_line("Version: 0.0.1")
|
||||
s:add_line("Agent: nsclient++")
|
||||
s:add_line("AgentOS: Windows")
|
||||
packet:add_section(s)
|
||||
return true
|
||||
end
|
||||
|
||||
reg = nscp.check_mk()
|
||||
reg:client_callback(client_process)
|
||||
reg:server_callback(server_process)
|
162
bibliotheque/files/nsclient/scripts/lua/lib/test_helper.lua
Normal file
162
bibliotheque/files/nsclient/scripts/lua/lib/test_helper.lua
Normal file
@ -0,0 +1,162 @@
|
||||
-----------------------------------------------------------------------------
|
||||
-- Imports and dependencies
|
||||
-----------------------------------------------------------------------------
|
||||
local math = require('math')
|
||||
local os = require('os')
|
||||
local string = require('string')
|
||||
local table = require('table')
|
||||
local nscp = require('nscp')
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Module declaration
|
||||
-----------------------------------------------------------------------------
|
||||
module("test_helper", package.seeall)
|
||||
local valid_chars = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
|
||||
"0","1","2","3","4","5","6","7","8","9","-"}
|
||||
local core = nscp.Core()
|
||||
|
||||
math.randomseed(os.time())
|
||||
|
||||
function random(len) -- args: smallest and largest possible password lengths, inclusive
|
||||
pass = {}
|
||||
for z = 1,len do
|
||||
case = math.random(1,2)
|
||||
a = math.random(1,#valid_chars)
|
||||
if case == 1 then
|
||||
x=string.upper(valid_chars[a])
|
||||
elseif case == 2 then
|
||||
x=string.lower(valid_chars[a])
|
||||
end
|
||||
table.insert(pass, x)
|
||||
end
|
||||
return(table.concat(pass))
|
||||
end
|
||||
string.random = random
|
||||
|
||||
function status_to_int(status)
|
||||
if status == 'ok' then
|
||||
return 0
|
||||
elseif status == 'warn' then
|
||||
return 1
|
||||
elseif status == 'crit' then
|
||||
return 2
|
||||
elseif status == 'unknown' then
|
||||
return 3
|
||||
else
|
||||
core:log('error', "Unknown status: "..status)
|
||||
return 3
|
||||
end
|
||||
end
|
||||
|
||||
TestResult = { status = true; children = {} }
|
||||
function TestResult:new(o)
|
||||
o = o or {}
|
||||
o["children"] = o["children"] or {}
|
||||
if o["status"] == nil then o["status"] = true end
|
||||
setmetatable(o, self)
|
||||
self.__index = self
|
||||
return o
|
||||
|
||||
end
|
||||
function TestResult:add(result)
|
||||
if not result then
|
||||
error("invalid result")
|
||||
end
|
||||
if not result.status then self.status = false end
|
||||
table.insert(self.children,result)
|
||||
end
|
||||
function TestResult:add_message(result, message)
|
||||
table.insert(self.children,TestResult:new{status=result, message=message})
|
||||
end
|
||||
function TestResult:assert_equals(a, b, message)
|
||||
if a==b then
|
||||
table.insert(self.children,TestResult:new{status=true, message=message})
|
||||
else
|
||||
table.insert(self.children,TestResult:new{status=false, message=message..': '..tostring(a)..' != '..tostring(b)})
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function TestResult:print(indent)
|
||||
indent = indent or 0
|
||||
local pad = string.rep(' ', indent)
|
||||
if self.status then
|
||||
core:log("info", pad .. "[OK ] - " .. self.message)
|
||||
else
|
||||
core:log("error", pad .. "[ERR] - " .. self.message)
|
||||
end
|
||||
if # self.children > 0 then
|
||||
for i,v in ipairs(self.children) do v:print(indent+2) end
|
||||
end
|
||||
end
|
||||
|
||||
function TestResult:print_failed(indent)
|
||||
indent = indent or 0
|
||||
local pad = string.rep(' ', indent)
|
||||
if not self.status then
|
||||
core:log("error", pad .. "[ERR] - " .. self.message)
|
||||
end
|
||||
if # self.children > 0 then
|
||||
for i,v in ipairs(self.children) do v:print_failed(indent+2) end
|
||||
end
|
||||
end
|
||||
|
||||
function TestResult:count()
|
||||
local ok = 0
|
||||
local err = 0
|
||||
if self.status then
|
||||
ok = ok + 1
|
||||
else
|
||||
err = err + 1
|
||||
end
|
||||
if # self.children > 0 then
|
||||
for i,v in ipairs(self.children) do
|
||||
local lok, lerr = v:count()
|
||||
ok = ok + lok
|
||||
err = err + lerr
|
||||
end
|
||||
end
|
||||
return ok, err
|
||||
end
|
||||
|
||||
|
||||
|
||||
function TestResult:get_nagios()
|
||||
local ok, err = self:count()
|
||||
if not self.status then
|
||||
return 'crit', tostring(err)..' test cases failed', ''
|
||||
else
|
||||
return 'ok', tostring(ok)..' test cases succeded', ''
|
||||
end
|
||||
end
|
||||
|
||||
local test_cases = {}
|
||||
function install_test_manager(cases)
|
||||
test_cases = cases
|
||||
for i=1,# test_cases do
|
||||
test_cases[i]:install({})
|
||||
end
|
||||
return 'ok'
|
||||
end
|
||||
|
||||
local test_cases = {}
|
||||
function init_test_manager(cases)
|
||||
test_cases = cases
|
||||
local reg = nscp.Registry()
|
||||
reg:simple_query('lua_unittest', lua_unittest_handler, 'TODO')
|
||||
end
|
||||
|
||||
function lua_unittest_handler(command, args)
|
||||
local result = TestResult:new{message='Running testsuite'}
|
||||
for i=1,# test_cases do
|
||||
local case_result = TestResult:new{message='Running testsuite'}
|
||||
test_cases[i]:setup()
|
||||
case_result:add(test_cases[i]:run())
|
||||
test_cases[i]:teardown()
|
||||
result:add(case_result)
|
||||
end
|
||||
result:print()
|
||||
core:log("info", "--//Failed tests//---")
|
||||
result:print_failed()
|
||||
return result:get_nagios()
|
||||
end
|
9
bibliotheque/files/nsclient/scripts/lua/noperf.lua
Normal file
9
bibliotheque/files/nsclient/scripts/lua/noperf.lua
Normal file
@ -0,0 +1,9 @@
|
||||
function no_perf(command, args)
|
||||
local core = nscp.Core()
|
||||
nscp.print ('Uhmmn, 111')
|
||||
code, msg, perf = core:simple_query('check_uptime', args)
|
||||
nscp.print ('Uhmmn, 22')
|
||||
return code, msg, ''
|
||||
end
|
||||
local reg = nscp.Registry()
|
||||
reg:simple_query('check_uptime_no_perf', no_perf, 'Wrapped check-uptime which does not yield performance data')
|
55
bibliotheque/files/nsclient/scripts/lua/test.lua
Normal file
55
bibliotheque/files/nsclient/scripts/lua/test.lua
Normal file
@ -0,0 +1,55 @@
|
||||
nscp.print('Loading test script...')
|
||||
|
||||
v = nscp.getSetting('NSCA Agent', 'interval', 'broken')
|
||||
nscp.print('value: ' .. v)
|
||||
|
||||
function test_func_query(command, args)
|
||||
nscp.print('Inside function (query): ' .. command)
|
||||
return 'ok', 'whoops 001', ''
|
||||
end
|
||||
|
||||
function test_func_exec(command, args)
|
||||
nscp.print('Inside function (exec): ' .. command)
|
||||
return 'ok', 'whoops 002'
|
||||
end
|
||||
|
||||
function test_func_submission(command, args)
|
||||
nscp.print('Inside function (exec): ' .. command)
|
||||
return 'ok'
|
||||
end
|
||||
|
||||
|
||||
nscp.execute('version')
|
||||
|
||||
local reg = Registry()
|
||||
reg:simple_function('lua_test', test_func_query, 'this is a command')
|
||||
reg:simple_cmdline('lua_test', test_func_exec)
|
||||
reg:simple_subscription('lua_test', test_func_submission)
|
||||
|
||||
local settings = Settings()
|
||||
|
||||
str = settings:get_string('/settings/lua/scripts', 'testar', 'FOO BAR')
|
||||
nscp.print('Value: (FOO BAR): ' .. str)
|
||||
settings:set_string('/settings/lua/scripts', 'testar', 'BAR FOO')
|
||||
str = settings:get_string('/settings/lua/scripts', 'testar', 'FOO BAR')
|
||||
nscp.print('Value: (BAR FOO): ' .. str)
|
||||
i = settings:get_int('/settings/lua/scripts', 'testar', 123)
|
||||
nscp.print('Value: (123): ' .. i)
|
||||
settings:set_int('/settings/lua/scripts', 'testar', 456)
|
||||
i = settings:get_int('/settings/lua/scripts', 'testar', 789)
|
||||
nscp.print('Value: (456): ' .. i)
|
||||
|
||||
local core = Core()
|
||||
code, msg, perf = core:simple_query('lua_test', {'a', 'b', 'c'})
|
||||
nscp.print('Value: (query): ' .. code)
|
||||
nscp.print('Value: (query): ' .. msg)
|
||||
nscp.print('Value: (query): ' .. perf)
|
||||
code, msgs = core:simple_exec('*', 'lua_test', {'a', 'b', 'c'})
|
||||
nscp.print('Value: (exec): ' .. code)
|
||||
for msg in pairs(msgs) do
|
||||
nscp.print('Value: (exec): ' .. msg)
|
||||
end
|
||||
code, msg = core:simple_submit('lua_test', 'test_lua', 'ok', 'foo', '')
|
||||
nscp.print('Value: (submit): ' .. code)
|
||||
nscp.print('Value: (submit): ' .. msg)
|
||||
|
311
bibliotheque/files/nsclient/scripts/lua/test_ext_script.lua
Normal file
311
bibliotheque/files/nsclient/scripts/lua/test_ext_script.lua
Normal file
@ -0,0 +1,311 @@
|
||||
test = require("test_helper")
|
||||
|
||||
TestExtScript = {
|
||||
requests = {},
|
||||
responses = {}
|
||||
}
|
||||
function TestExtScript:install(arguments)
|
||||
local conf = nscp.Settings()
|
||||
|
||||
conf:set_string('/modules', 'test_extscripts', 'CheckExternalScripts')
|
||||
conf:set_string('/modules', 'luatest', 'LUAScript')
|
||||
|
||||
conf:set_string('/settings/luatest/scripts', 'test_nrpe', 'test_nrpe.lua')
|
||||
|
||||
conf:set_string('/settings/NRPE/test_nrpe_server', 'port', '15666')
|
||||
conf:set_string('/settings/NRPE/test_nrpe_server', 'inbox', 'nrpe_test_inbox')
|
||||
conf:set_string('/settings/NRPE/test_nrpe_server', 'encryption', '1')
|
||||
|
||||
conf:set_string('/settings/NRPE/test_nrpe_client/targets', 'nrpe_test_local', 'nrpe://127.0.0.1:15666')
|
||||
conf:set_string('/settings/NRPE/test_nrpe_client', 'channel', 'nrpe_test_outbox')
|
||||
--conf:save()
|
||||
end
|
||||
|
||||
function TestExtScript:setup()
|
||||
local reg = nscp.Registry()
|
||||
reg:simple_query('check_py_nrpe_test_s', self, self.simple_handler, 'TODO')
|
||||
--reg:query('check_py_nrpe_test', self, self.handler, 'TODO')
|
||||
end
|
||||
function TestExtScript:teardown()
|
||||
end
|
||||
|
||||
function TestExtScript:uninstall()
|
||||
end
|
||||
|
||||
function TestExtScript:help()
|
||||
end
|
||||
|
||||
function TestExtScript:init(plugin_id)
|
||||
end
|
||||
|
||||
function TestExtScript:shutdown()
|
||||
end
|
||||
|
||||
function TestExtScript:has_response(id)
|
||||
return self.responses[id]
|
||||
end
|
||||
|
||||
function TestExtScript:get_response(id)
|
||||
msg = self.requests[id]
|
||||
if msg == nil then
|
||||
msg = TestMessage
|
||||
msg.uuid=id
|
||||
self.responses[id] = msg
|
||||
return msg
|
||||
end
|
||||
return msg
|
||||
end
|
||||
|
||||
function TestExtScript:set_response(msg)
|
||||
self.responses[msg.uuid] = msg
|
||||
end
|
||||
|
||||
function TestExtScript:del_response(id)
|
||||
self.responses[id] = nil
|
||||
end
|
||||
|
||||
function TestExtScript:get_request(id)
|
||||
msg = self.requests[id]
|
||||
if msg == nil then
|
||||
msg = TestMessage
|
||||
msg.uuid=id
|
||||
self.requests[id] = msg
|
||||
return msg
|
||||
end
|
||||
return msg
|
||||
end
|
||||
|
||||
function TestExtScript:set_request(msg)
|
||||
self.requests[msg.uuid] = msg
|
||||
end
|
||||
|
||||
function TestExtScript:del_request(id)
|
||||
self.requests[id] = nil
|
||||
end
|
||||
|
||||
function TestExtScript:simple_handler(command, args)
|
||||
local core = nscp.Core()
|
||||
msg = self:get_response(args[0])
|
||||
msg.got_simple_response = true
|
||||
self:set_response(msg)
|
||||
message = rmsg.message
|
||||
if args[1] then
|
||||
message = string.rep('x', args[1])
|
||||
end
|
||||
rmsg = self:get_request(args[0])
|
||||
return rmsg.status, message, rmsg.perfdata
|
||||
end
|
||||
|
||||
function TestExtScript:handler(req)
|
||||
local msg = self:get_response(args[0])
|
||||
msg.got_response = true
|
||||
self:set_response(msg)
|
||||
end
|
||||
|
||||
function TestExtScript:submit_payload(tag, ssl, length, payload_length, source, status, message, perf, target)
|
||||
local core = nscp.Core()
|
||||
local result = test.TestResult:new{message='Testing NRPE: '..tag..' for '..target}
|
||||
|
||||
local msg = protobuf.Plugin.QueryRequestMessage.new()
|
||||
hdr = msg:get_header()
|
||||
hdr:set_recipient_id(target)
|
||||
hdr:set_command('nrpe_forward')
|
||||
host = hdr:add_hosts()
|
||||
host:set_address("127.0.0.1:15666")
|
||||
host:set_id(target)
|
||||
if target == 'valid' then
|
||||
else
|
||||
enc = host:add_metadata()
|
||||
enc:set_key("use ssl")
|
||||
enc:set_value(tostring(ssl))
|
||||
enc = host:add_metadata()
|
||||
enc:set_key("payload length")
|
||||
enc:set_value(tostring(length))
|
||||
enc = host:add_metadata()
|
||||
enc:set_key("timeout")
|
||||
enc:set_value('10')
|
||||
end
|
||||
|
||||
uid = string.random(12)
|
||||
payload = msg:add_payload()
|
||||
payload:set_command('check_py_nrpe_test_s')
|
||||
payload:set_arguments(1, uid)
|
||||
if payload_length ~= 0 then
|
||||
payload:set_arguments(2, payload_length)
|
||||
end
|
||||
rmsg = self:get_request(uid)
|
||||
rmsg.status = status
|
||||
rmsg.message = message
|
||||
rmsg.perfdata = perf
|
||||
self:set_request(rmsg)
|
||||
serialized = msg:serialized()
|
||||
result_code, response = core:query(serialized)
|
||||
response_message = protobuf.Plugin.QueryResponseMessage.parsefromstring(response)
|
||||
|
||||
|
||||
found = False
|
||||
for i = 0,10 do
|
||||
if (self:has_response(uid)) then
|
||||
rmsg = self:get_response(uid)
|
||||
--#result.add_message(rmsg.got_response, 'Testing to recieve message using %s'%tag)
|
||||
result:add_message(rmsg.got_simple_response, 'Testing to recieve simple message using '..tag)
|
||||
result:add_message(response_message:size_payload() == 1, 'Verify that we only get one payload response for '..tag)
|
||||
pl = response_message:get_payload(1)
|
||||
result:assert_equals(pl:get_result(), test.status_to_int(status), 'Verify that status is sent through '..tag)
|
||||
if payload_length == 0 then
|
||||
result:assert_equals(pl:get_message(), rmsg.message, 'Verify that message is sent through '..tag)
|
||||
else
|
||||
max_len = payload_length
|
||||
if max_len >= length then
|
||||
max_len = length - 1
|
||||
end
|
||||
result:assert_equals(string.len(pl:get_message()), max_len, 'Verify that message length is correct ' .. max_len .. ': ' ..tag)
|
||||
end
|
||||
--#result.assert_equals(rmsg.perfdata, perf, 'Verify that performance data is sent through')
|
||||
self:del_response(uid)
|
||||
found = true
|
||||
break
|
||||
else
|
||||
core:log('info', string.format('Waiting for %s (%s/%s)', uid,tag,target))
|
||||
nscp.sleep(500)
|
||||
end
|
||||
end
|
||||
if (not found) then
|
||||
result:add_message(false, string.format('Failed to find message %s using %s', uid, tag))
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
function TestExtScript:test_one(ssl, length, payload_length, status)
|
||||
tag = string.format("%s/%d/%s", tostring(ssl), length, status)
|
||||
local result = test.TestResult:new{message=string.format('Testing: %s with various targets', tag)}
|
||||
for k,t in pairs({'valid', 'test_rp', 'invalid'}) do
|
||||
result:add(self:submit_payload(tag, ssl, length, payload_length, tag .. 'src' .. tag, status, tag .. 'msg' .. tag, '', t))
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
function TestExtScript:do_one_test(ssl, length)
|
||||
if ssl == nil then ssl = true end
|
||||
length = length or 1024
|
||||
|
||||
local conf = nscp.Settings()
|
||||
local core = nscp.Core()
|
||||
conf:set_int('/settings/NRPE/test_nrpe_server', 'payload length', length)
|
||||
conf:set_bool('/settings/NRPE/test_nrpe_server', 'use ssl', ssl)
|
||||
conf:set_bool('/settings/NRPE/test_nrpe_server', 'allow arguments', true)
|
||||
core:reload('test_nrpe_server')
|
||||
|
||||
conf:set_string('/settings/NRPE/test_nrpe_client/targets/default', 'address', 'nrpe://127.0.0.1:35666')
|
||||
conf:set_bool('/settings/NRPE/test_nrpe_client/targets/default', 'use ssl', not ssl)
|
||||
conf:set_int('/settings/NRPE/test_nrpe_client/targets/default', 'payload length', length*3)
|
||||
|
||||
conf:set_string('/settings/NRPE/test_nrpe_client/targets/invalid', 'address', 'nrpe://127.0.0.1:25666')
|
||||
conf:set_bool('/settings/NRPE/test_nrpe_client/targets/invalid', 'use ssl', not ssl)
|
||||
conf:set_int('/settings/NRPE/test_nrpe_client/targets/invalid', 'payload length', length*2)
|
||||
|
||||
conf:set_string('/settings/NRPE/test_nrpe_client/targets/valid', 'address', 'nrpe://127.0.0.1:15666')
|
||||
conf:set_bool('/settings/NRPE/test_nrpe_client/targets/valid', 'use ssl', ssl)
|
||||
conf:set_int('/settings/NRPE/test_nrpe_client/targets/valid', 'payload length', length)
|
||||
core:reload('test_nrpe_client')
|
||||
|
||||
local result = test.TestResult:new{message="Testing "..tostring(ssl)..", "..tostring(length)}
|
||||
result:add(self:test_one(ssl, length, 0, 'unknown'))
|
||||
result:add(self:test_one(ssl, length, 0, 'ok'))
|
||||
result:add(self:test_one(ssl, length, 0, 'warn'))
|
||||
result:add(self:test_one(ssl, length, 0, 'crit'))
|
||||
result:add(self:test_one(ssl, length, length/2, 'ok'))
|
||||
result:add(self:test_one(ssl, length, length, 'ok'))
|
||||
result:add(self:test_one(ssl, length, length*2, 'ok'))
|
||||
return result
|
||||
end
|
||||
|
||||
function TestExtScript:test_timeout(ssl, server_timeout, client_timeout, length)
|
||||
|
||||
local conf = nscp.Settings()
|
||||
local core = nscp.Core()
|
||||
conf:set_bool('/settings/NRPE/test_nrpe_server', 'use ssl', ssl)
|
||||
conf:set_int('/settings/NRPE/test_nrpe_server', 'timeout', server_timeout)
|
||||
conf:set_bool('/settings/NRPE/test_nrpe_server', 'allow arguments', true)
|
||||
conf:set_int('/settings/NRPE/test_nrpe_server', 'payload length', length)
|
||||
core:reload('test_nrpe_server')
|
||||
|
||||
conf:set_string('/settings/NRPE/test_nrpe_client/targets/default', 'address', 'nrpe://127.0.0.1:15666')
|
||||
conf:set_bool('/settings/NRPE/test_nrpe_client/targets/default', 'use ssl', ssl)
|
||||
conf:set_int('/settings/NRPE/test_nrpe_client/targets/default', 'timeout', client_timeout)
|
||||
conf:set_int('/settings/NRPE/test_nrpe_client/targets/default', 'payload length', length)
|
||||
|
||||
core:reload('test_nrpe_client')
|
||||
|
||||
local result = test.TestResult:new{message="Testing timeouts ssl: "..tostring(ssl)..", server: "..tostring(server_timeout)..", client: "..tostring(client_timeout)}
|
||||
|
||||
local msg = protobuf.Plugin.QueryRequestMessage.new()
|
||||
hdr = msg:get_header()
|
||||
hdr:set_recipient_id('test')
|
||||
host = hdr:add_hosts()
|
||||
host:set_address("127.0.0.1:15666")
|
||||
host:set_id('test')
|
||||
meta = hdr:add_metadata()
|
||||
meta:set_key("command")
|
||||
meta:set_value('check_py_nrpe_test_s')
|
||||
meta = hdr:add_metadata()
|
||||
meta:set_key("retry")
|
||||
meta:set_value('0')
|
||||
|
||||
uid = string.random(12)
|
||||
payload = msg:add_payload()
|
||||
payload:set_command('nrpe_forward')
|
||||
payload:set_arguments(1, uid)
|
||||
rmsg = self:get_request(uid)
|
||||
rmsg.status = 'ok'
|
||||
rmsg.message = 'Hello: Timeout'
|
||||
rmsg.perfdata = ''
|
||||
self:set_request(rmsg)
|
||||
serialized = msg:serialized()
|
||||
result_code, response = core:query(serialized)
|
||||
response_message = protobuf.Plugin.QueryResponseMessage.parsefromstring(response)
|
||||
|
||||
|
||||
found = False
|
||||
for i = 0,10 do
|
||||
if (self:has_response(uid)) then
|
||||
rmsg = self:get_response(uid)
|
||||
result:add_message(false, string.format('Testing to recieve message using'))
|
||||
self:del_response(uid)
|
||||
found = true
|
||||
break
|
||||
else
|
||||
core:log('error', string.format('Timeout waiting for %s', uid))
|
||||
--sleep(500)
|
||||
end
|
||||
end
|
||||
if (found) then
|
||||
result:add_message(false, string.format('Making sure timeout message was never delivered'))
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
function TestExtScript:run()
|
||||
local result = test.TestResult:new{message="NRPE Test Suite"}
|
||||
result:add(self:do_one_test(true, 1024))
|
||||
result:add(self:do_one_test(false, 1024))
|
||||
result:add(self:do_one_test(true, 4096))
|
||||
result:add(self:do_one_test(true, 65536))
|
||||
result:add(self:do_one_test(true, 1048576))
|
||||
|
||||
result:add(self:test_timeout(false, 30, 1, 1048576000))
|
||||
result:add(self:test_timeout(false, 1, 30, 1048576000))
|
||||
result:add(self:test_timeout(true, 30, 1, 1048576000))
|
||||
result:add(self:test_timeout(true, 1, 30, 1048576000))
|
||||
return result
|
||||
end
|
||||
|
||||
|
||||
instances = { TestNRPE }
|
||||
test.init_test_manager(instances)
|
||||
|
||||
function main(args)
|
||||
return test.install_test_manager(instances)
|
||||
end
|
328
bibliotheque/files/nsclient/scripts/lua/test_nrpe.lua
Normal file
328
bibliotheque/files/nsclient/scripts/lua/test_nrpe.lua
Normal file
@ -0,0 +1,328 @@
|
||||
test = require("test_helper")
|
||||
|
||||
TestMessage = {
|
||||
uuid = nil,
|
||||
source = nil,
|
||||
command = nil,
|
||||
status = nil,
|
||||
message = nil,
|
||||
perfdata = nil,
|
||||
got_simple_response = false,
|
||||
got_response = false
|
||||
}
|
||||
|
||||
TestNRPE = {
|
||||
requests = {},
|
||||
responses = {}
|
||||
}
|
||||
function TestNRPE:install(arguments)
|
||||
local conf = nscp.Settings()
|
||||
|
||||
conf:set_string('/modules', 'test_nrpe_server', 'NRPEServer')
|
||||
conf:set_string('/modules', 'test_nrpe_client', 'NRPEClient')
|
||||
conf:set_string('/modules', 'luatest', 'LUAScript')
|
||||
|
||||
conf:set_string('/settings/luatest/scripts', 'test_nrpe', 'test_nrpe.lua')
|
||||
|
||||
conf:set_string('/settings/NRPE/test_nrpe_server', 'port', '15666')
|
||||
conf:set_string('/settings/NRPE/test_nrpe_server', 'inbox', 'nrpe_test_inbox')
|
||||
conf:set_string('/settings/NRPE/test_nrpe_server', 'encryption', '1')
|
||||
conf:set_string('/settings/NRPE/test_nrpe_server', 'insecure', 'true')
|
||||
|
||||
conf:set_string('/settings/NRPE/test_nrpe_client/targets', 'nrpe_test_local', 'nrpe://127.0.0.1:15666')
|
||||
conf:set_string('/settings/NRPE/test_nrpe_client/targets/default', 'insecure', 'true')
|
||||
end
|
||||
|
||||
function TestNRPE:setup()
|
||||
local reg = nscp.Registry()
|
||||
reg:simple_query('check_py_nrpe_test_s', self, self.simple_handler, 'TODO')
|
||||
--reg:query('check_py_nrpe_test', self, self.handler, 'TODO')
|
||||
end
|
||||
function TestNRPE:teardown()
|
||||
end
|
||||
|
||||
function TestNRPE:uninstall()
|
||||
end
|
||||
|
||||
function TestNRPE:help()
|
||||
end
|
||||
|
||||
function TestNRPE:init(plugin_id)
|
||||
end
|
||||
|
||||
function TestNRPE:shutdown()
|
||||
end
|
||||
|
||||
function TestNRPE:has_response(id)
|
||||
return self.responses[id]
|
||||
end
|
||||
|
||||
function TestNRPE:get_response(id)
|
||||
msg = self.requests[id]
|
||||
if msg == nil then
|
||||
msg = TestMessage
|
||||
msg.uuid=id
|
||||
self.responses[id] = msg
|
||||
return msg
|
||||
end
|
||||
return msg
|
||||
end
|
||||
|
||||
function TestNRPE:set_response(msg)
|
||||
self.responses[msg.uuid] = msg
|
||||
end
|
||||
|
||||
function TestNRPE:del_response(id)
|
||||
self.responses[id] = nil
|
||||
end
|
||||
|
||||
function TestNRPE:get_request(id)
|
||||
msg = self.requests[id]
|
||||
if msg == nil then
|
||||
msg = TestMessage
|
||||
msg.uuid=id
|
||||
self.requests[id] = msg
|
||||
return msg
|
||||
end
|
||||
return msg
|
||||
end
|
||||
|
||||
function TestNRPE:set_request(msg)
|
||||
self.requests[msg.uuid] = msg
|
||||
end
|
||||
|
||||
function TestNRPE:del_request(id)
|
||||
self.requests[id] = nil
|
||||
end
|
||||
|
||||
function TestNRPE:simple_handler(command, args)
|
||||
local core = nscp.Core()
|
||||
msg = self:get_response(args[0])
|
||||
msg.got_simple_response = true
|
||||
self:set_response(msg)
|
||||
message = rmsg.message
|
||||
if args[1] then
|
||||
message = string.rep('x', args[1])
|
||||
end
|
||||
rmsg = self:get_request(args[0])
|
||||
return rmsg.status, message, rmsg.perfdata
|
||||
end
|
||||
|
||||
function TestNRPE:handler(req)
|
||||
local msg = self:get_response(args[0])
|
||||
msg.got_response = true
|
||||
self:set_response(msg)
|
||||
end
|
||||
|
||||
function TestNRPE:submit_payload(tag, ssl, length, payload_length, source, status, message, perf, target)
|
||||
local core = nscp.Core()
|
||||
local result = test.TestResult:new{message='Testing NRPE: '..tag..' for '..target}
|
||||
|
||||
local msg = protobuf.Plugin.QueryRequestMessage.new()
|
||||
hdr = msg:get_header()
|
||||
hdr:set_destination_id(target)
|
||||
hdr:set_command("nrpe_forward")
|
||||
host = hdr:add_hosts()
|
||||
host:set_address("127.0.0.1:15666")
|
||||
host:set_id(target)
|
||||
if target == 'valid' then
|
||||
else
|
||||
enc = host:add_metadata()
|
||||
enc:set_key("use ssl")
|
||||
enc:set_value(tostring(ssl))
|
||||
enc = host:add_metadata()
|
||||
enc:set_key("payload length")
|
||||
enc:set_value(tostring(length))
|
||||
enc = host:add_metadata()
|
||||
enc:set_key("timeout")
|
||||
enc:set_value('10')
|
||||
end
|
||||
|
||||
uid = string.random(12)
|
||||
payload = msg:add_payload()
|
||||
payload:set_command('check_py_nrpe_test_s')
|
||||
payload:set_arguments(1, uid)
|
||||
if payload_length ~= 0 then
|
||||
payload:set_arguments(2, payload_length)
|
||||
end
|
||||
rmsg = self:get_request(uid)
|
||||
rmsg.status = status
|
||||
rmsg.message = message
|
||||
rmsg.perfdata = perf
|
||||
self:set_request(rmsg)
|
||||
serialized = msg:serialized()
|
||||
result_code, response = core:query(serialized)
|
||||
response_message = protobuf.Plugin.QueryResponseMessage.parsefromstring(response)
|
||||
|
||||
|
||||
found = False
|
||||
for i = 0,10 do
|
||||
if (self:has_response(uid)) then
|
||||
rmsg = self:get_response(uid)
|
||||
--#result.add_message(rmsg.got_response, 'Testing to recieve message using %s'%tag)
|
||||
result:add_message(rmsg.got_simple_response, 'Testing to recieve simple message using '..tag)
|
||||
result:add_message(response_message:size_payload() == 1, 'Verify that we only get one payload response for '..tag)
|
||||
pl = response_message:get_payload(1)
|
||||
result:assert_equals(pl:get_result(), test.status_to_int(status), 'Verify that status is sent through '..tag)
|
||||
if payload_length == 0 then
|
||||
l = pl:get_lines(1)
|
||||
result:assert_equals(l:get_message(), rmsg.message, 'Verify that message is sent through '..tag)
|
||||
else
|
||||
max_len = payload_length
|
||||
if max_len >= length then
|
||||
max_len = length - 1
|
||||
end
|
||||
l = pl:get_lines(1)
|
||||
result:assert_equals(string.len(l:get_message()), max_len, 'Verify that message length is correct ' .. max_len .. ': ' ..tag)
|
||||
end
|
||||
--#result.assert_equals(rmsg.perfdata, perf, 'Verify that performance data is sent through')
|
||||
self:del_response(uid)
|
||||
found = true
|
||||
break
|
||||
else
|
||||
core:log('info', string.format('Waiting for %s (%s/%s)', uid,tag,target))
|
||||
nscp.sleep(500)
|
||||
end
|
||||
end
|
||||
if (not found) then
|
||||
result:add_message(false, string.format('Failed to find message %s using %s', uid, tag))
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
function TestNRPE:test_one(ssl, length, payload_length, status)
|
||||
tag = string.format("%s/%d/%s", tostring(ssl), length, status)
|
||||
local result = test.TestResult:new{message=string.format('Testing: %s with various targets', tag)}
|
||||
for k,t in pairs({'valid', 'test_rp', 'invalid'}) do
|
||||
result:add(self:submit_payload(tag, ssl, length, payload_length, tag .. 'src' .. tag, status, tag .. 'msg' .. tag, '', t))
|
||||
end
|
||||
result:add(self:submit_payload(tag, ssl, length, payload_length, tag .. 'src' .. tag, status, tag .. 'msg' .. tag, '', 'valid'))
|
||||
return result
|
||||
end
|
||||
|
||||
function TestNRPE:do_one_test(ssl, length)
|
||||
if ssl == nil then ssl = true end
|
||||
length = length or 1024
|
||||
|
||||
local conf = nscp.Settings()
|
||||
local core = nscp.Core()
|
||||
conf:set_int('/settings/NRPE/test_nrpe_server', 'payload length', length)
|
||||
conf:set_bool('/settings/NRPE/test_nrpe_server', 'use ssl', ssl)
|
||||
conf:set_bool('/settings/NRPE/test_nrpe_server', 'allow arguments', true)
|
||||
conf:set_bool('/settings/NRPE/test_nrpe_server', 'extended response', false)
|
||||
core:reload('test_nrpe_server')
|
||||
|
||||
conf:set_string('/settings/NRPE/test_nrpe_client/targets/default', 'address', 'nrpe://127.0.0.1:35666')
|
||||
conf:set_bool('/settings/NRPE/test_nrpe_client/targets/default', 'ssl', not ssl)
|
||||
conf:set_int('/settings/NRPE/test_nrpe_client/targets/default', 'payload length', length*3)
|
||||
|
||||
conf:set_string('/settings/NRPE/test_nrpe_client/targets/invalid', 'address', 'nrpe://127.0.0.1:25666')
|
||||
conf:set_bool('/settings/NRPE/test_nrpe_client/targets/invalid', 'use ssl', not ssl)
|
||||
conf:set_int('/settings/NRPE/test_nrpe_client/targets/invalid', 'payload length', length*2)
|
||||
|
||||
conf:set_string('/settings/NRPE/test_nrpe_client/targets/valid', 'address', 'nrpe://127.0.0.1:15666')
|
||||
conf:set_bool('/settings/NRPE/test_nrpe_client/targets/valid', 'use ssl', ssl)
|
||||
conf:set_int('/settings/NRPE/test_nrpe_client/targets/valid', 'payload length', length)
|
||||
core:reload('test_nrpe_client')
|
||||
|
||||
local result = test.TestResult:new{message="Testing "..tostring(ssl)..", "..tostring(length)}
|
||||
result:add(self:test_one(ssl, length, 0, 'unknown'))
|
||||
result:add(self:test_one(ssl, length, 0, 'ok'))
|
||||
result:add(self:test_one(ssl, length, 0, 'warn'))
|
||||
result:add(self:test_one(ssl, length, 0, 'crit'))
|
||||
result:add(self:test_one(ssl, length, length/2, 'ok'))
|
||||
result:add(self:test_one(ssl, length, length, 'ok'))
|
||||
result:add(self:test_one(ssl, length, length*2, 'ok'))
|
||||
return result
|
||||
end
|
||||
|
||||
function TestNRPE:test_timeout(ssl, server_timeout, client_timeout, length)
|
||||
|
||||
local conf = nscp.Settings()
|
||||
local core = nscp.Core()
|
||||
conf:set_bool('/settings/NRPE/test_nrpe_server', 'use ssl', ssl)
|
||||
conf:set_int('/settings/NRPE/test_nrpe_server', 'timeout', server_timeout)
|
||||
conf:set_bool('/settings/NRPE/test_nrpe_server', 'allow arguments', true)
|
||||
conf:set_int('/settings/NRPE/test_nrpe_server', 'payload length', length)
|
||||
core:reload('test_nrpe_server')
|
||||
|
||||
conf:set_string('/settings/NRPE/test_nrpe_client/targets/default', 'address', 'nrpe://127.0.0.1:15666')
|
||||
conf:set_bool('/settings/NRPE/test_nrpe_client/targets/default', 'use ssl', ssl)
|
||||
conf:set_int('/settings/NRPE/test_nrpe_client/targets/default', 'timeout', client_timeout)
|
||||
conf:set_int('/settings/NRPE/test_nrpe_client/targets/default', 'payload length', length)
|
||||
|
||||
core:reload('test_nrpe_client')
|
||||
|
||||
local result = test.TestResult:new{message="Testing timeouts ssl: "..tostring(ssl)..", server: "..tostring(server_timeout)..", client: "..tostring(client_timeout)}
|
||||
|
||||
local msg = protobuf.Plugin.QueryRequestMessage.new()
|
||||
hdr = msg:get_header()
|
||||
hdr:set_destination_id('test')
|
||||
host = hdr:add_hosts()
|
||||
host:set_address("127.0.0.1:15666")
|
||||
host:set_id('test')
|
||||
meta = hdr:add_metadata()
|
||||
meta:set_key("command")
|
||||
meta:set_value('check_py_nrpe_test_s')
|
||||
meta = hdr:add_metadata()
|
||||
meta:set_key("retry")
|
||||
meta:set_value('0')
|
||||
|
||||
uid = string.random(12)
|
||||
payload = msg:add_payload()
|
||||
payload:set_command('nrpe_forward')
|
||||
payload:set_arguments(1, uid)
|
||||
rmsg = self:get_request(uid)
|
||||
rmsg.status = 'ok'
|
||||
rmsg.message = 'Hello: Timeout'
|
||||
rmsg.perfdata = ''
|
||||
self:set_request(rmsg)
|
||||
serialized = msg:serialized()
|
||||
result_code, response = core:query(serialized)
|
||||
response_message = protobuf.Plugin.QueryResponseMessage.parsefromstring(response)
|
||||
|
||||
|
||||
found = False
|
||||
for i = 0,10 do
|
||||
if (self:has_response(uid)) then
|
||||
rmsg = self:get_response(uid)
|
||||
result:add_message(false, string.format('Testing to recieve message using'))
|
||||
self:del_response(uid)
|
||||
found = true
|
||||
break
|
||||
else
|
||||
core:log('error', string.format('Timeout waiting for %s', uid))
|
||||
nscp.sleep(500)
|
||||
end
|
||||
end
|
||||
if (found) then
|
||||
result:add_message(false, string.format('Making sure timeout message was never delivered'))
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
function TestNRPE:run()
|
||||
local result = test.TestResult:new{message="NRPE Test Suite"}
|
||||
result:add(self:do_one_test(true, 1024))
|
||||
result:add(self:do_one_test(false, 1024))
|
||||
result:add(self:do_one_test(true, 4096))
|
||||
result:add(self:do_one_test(true, 65536))
|
||||
result:add(self:do_one_test(true, 1048576))
|
||||
|
||||
result:add(self:test_timeout(false, 30, 1, 104857600))
|
||||
result:add(self:test_timeout(false, 1, 30, 104857600))
|
||||
result:add(self:test_timeout(true, 30, 1, 104857600))
|
||||
result:add(self:test_timeout(true, 1, 30, 104857600))
|
||||
nscp.sleep(500)
|
||||
return result
|
||||
end
|
||||
|
||||
|
||||
instances = { TestNRPE }
|
||||
test.init_test_manager(instances)
|
||||
|
||||
function main(args)
|
||||
return test.install_test_manager(instances)
|
||||
end
|
Reference in New Issue
Block a user