Browse Source

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)
Simon Pasquier 3 years ago
parent
commit
a53053b570

+ 21
- 0
deployment_scripts/puppet/modules/lma_collector/files/plugins/common/lma_utils.lua View File

@@ -214,4 +214,25 @@ function chomp(s)
214 214
     return string.gsub(s, "\n$", "")
215 215
 end
216 216
 
217
+function truncate(str, max_length, delimiter)
218
+    if string.len(str) <= max_length then
219
+        return str
220
+    end
221
+
222
+    local pos = 1
223
+    while true do
224
+        local next_pos1, next_pos2 = string.find(str, delimiter, pos)
225
+        if not next_pos1 or next_pos1 - 1 > max_length then
226
+            pos = pos - string.len(delimiter) - 1
227
+            if pos < 1 then
228
+                pos = max_length
229
+            end
230
+            break
231
+        end
232
+        pos = next_pos2 + 1
233
+    end
234
+
235
+    return string.sub(str, 1, pos)
236
+end
237
+
217 238
 return M

+ 3
- 1
deployment_scripts/puppet/modules/lma_collector/files/plugins/encoders/status_nagios.lua View File

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

+ 30
- 0
deployment_scripts/puppet/modules/lma_collector/tests/lua/test_lma_utils.lua View File

@@ -60,6 +60,36 @@ TestLmaUtils = {}
60 60
         assert(msg:match(': fail'))
61 61
     end
62 62
 
63
+    function TestLmaUtils:test_truncate_with_small_string()
64
+        local ret = lma_utils.truncate('foo', 10, '<BR/>')
65
+        assertEquals(ret, 'foo')
66
+    end
67
+
68
+    function TestLmaUtils:test_truncate_with_large_string()
69
+        local ret = lma_utils.truncate('foo and long string', 10, '<BR/>')
70
+        assertEquals(ret, 'foo and lo')
71
+    end
72
+
73
+    function TestLmaUtils:test_truncate_with_one_delimiter()
74
+        local ret = lma_utils.truncate('foo<BR/>longstring', 10, '<BR/>')
75
+        assertEquals(ret, 'foo')
76
+    end
77
+
78
+    function TestLmaUtils:test_truncate_with_several_delimiters_1()
79
+        local ret = lma_utils.truncate('foo<BR/>bar<BR/>longstring', 10, '<BR/>')
80
+        assertEquals(ret, 'foo')
81
+    end
82
+
83
+    function TestLmaUtils:test_truncate_with_several_delimiters_2()
84
+        local ret = lma_utils.truncate('foo<BR/>ba<BR/>longstring', 10, '<BR/>')
85
+        assertEquals(ret, 'foo<BR/>ba')
86
+    end
87
+
88
+    function TestLmaUtils:test_truncate_with_several_delimiters_3()
89
+        local ret = lma_utils.truncate('foo<BR/>ba<BR/>long<BR/>string', 12, '<BR/>')
90
+        assertEquals(ret, 'foo<BR/>ba')
91
+    end
92
+
63 93
 lu = LuaUnit
64 94
 lu:setVerbosity( 1 )
65 95
 os.exit( lu:run() )

Loading…
Cancel
Save