plugin script output length to 512kB
Message-ID: <51fa4f84.idH2x7POOf2udWwr%ab(a)mathias-kettner.de>
User-Agent: Heirloom mailx 12.4 7/29/08
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Module: check_mk
Branch: master
Commit: 8ca183b0d9f08a9420550b38759aa43094269f4e
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=8ca183b0d9f08a…
Author: Andreas Boesl <ab(a)mathias-kettner.de>
Date: Thu Aug 1 14:07:29 2013 +0200
windows_agent: increased maximum local/plugin script output length to 512kB
---
ChangeLog | 1 +
agents/windows/check_mk_agent.cc | 42 +++++++++++++++++++-------------------
2 files changed, 22 insertions(+), 21 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index a179514..2bb1839 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -61,6 +61,7 @@
* windows_agent: now starts local/plugin scripts in separate threads/processes
new script parameters cache_age, retry_count, timeout
new script caching options "off", "async",
"sync"
+ * windows_agent: increased maximum local/plugin script output length to 512kB
Notifications:
* notify.py: Matching service level: Use the hosts service level if a
diff --git a/agents/windows/check_mk_agent.cc b/agents/windows/check_mk_agent.cc
index 16773bf..d585675 100755
--- a/agents/windows/check_mk_agent.cc
+++ b/agents/windows/check_mk_agent.cc
@@ -103,7 +103,7 @@
// Maximum heap buffer for a single local/plugin script
// This buffer contains the check output
-#define SCRIPT_BUFFER_HEAP 524288
+#define SCRIPT_BUFFER_HEAP 524288L
// Maximum timeout for a single local/plugin script
#define DEFAULT_PLUGIN_TIMEOUT 60
@@ -2211,8 +2211,7 @@ int launch_program(script_container* cont)
{
int exit_code = 0;
int out_offset = 0;
- char buf[1024]; // i/o buffer
- unsigned int buf_size = sizeof(buf);
+ char buf[16635]; // i/o buffer
STARTUPINFO si;
SECURITY_ATTRIBUTES sa;
@@ -2265,37 +2264,38 @@ int launch_program(script_container* cont)
memset(buf, 0, sizeof(buf));
time_t process_start = time(0);
+ bool buffer_full = false;
+
for(;;)
{
if (cont->should_terminate || time(0) - process_start > cont->timeout){
exit_code = 2;
break;
}
- GetExitCodeProcess(pi.hProcess,&exit); // while the process is running
- PeekNamedPipe(read_stdout,buf,buf_size - 1,&bread,&avail,NULL);
- // check to see if there is any data to read from stdout
- if (bread != 0) {
- memset(buf, 0, sizeof(buf));
- if (avail > buf_size - 1) {
- while (bread >= buf_size - 1) {
- ReadFile(read_stdout,buf,buf_size - 1,&bread,NULL); // read the
stdout pipe
- out_offset += snprintf(cont->buffer_work + out_offset,
SCRIPT_BUFFER_HEAP - out_offset, buf);
- memset(buf, 0, sizeof(buf));
- }
+ GetExitCodeProcess(pi.hProcess, &exit); // while the process is running
+ while (!buffer_full) {
+ PeekNamedPipe(read_stdout, buf, sizeof(buf), &bread, &avail, NULL);
+ if (avail == 0)
+ break;
+
+ if (out_offset + bread > SCRIPT_BUFFER_HEAP) {
+ buffer_full = true;
+ break;
}
- else {
- ReadFile(read_stdout,buf,buf_size - 1,&bread,NULL);
+
+ if (bread > 0) {
+ memset(buf, 0, sizeof(buf));
+ ReadFile(read_stdout, buf, sizeof(buf), &bread, NULL);
out_offset += snprintf(cont->buffer_work + out_offset,
SCRIPT_BUFFER_HEAP - out_offset, buf);
}
}
-
- if (out_offset >= SCRIPT_BUFFER_HEAP)
+ if (buffer_full)
break;
if (exit != STILL_ACTIVE)
break;
- Sleep(50);
+ Sleep(10);
}
TerminateJobObject(cont->job_object, exit_code);
@@ -2312,7 +2312,7 @@ int launch_program(script_container* cont)
DWORD WINAPI ScriptWorkerThread(LPVOID lpParam)
{
script_container* cont = (script_container*) lpParam;
- cont->buffer_work = (char*) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
SCRIPT_BUFFER_HEAP + 1);
+ cont->buffer_work = (char*) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
SCRIPT_BUFFER_HEAP + 1 );
// Execute script
int result = launch_program(cont);
@@ -3574,7 +3574,7 @@ void listen_tcp_loop()
void output(SOCKET &out, const char *format, ...)
{
- static char outbuffer[16384];
+ static char outbuffer[SCRIPT_BUFFER_HEAP];
static int len = 0;
va_list ap;
va_start(ap, format);