Truncate Nagios plugin output to 1024 bytes max

It appears that Nagios cannot ingest output which is larger than 1024
bytes so this change makes sure that the Nagios encoder complies with
this requirement.

Change-Id: I22c7186f0dc6edabe8c3372a8c06197b276a9d4d
Closes-Bug: #1517917
(cherry picked from commit ca031a41aa)
This commit is contained in:
Simon Pasquier 2015-11-19 16:55:25 +01:00
parent 91b5286bad
commit a53053b570
3 changed files with 54 additions and 1 deletions

View File

@ -214,4 +214,25 @@ function chomp(s)
return string.gsub(s, "\n$", "")
end
function truncate(str, max_length, delimiter)
if string.len(str) <= max_length then
return str
end
local pos = 1
while true do
local next_pos1, next_pos2 = string.find(str, delimiter, pos)
if not next_pos1 or next_pos1 - 1 > max_length then
pos = pos - string.len(delimiter) - 1
if pos < 1 then
pos = max_length
end
break
end
pos = next_pos2 + 1
end
return string.sub(str, 1, pos)
end
return M

View File

@ -71,7 +71,9 @@ function process_message()
details[#details+1] = alarm
end
end
data['plugin_output'] = table.concat(details, nagios_break_line)
-- Nagios cannot accept 'plugin_output' parameter greater than 1024 bytes
-- See bug #1517917 for details
data['plugin_output'] = lma.truncate(table.concat(details, nagios_break_line), 1024, nagios_break_line)
local params = {}
for k, v in pairs(data) do

View File

@ -60,6 +60,36 @@ TestLmaUtils = {}
assert(msg:match(': fail'))
end
function TestLmaUtils:test_truncate_with_small_string()
local ret = lma_utils.truncate('foo', 10, '<BR/>')
assertEquals(ret, 'foo')
end
function TestLmaUtils:test_truncate_with_large_string()
local ret = lma_utils.truncate('foo and long string', 10, '<BR/>')
assertEquals(ret, 'foo and lo')
end
function TestLmaUtils:test_truncate_with_one_delimiter()
local ret = lma_utils.truncate('foo<BR/>longstring', 10, '<BR/>')
assertEquals(ret, 'foo')
end
function TestLmaUtils:test_truncate_with_several_delimiters_1()
local ret = lma_utils.truncate('foo<BR/>bar<BR/>longstring', 10, '<BR/>')
assertEquals(ret, 'foo')
end
function TestLmaUtils:test_truncate_with_several_delimiters_2()
local ret = lma_utils.truncate('foo<BR/>ba<BR/>longstring', 10, '<BR/>')
assertEquals(ret, 'foo<BR/>ba')
end
function TestLmaUtils:test_truncate_with_several_delimiters_3()
local ret = lma_utils.truncate('foo<BR/>ba<BR/>long<BR/>string', 12, '<BR/>')
assertEquals(ret, 'foo<BR/>ba')
end
lu = LuaUnit
lu:setVerbosity( 1 )
os.exit( lu:run() )