Module: check_mk
Branch: master
Commit: 662af878e1381595e2727cd54c82283f691dde22
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=662af878e13815…
Author: Jukka Aro <ja(a)mathias-kettner.de>
Date: Fri Nov 24 14:35:30 2017 +0100
5412 FIX Windows agent: fix logfile glob patterns without '*' at the end
Windows logfile patterns have required a wildcard '*' at the end in
order to work correctly. It was not possible to require an exact match
to the end of a line without wildcard '*' at the end. The reason was a
carriage-return (CR) character at the end of each read line that
prevented a match unless a '*' was present at the end of the pattern.
Now also patterns without a wildcard '*' at the end work correctly.
Change-Id: I576b6302e333bfc6eb2014bcc759caa0e17eeb0f
---
.werks/5412 | 16 ++++++++++++++++
agents/windows/build_version | 2 +-
agents/windows/sections/SectionLogwatch.cc | 23 +++++++++++++++++------
3 files changed, 34 insertions(+), 7 deletions(-)
diff --git a/.werks/5412 b/.werks/5412
new file mode 100644
index 0000000..c072659
--- /dev/null
+++ b/.werks/5412
@@ -0,0 +1,16 @@
+Title: Windows agent: fix logfile glob patterns without '*' at the end
+Level: 1
+Component: checks
+Compatible: incomp
+Edition: cre
+Version: 1.5.0i2
+Date: 1511530237
+Class: fix
+
+Windows logfile patterns have required a wildcard '*' at the end in
+order to work correctly. It was not possible to require an exact match
+to the end of a line without wildcard '*' at the end. The reason was a
+carriage-return (CR) character at the end of each read line that
+prevented a match unless a '*' was present at the end of the pattern.
+
+Now also patterns without a wildcard '*' at the end work correctly.
diff --git a/agents/windows/build_version b/agents/windows/build_version
index 533bca3..eab009b 100644
--- a/agents/windows/build_version
+++ b/agents/windows/build_version
@@ -1 +1 @@
-3004
+3006
diff --git a/agents/windows/sections/SectionLogwatch.cc
b/agents/windows/sections/SectionLogwatch.cc
index 7e9867b..f179114 100644
--- a/agents/windows/sections/SectionLogwatch.cc
+++ b/agents/windows/sections/SectionLogwatch.cc
@@ -216,17 +216,19 @@ void SectionLogwatch::saveOffsets(const std::string
&logwatch_statefile) {
}
}
+namespace {
+
// Process content of the given textfile
// Can be called in dry-run mode (write_output = false). This tries to detect
// CRIT or WARN patterns
// If write_output is set to true any data found is written to the out socket
-int fill_unicode_bytebuffer(FILE *file, char *buffer, int offset) {
+inline int fill_unicode_bytebuffer(FILE *file, char *buffer, int offset) {
int bytes_to_read = UNICODE_BUFFER_SIZE - offset;
int read_bytes = fread(buffer + offset, 1, bytes_to_read, file);
return read_bytes + offset;
}
-int find_crnl_end(char *buffer) {
+inline int find_crnl_end(char *buffer) {
for (size_t index = 0; index < UNICODE_BUFFER_SIZE; index += 2) {
if (buffer[index] == 0x0d && index < UNICODE_BUFFER_SIZE - 2
&&
buffer[index + 2] == 0x0a)
@@ -235,6 +237,14 @@ int find_crnl_end(char *buffer) {
return -1;
}
+inline void rtrim(std::string &s) {
+ s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) {
+ return !std::isspace(ch);
+ }).base(), s.end());
+}
+
+} //namespace
+
SectionLogwatch::ProcessTextfileResponse
SectionLogwatch::processTextfileUnicode(FILE *file, logwatch_textfile *textfile,
std::ostream &out, bool write_output) {
@@ -276,6 +286,7 @@ SectionLogwatch::processTextfileUnicode(FILE *file, logwatch_textfile
*textfile,
// Argh! The wstring needs to be cut at CRLF as it may go on beyond that
unicode_wstr.resize(buffer_size);
std::string output_buffer = to_utf8(unicode_wstr);
+ rtrim(output_buffer);
Debug(_logger)
<< "SectionLogwatch::processTextfileUnicode, output_buffer:
"
<< output_buffer;
@@ -344,12 +355,12 @@ SectionLogwatch::processTextfileDefault(FILE *file,
logwatch_textfile *textfile,
while (!feof(file)) {
if (!fgets(line, sizeof(line), file)) break;
-
- if (line[strlen(line) - 1] == '\n') line[strlen(line) - 1] = 0;
+ std::string lineString(line);
+ rtrim(lineString);
char state = '.';
for (condition_pattern *pattern : *textfile->patterns) {
- if (globmatch(pattern->glob_pattern, line)) {
+ if (globmatch(pattern->glob_pattern, lineString.c_str())) {
if (!write_output &&
(pattern->state == 'C' || pattern->state == 'W'
||
pattern->state == 'O')) {
@@ -364,7 +375,7 @@ SectionLogwatch::processTextfileDefault(FILE *file, logwatch_textfile
*textfile,
if (write_output && strlen(line) > 0 &&
!(textfile->nocontext && (state == 'I' || state ==
'.')))
- out << state << " " << line <<
"\n";
+ out << state << " " << lineString <<
"\n";
}
response.found_match = false;