Module: check_mk
Branch: master
Commit: d469af8664f1d6a7ac902804ab39128e47ffb490
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=d469af8664f1d6…
Author: Jukka Aro <ja(a)mathias-kettner.de>
Date: Wed Jan 10 12:43:10 2018 +0100
Windows agent: reduce logfile hint fields
Logfile hints are used to store offset information read from
logstate.txt. The hints were using the struct logwatch_textfile that
contains extra fields for this purpose and forced the use of a bogus
vector for patterns that never exist, among others. Reduce the number
of fields by introducing a reduced version of the struct
(logwatch_hint) that only contains the necessary fields.
Change-Id: I135d97616e90d80ea9685eb5ae2b6b406fb5ddcb
---
agents/windows/build_version | 2 +-
agents/windows/sections/SectionLogwatch.cc | 28 ++++++--------
agents/windows/sections/SectionLogwatch.h | 43 +++++++++++++++++-----
.../windows/test/sections/SectionLogwatchTest.cc | 25 ++++---------
4 files changed, 54 insertions(+), 44 deletions(-)
diff --git a/agents/windows/build_version b/agents/windows/build_version
index b5a790b..ee6ce07 100644
--- a/agents/windows/build_version
+++ b/agents/windows/build_version
@@ -1 +1 @@
-3052
+3054
diff --git a/agents/windows/sections/SectionLogwatch.cc
b/agents/windows/sections/SectionLogwatch.cc
index 1dc5bae..c399b13 100644
--- a/agents/windows/sections/SectionLogwatch.cc
+++ b/agents/windows/sections/SectionLogwatch.cc
@@ -116,7 +116,7 @@ void addConditionPattern(globline_container &globline, const char
*state,
} // namespace
-logwatch_textfile parseLogwatchStateLine(const std::string &line) {
+logwatch_hint parseLogwatchStateLine(const std::string &line) {
/* Example: line = "M://log1.log|98374598374|0|16"; */
const auto tokens = tokenize(line, "\\|");
@@ -127,14 +127,9 @@ logwatch_textfile parseLogwatchStateLine(const std::string &line)
{
}
try {
- return {tokens[0],
- std::move(std::vector<std::string>{tokens[0]}),
- std::stoull(tokens[1]),
- std::stoull(tokens[2]),
- std::stoull(tokens[3]),
- false,
- false,
- condition_patterns_t()};
+ return {tokens[0], std::move(std::vector<std::string>{tokens[0]}),
+ std::stoull(tokens[1]), std::stoull(tokens[2]),
+ std::stoull(tokens[3])};
} catch (const std::invalid_argument &) {
throw StateParseError{std::string("Invalid state line: ") + line};
}
@@ -571,10 +566,10 @@ logwatch_textfile &SectionLogwatch::addNewLogwatchTextfile(
}
const auto cend = _hints.cend();
- const auto it = std::find_if(
- _hints.cbegin(), cend, [full_filename](const logwatch_textfile &hint) {
- return hint.paths.front() == full_filename;
- });
+ const auto it = std::find_if(_hints.cbegin(), cend,
+ [full_filename](const logwatch_hint &hint) {
+ return hint.paths.front() == full_filename;
+ });
bool found_hint = it != cend;
// previously the file size was taken from the hint file. Why is the file
@@ -677,10 +672,9 @@ logwatch_textfile &SectionLogwatch::addNewRotatedLogfile(
assert(filenames.size() > 0);
const auto cend = _hints.cend();
- auto hint_iter = std::find_if(_hints.cbegin(), _hints.cend(),
- [&pattern](const logwatch_textfile &hint) {
- return hint.name == pattern;
- });
+ auto hint_iter = std::find_if(
+ _hints.cbegin(), _hints.cend(),
+ [&pattern](const logwatch_hint &hint) { return hint.name == pattern; });
bool found_hint = hint_iter != cend;
std::vector<std::string> paths{filenames};
diff --git a/agents/windows/sections/SectionLogwatch.h
b/agents/windows/sections/SectionLogwatch.h
index c873b9d..2c2d87f 100644
--- a/agents/windows/sections/SectionLogwatch.h
+++ b/agents/windows/sections/SectionLogwatch.h
@@ -66,10 +66,10 @@ struct logwatch_textfile {
, nocontext(nocontext_)
, rotated(rotated_)
, patterns(std::ref(patterns_)) {}
- logwatch_textfile(const logwatch_textfile&) = delete;
- logwatch_textfile &operator=(const logwatch_textfile&) = delete;
- logwatch_textfile(logwatch_textfile&&) = default;
- logwatch_textfile &operator=(logwatch_textfile&&) = default;
+ logwatch_textfile(const logwatch_textfile &) = delete;
+ logwatch_textfile &operator=(const logwatch_textfile &) = delete;
+ logwatch_textfile(logwatch_textfile &&) = default;
+ logwatch_textfile &operator=(logwatch_textfile &&) = default;
std::string name; // name used for section headers. this is the
// filename for regular logs and the pattern
@@ -86,6 +86,32 @@ struct logwatch_textfile {
patterns; // glob patterns applying for this file
};
+// A hint instance containing information about the stored offsets for a
+// monitored logfile.
+struct logwatch_hint {
+ logwatch_hint(const std::string &name_,
+ const std::vector<std::string> &paths_,
+ unsigned long long file_id_, unsigned long long file_size_,
+ unsigned long long offset_)
+ : name(name_)
+ , paths(paths_)
+ , file_id(file_id_)
+ , file_size(file_size_)
+ , offset(offset_) {}
+ logwatch_hint(const logwatch_hint &) = delete;
+ logwatch_hint &operator=(const logwatch_hint &) = delete;
+ logwatch_hint(logwatch_hint &&) = default;
+ logwatch_hint &operator=(logwatch_hint &&) = default;
+
+ std::string name; // name used for section headers. this is the
+ // filename for regular logs and the pattern
+ // for rotated logs
+ std::vector<std::string> paths;
+ unsigned long long file_id; // used to detect if a file has been replaced
+ unsigned long long file_size; // size of the file
+ unsigned long long offset{0}; // current fseek offset in the file
+};
+
// Single element of a globline:
// C:/tmp/Testfile*.log
struct glob_token {
@@ -115,9 +141,8 @@ globline_container from_string<globline_container>(const
WinApiAdaptor &winapi,
inline std::ostream &operator<<(std::ostream &os, const globline_container
&g) {
os << "\n[tokens]\n";
for (const auto &token : g.tokens) {
- os << "<pattern: " << token.pattern
- << ", nocontext: " << std::boolalpha <<
token.nocontext
- << ", from_start: " << token.from_start
+ os << "<pattern: " << token.pattern << ",
nocontext: " << std::boolalpha
+ << token.nocontext << ", from_start: " <<
token.from_start
<< ", rotated: " << token.rotated
<< ", found_match: " << token.found_match <<
">\n";
}
@@ -156,7 +181,7 @@ public:
}
};
-logwatch_textfile parseLogwatchStateLine(const std::string &line);
+logwatch_hint parseLogwatchStateLine(const std::string &line);
class SectionLogwatch : public Section {
struct ProcessTextfileResponse {
@@ -238,7 +263,7 @@ private:
GlobListConfigurable _globlines;
std::vector<logwatch_textfile> _textfiles;
- std::vector<logwatch_textfile> _hints;
+ std::vector<logwatch_hint> _hints;
bool _offsets_loaded{false};
bool _initialised{false};
};
diff --git a/agents/windows/test/sections/SectionLogwatchTest.cc
b/agents/windows/test/sections/SectionLogwatchTest.cc
index 03b4f42..19a0d5a 100644
--- a/agents/windows/test/sections/SectionLogwatchTest.cc
+++ b/agents/windows/test/sections/SectionLogwatchTest.cc
@@ -12,12 +12,10 @@ bool operator==(const condition_pattern &c1, const
condition_pattern &c2) {
return c1.state == c2.state && c1.glob_pattern == c2.glob_pattern;
}
-bool operator==(const logwatch_textfile &t1, const logwatch_textfile &t2) {
+bool operator==(const logwatch_hint &t1, const logwatch_hint &t2) {
return t1.name == t2.name && t1.paths == t2.paths &&
t1.file_id == t2.file_id && t1.file_size == t2.file_size &&
- t1.offset == t2.offset && t1.missing == t2.missing &&
- t1.nocontext == t2.nocontext && t1.rotated == t2.rotated &&
- t1.encoding == t2.encoding && t1.patterns.get() == t2.patterns.get();
+ t1.offset == t2.offset;
}
bool operator==(const glob_token &t1, const glob_token &t2) {
@@ -32,14 +30,9 @@ bool operator==(const globline_container &g1, const
globline_container &g2) {
TEST_F(wa_SectionLogwatchTest, parseLogwatchStateLine_valid) {
char line[] = "M:\\log1.log|98374598374|0|16";
- const logwatch_textfile expected{"M:\\log1.log",
-
std::vector<std::string>{"M:\\log1.log"},
- 98374598374,
- 0,
- 16,
- false,
- false,
- condition_patterns_t()};
+ const logwatch_hint expected{"M:\\log1.log",
+
std::vector<std::string>{"M:\\log1.log"},
+ 98374598374, 0, 16};
ASSERT_EQ(expected, parseLogwatchStateLine(line));
}
@@ -71,11 +64,9 @@ TEST_F(wa_SectionLogwatchTest,
parseLogwatchStateLine_invalid_separator) {
TEST_F(wa_SectionLogwatchTest, parseLogwatchStateLine_negative) {
char line[] = "M:\\log1.log|-1|-1|-1";
const auto maxValue = std::numeric_limits<unsigned long long>::max();
- const logwatch_textfile expected{
- "M:\\log1.log",
std::vector<std::string>{"M:\\log1.log"},
- maxValue, maxValue,
- maxValue, false,
- false, condition_patterns_t()};
+ const logwatch_hint expected{"M:\\log1.log",
+
std::vector<std::string>{"M:\\log1.log"},
+ maxValue, maxValue, maxValue};
ASSERT_EQ(expected, parseLogwatchStateLine(line));
}