Module: check_mk
Branch: master
Commit: 730e3a3f20c566783c4d2d573c9aa77925168cb1
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=730e3a3f20c566…
Author: Jukka Aro <ja(a)mathias-kettner.de>
Date: Tue Feb 27 15:39:30 2018 +0100
Win-agent: split SectionEventlog::outputEventlog
Opening eventlog does not belong to outputting it, extract it to its own
function. Reduce overall complexity by splitting functions.
---
agents/windows/build_version | 2 +-
agents/windows/sections/SectionEventlog.cc | 92 ++++++++++++++++--------------
agents/windows/sections/SectionEventlog.h | 4 +-
3 files changed, 52 insertions(+), 46 deletions(-)
diff --git a/agents/windows/build_version b/agents/windows/build_version
index 30aafae..1409ef4 100644
--- a/agents/windows/build_version
+++ b/agents/windows/build_version
@@ -1 +1 @@
-3130
+3132
diff --git a/agents/windows/sections/SectionEventlog.cc
b/agents/windows/sections/SectionEventlog.cc
index aaee8c0..61722f2 100644
--- a/agents/windows/sections/SectionEventlog.cc
+++ b/agents/windows/sections/SectionEventlog.cc
@@ -220,52 +220,36 @@ void SectionEventlog::saveEventlogOffsets(const std::string
&statefile) {
}
}
-uint64_t SectionEventlog::outputEventlog(std::ostream &out, const char *logname,
+uint64_t SectionEventlog::outputEventlog(std::ostream &out, IEventLog &log,
uint64_t previouslyReadId,
EventlogLevel level,
bool hideContext) {
- Debug(_logger) << " - event log \"" << logname <<
"\":";
+ const auto getState = [](const IEventLogRecord &record,
+ EventlogLevel level) {
+ return getEventState(record, level).second;
+ };
+ uint64_t lastReadId = 0;
+ EventlogLevel worstState{EventlogLevel::All};
- try {
- std::unique_ptr<IEventLog> log(open_eventlog(
- to_utf16(logname, _winapi), *_vista_api, _logger, _winapi));
- {
- Debug(_logger) << " . successfully opened event log";
- out << "[[[" << logname << "]]]\n";
-
- const auto getState = [](const IEventLogRecord &record,
- EventlogLevel level) {
- return getEventState(record, level).second;
- };
- uint64_t lastReadId = 0;
- EventlogLevel worstState{EventlogLevel::All};
-
- // first pass - determine if there are records above level
- std::tie(lastReadId, worstState) =
- processEventLog(*log, previouslyReadId, level, getState);
- Debug(_logger) << " . worst state: " <<
static_cast<int>(worstState);
-
- // second pass - if there were, print everything
- if (worstState >= level) {
- const auto outputRecord = [&out, hideContext](
- const IEventLogRecord &record, EventlogLevel level) {
- return outputEventlogRecord(out, record, level,
- hideContext);
- };
- processEventLog(*log, previouslyReadId, level, outputRecord);
- }
- // Return the last entry number. We need to fetch the last record ID
- // separately if INT_MAX was used as seek offset and no new entries
- // were read.
- return (std::numeric_limits<uint64_t>::max() == lastReadId)
- ? log->getLastRecordId()
- : lastReadId;
- }
- } catch (const std::exception &e) {
- Error(_logger) << "failed to read event log: " << e.what()
<< std::endl;
- out << "[[[" << logname <<
":missing]]]\n";
- return previouslyReadId;
+ // first pass - determine if there are records above level
+ std::tie(lastReadId, worstState) =
+ processEventLog(log, previouslyReadId, level, getState);
+ Debug(_logger) << " . worst state: " <<
static_cast<int>(worstState);
+
+ // second pass - if there were, print everything
+ if (worstState >= level) {
+ const auto outputRecord = [&out, hideContext](
+ const IEventLogRecord &record, EventlogLevel level) {
+ return outputEventlogRecord(out, record, level, hideContext);
+ };
+ processEventLog(log, previouslyReadId, level, outputRecord);
}
+ // Return the last entry number. We need to fetch the last record ID
+ // separately if INT_MAX was used as seek offset and no new entries
+ // were read.
+ return (std::numeric_limits<uint64_t>::max() == lastReadId)
+ ? log.getLastRecordId()
+ : lastReadId;
}
// Keeps memory of an event log we have found. It
@@ -372,6 +356,24 @@ std::pair<EventlogLevel, bool> SectionEventlog::readConfig(
: std::make_pair(EventlogLevel::Warn, false);
}
+std::unique_ptr<IEventLog> SectionEventlog::openEventlog(
+ const std::string &logname, std::ostream &out) const {
+ Debug(_logger) << " - event log \"" << logname <<
"\":";
+
+ try {
+ std::unique_ptr<IEventLog> log(open_eventlog(
+ to_utf16(logname.c_str(), _winapi), *_vista_api, _logger, _winapi));
+
+ Debug(_logger) << " . successfully opened event log";
+ out << "[[[" << logname << "]]]\n";
+ return log;
+ } catch (const std::exception &e) {
+ Error(_logger) << "failed to read event log: " << e.what()
<< std::endl;
+ out << "[[[" << logname <<
":missing]]]\n";
+ return nullptr;
+ }
+}
+
// The output of this section is compatible with
// the logwatch agent for Linux and UNIX
bool SectionEventlog::produceOutputInner(std::ostream &out) {
@@ -394,9 +396,11 @@ bool SectionEventlog::produceOutputInner(std::ostream &out) {
std::tie(level, hideContext) = readConfig(state);
if (level != EventlogLevel::Off) {
- state.record_no =
- outputEventlog(out, state.name.c_str(), state.record_no,
- level, hideContext);
+ const auto log = openEventlog(state.name, out);
+ if (log) {
+ state.record_no = outputEventlog(
+ out, *log, state.record_no, level, hideContext);
+ }
}
}
}
diff --git a/agents/windows/sections/SectionEventlog.h
b/agents/windows/sections/SectionEventlog.h
index 76ffdbf..83490f0 100644
--- a/agents/windows/sections/SectionEventlog.h
+++ b/agents/windows/sections/SectionEventlog.h
@@ -135,7 +135,7 @@ protected:
virtual bool produceOutputInner(std::ostream &out) override;
private:
- uint64_t outputEventlog(std::ostream &out, const char *logname,
+ uint64_t outputEventlog(std::ostream &out, IEventLog &log,
uint64_t previouslyReadId, EventlogLevel level,
bool hideContext);
void registerEventlog(const char *logname);
@@ -145,6 +145,8 @@ private:
void readHintOffsets();
std::pair<EventlogLevel, bool> readConfig(
const eventlog_file_state &state) const;
+ std::unique_ptr<IEventLog> openEventlog(const std::string &logname,
+ std::ostream &out) const;
Configurable<bool> _send_initial;
Configurable<bool> _vista_api;