Module: check_mk
Branch: master
Commit: 6af35316583ea916b0a9bace595fbe66e2ed208a
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=6af35316583ea9…
Author: Jukka Aro <ja(a)mathias-kettner.de>
Date: Tue Feb 27 14:01:24 2018 +0100
Win-agent: split SectionEventlog::produceOutputInner
Extract smaller subfunctions to improve readability.
---
agents/windows/build_version | 2 +-
agents/windows/sections/SectionEventlog.cc | 71 ++++++++++++++++--------------
agents/windows/sections/SectionEventlog.h | 4 +-
3 files changed, 41 insertions(+), 36 deletions(-)
diff --git a/agents/windows/build_version b/agents/windows/build_version
index b345efa..179b5d3 100644
--- a/agents/windows/build_version
+++ b/agents/windows/build_version
@@ -1 +1 @@
-3124
+3126
diff --git a/agents/windows/sections/SectionEventlog.cc
b/agents/windows/sections/SectionEventlog.cc
index 1ea33ed..dccbe5e 100644
--- a/agents/windows/sections/SectionEventlog.cc
+++ b/agents/windows/sections/SectionEventlog.cc
@@ -75,7 +75,7 @@ const char *level_name(int level_id) {
// The int return value is there just for convenience, actually we are not
// interested in the state int value at this point any more.
int outputEventlogRecord(std::ostream &out, const IEventLogRecord &event,
- int level, int hide_context) {
+ int level, bool hide_context) {
char type_char;
int this_state;
std::tie(type_char, this_state) = getEventState(event, level);
@@ -236,7 +236,7 @@ void SectionEventlog::saveEventlogOffsets(const std::string
&statefile) {
uint64_t SectionEventlog::outputEventlog(std::ostream &out, const char *logname,
uint64_t previouslyReadId, int level,
- int hideContext) {
+ bool hideContext) {
Debug(_logger) << " - event log \"" << logname <<
"\":";
try {
@@ -354,6 +354,36 @@ void SectionEventlog::postprocessConfig() {
loadEventlogOffsets(_env.eventlogStatefile());
}
+void SectionEventlog::readHintOffsets() {
+ // Special handling on startup (first_run)
+ // The last processed record number of each eventlog is stored in the
+ // file eventstate.txt
+ if (_first_run && !*_send_initial) {
+ for (auto &state : _state) {
+ auto it = std::find_if(
+ _hints.cbegin(), _hints.cend(),
+ [&state](const auto &hint) { return state.name == hint.name; });
+ // If there is no entry for the given eventlog we start at the
+ // end
+ state.record_no =
+ it != _hints.cend() ? it->record_no : uint64limits::max();
+ }
+ }
+}
+
+std::pair<int, bool> SectionEventlog::readConfig(
+ const eventlog_file_state &state) const {
+ // Get the configuration of that log file (which messages to
+ // send)
+ auto it = std::find_if(
+ _config->cbegin(), _config->cend(), [&state](const auto &config) {
+ return config.name == "*" || ci_equal(config.name, state.name);
+ });
+ return it != _config->cend()
+ ? std::make_pair(it->level, it->hide_context != 0)
+ : std::make_pair(1, false);
+}
+
// The output of this section is compatible with
// the logwatch agent for Linux and UNIX
bool SectionEventlog::produceOutputInner(std::ostream &out) {
@@ -365,47 +395,20 @@ bool SectionEventlog::produceOutputInner(std::ostream &out) {
// not been processed.
if (find_eventlogs(out)) {
- // Special handling on startup (first_run)
- // The last processed record number of each eventlog is stored in the
- // file eventstate.txt
- if (_first_run && !*_send_initial) {
- for (auto &state : _state) {
- bool found_hint = false;
- for (const auto &hint : _hints) {
- if (state.name == hint.name) {
- state.record_no = hint.record_no;
- found_hint = true;
- break;
- }
- }
- // If there is no entry for the given eventlog we start at the
- // end
- if (!found_hint) {
- state.record_no = uint64limits::max();
- }
- }
- }
+ readHintOffsets();
for (auto &state : _state) {
if (!state.newly_discovered) // not here any more!
out << "[[[" << state.name <<
":missing]]]\n";
else {
- // Get the configuration of that log file (which messages to
- // send)
int level = 1;
- int hide_context = 0;
- for (const eventlog_config_entry &config : *_config) {
- if ((config.name == "*") ||
- ci_equal(config.name, state.name)) {
- level = config.level;
- hide_context = config.hide_context;
- break;
- }
- }
+ bool hideContext = false;
+ std::tie(level, hideContext) = readConfig(state);
+
if (level != -1) {
state.record_no =
outputEventlog(out, state.name.c_str(), state.record_no,
- level, hide_context);
+ level, hideContext);
}
}
}
diff --git a/agents/windows/sections/SectionEventlog.h
b/agents/windows/sections/SectionEventlog.h
index f2ae38b..ce138b7 100644
--- a/agents/windows/sections/SectionEventlog.h
+++ b/agents/windows/sections/SectionEventlog.h
@@ -120,11 +120,13 @@ protected:
private:
uint64_t outputEventlog(std::ostream &out, const char *logname,
uint64_t previouslyReadId, int level,
- int hideContext);
+ bool hideContext);
void registerEventlog(const char *logname);
bool find_eventlogs(std::ostream &out);
void loadEventlogOffsets(const std::string &statefile);
void saveEventlogOffsets(const std::string &statefile);
+ void readHintOffsets();
+ std::pair<int, bool> readConfig(const eventlog_file_state &state) const;
Configurable<bool> _send_initial;
Configurable<bool> _vista_api;