Module: check_mk
Branch: master
Commit: 2598a91fb54a1e182fa8c2eb5e1b0fa4eafdcea4
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=2598a91fb54a1e…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Fri Aug 31 11:28:58 2018 +0200
Each Logfile is owned by exactly one LogCache.
This fact can be used to avoid threading through a LogCache parameter.
Change-Id: Iabe795d80fa86c2410845457866ffc1b376a0175
---
livestatus/src/LogCache.cc | 6 ++++--
livestatus/src/Logfile.cc | 25 +++++++++++++------------
livestatus/src/Logfile.h | 13 ++++++-------
livestatus/src/TableLog.cc | 3 +--
livestatus/src/TableStateHistory.cc | 12 ++++++------
5 files changed, 30 insertions(+), 29 deletions(-)
diff --git a/livestatus/src/LogCache.cc b/livestatus/src/LogCache.cc
index f14380a..14afde4 100644
--- a/livestatus/src/LogCache.cc
+++ b/livestatus/src/LogCache.cc
@@ -74,12 +74,14 @@ void LogCache::updateLogfileIndex() {
_last_index_update = std::chrono::system_clock::now();
// We need to find all relevant logfiles. This includes directory, the
// current nagios.log and all files in the archive.
- addToIndex(std::make_unique<Logfile>(_mc, _mc->historyFilePath(), true));
+ addToIndex(
+ std::make_unique<Logfile>(_mc, this, _mc->historyFilePath(), true));
fs::path dirpath = _mc->logArchivePath();
try {
for (const auto &entry : fs::directory_iterator(dirpath)) {
- addToIndex(std::make_unique<Logfile>(_mc, entry.path(), false));
+ addToIndex(
+ std::make_unique<Logfile>(_mc, this, entry.path(), false));
}
} catch (const fs::filesystem_error &e) {
Warning(logger()) << "updating log file index: " <<
e.what();
diff --git a/livestatus/src/Logfile.cc b/livestatus/src/Logfile.cc
index be24cec..f24d108 100644
--- a/livestatus/src/Logfile.cc
+++ b/livestatus/src/Logfile.cc
@@ -44,8 +44,10 @@
#include <vector>
#endif
-Logfile::Logfile(MonitoringCore *mc, fs::path path, bool watch)
+Logfile::Logfile(MonitoringCore *mc, LogCache *logcache, fs::path path,
+ bool watch)
: _mc(mc)
+ , _logcache(logcache)
, _path(std::move(path))
, _since(0)
, _watch(watch)
@@ -83,7 +85,7 @@ void Logfile::flush() {
_logclasses_read = 0;
}
-void Logfile::load(LogCache *logcache, unsigned logclasses) {
+void Logfile::load(unsigned logclasses) {
unsigned missing_types = logclasses & ~_logclasses_read;
FILE *file = nullptr;
// The current logfile has the _watch flag set to true.
@@ -106,13 +108,13 @@ void Logfile::load(LogCache *logcache, unsigned logclasses) {
// have read to the end of the file
if (_logclasses_read != 0u) {
fsetpos(file, &_read_pos); // continue at previous end
- loadRange(file, _logclasses_read, logcache, logclasses);
+ loadRange(file, _logclasses_read, logclasses);
fgetpos(file, &_read_pos);
}
if (missing_types != 0u) {
fseek(file, 0, SEEK_SET);
_lineno = 0;
- loadRange(file, missing_types, logcache, logclasses);
+ loadRange(file, missing_types, logclasses);
_logclasses_read |= missing_types;
fgetpos(file, &_read_pos); // remember current end of file
}
@@ -130,13 +132,13 @@ void Logfile::load(LogCache *logcache, unsigned logclasses) {
}
_lineno = 0;
- loadRange(file, missing_types, logcache, logclasses);
+ loadRange(file, missing_types, logclasses);
fclose(file);
_logclasses_read |= missing_types;
}
}
-void Logfile::loadRange(FILE *file, unsigned missing_types, LogCache *logcache,
+void Logfile::loadRange(FILE *file, unsigned missing_types,
unsigned logclasses) {
std::vector<char> linebuffer(65536);
// TODO(sp) We should really use C++ I/O here...
@@ -156,7 +158,7 @@ void Logfile::loadRange(FILE *file, unsigned missing_types, LogCache
*logcache,
}
}
if (processLogLine(_lineno, &linebuffer[0], missing_types)) {
- logcache->logLineHasBeenAdded(this, logclasses);
+ _logcache->logLineHasBeenAdded(this, logclasses);
}
}
}
@@ -202,21 +204,20 @@ bool Logfile::processLogLine(size_t lineno, std::string line,
}
logfile_entries_t *Logfile::getEntriesFromQuery(const Query * /*unused*/,
- LogCache *logcache,
unsigned logclasses) {
// Make sure existing references to objects point to correct world
updateReferences();
// make sure all messages are present
- load(logcache, logclasses);
+ load(logclasses);
return &_entries;
}
-bool Logfile::answerQueryReverse(Query *query, LogCache *logcache, time_t since,
- time_t until, unsigned logclasses) {
+bool Logfile::answerQueryReverse(Query *query, time_t since, time_t until,
+ unsigned logclasses) {
// Make sure existing references to objects point to correct world
updateReferences();
// make sure all messages are present
- load(logcache, logclasses);
+ load(logclasses);
uint64_t untilkey = makeKey(until, 999999999);
auto it = _entries.upper_bound(untilkey);
while (it != _entries.begin()) {
diff --git a/livestatus/src/Logfile.h b/livestatus/src/Logfile.h
index 08e4dd7..062f04d 100644
--- a/livestatus/src/Logfile.h
+++ b/livestatus/src/Logfile.h
@@ -49,7 +49,7 @@ using logfile_entries_t = std::map<uint64_t,
std::unique_ptr<LogEntry>>;
class Logfile {
public:
- Logfile(MonitoringCore *mc, fs::path path, bool watch);
+ Logfile(MonitoringCore *mc, LogCache *logcache, fs::path path, bool watch);
std::string path() { return _path; }
#ifdef CMC
@@ -67,15 +67,15 @@ public:
// for TableStateHistory
logfile_entries_t *getEntriesFromQuery(const Query *query,
- LogCache *logcache,
unsigned logclasses);
// for TableLog::answerQuery
- bool answerQueryReverse(Query *query, LogCache *logcache, time_t since,
- time_t until, unsigned logclasses);
+ bool answerQueryReverse(Query *query, time_t since, time_t until,
+ unsigned logclasses);
private:
MonitoringCore *_mc;
+ LogCache *const _logcache;
fs::path _path;
time_t _since; // time of first entry
bool _watch; // true only for current logfile
@@ -87,9 +87,8 @@ private:
#endif
unsigned _logclasses_read; // only these types have been read
- void load(LogCache *logcache, unsigned logclasses);
- void loadRange(FILE *file, unsigned missing_types, LogCache *logcache,
- unsigned logclasses);
+ void load(unsigned logclasses);
+ void loadRange(FILE *file, unsigned missing_types, unsigned logclasses);
bool processLogLine(size_t lineno, std::string line, unsigned logclasses);
uint64_t makeKey(time_t t, size_t lineno);
void updateReferences();
diff --git a/livestatus/src/TableLog.cc b/livestatus/src/TableLog.cc
index bb23e12..b7097cf 100644
--- a/livestatus/src/TableLog.cc
+++ b/livestatus/src/TableLog.cc
@@ -166,8 +166,7 @@ void TableLog::answerQuery(Query *query) {
}
while (true) {
- if (!it->second->answerQueryReverse(query, _log_cache, since, until,
- classmask)) {
+ if (!it->second->answerQueryReverse(query, since, until, classmask)) {
break; // end of time range found
}
if (it == _log_cache->begin()) {
diff --git a/livestatus/src/TableStateHistory.cc b/livestatus/src/TableStateHistory.cc
index 86b98ff..8ead016 100644
--- a/livestatus/src/TableStateHistory.cc
+++ b/livestatus/src/TableStateHistory.cc
@@ -212,8 +212,8 @@ void TableStateHistory::getPreviousLogentry() {
return;
}
--_it_logs;
- _entries = _it_logs->second->getEntriesFromQuery(_query, _log_cache,
- classmask_statehist);
+ _entries =
+ _it_logs->second->getEntriesFromQuery(_query, classmask_statehist);
_it_entries = _entries->end();
}
--_it_entries;
@@ -230,8 +230,8 @@ LogEntry *TableStateHistory::getNextLogentry() {
return nullptr;
}
++_it_logs;
- _entries = _it_logs->second->getEntriesFromQuery(_query, _log_cache,
- classmask_statehist);
+ _entries =
+ _it_logs->second->getEntriesFromQuery(_query, classmask_statehist);
_it_entries = _entries->begin();
}
return _it_entries->second.get();
@@ -330,8 +330,8 @@ void TableStateHistory::answerQuery(Query *query) {
}
// Determine initial logentry
- _entries = _it_logs->second->getEntriesFromQuery(query, _log_cache,
- classmask_statehist);
+ _entries =
+ _it_logs->second->getEntriesFromQuery(query, classmask_statehist);
if (!_entries->empty() && _it_logs != newest_log) {
_it_entries = _entries->end();
// Check last entry. If it's younger than _since -> use this logfile too