Module: check_mk
Branch: master
Commit: c4b2eb014b4ed2b09b6b60409e704f3966cf4770
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=c4b2eb014b4ed2…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Fri Aug 31 08:57:32 2018 +0200
Various LogCache-related cleanups.
* Made methods private if possible.
* Just expose an iterator interface, not a whole internal field.
* Inlined an internal method, making the actual intent clearer.
* Improved naming.
Change-Id: I9f7fd73ea1bbe4f7de10bee4aae05caf2aaffddf
---
livestatus/src/LogCache.cc | 22 ++++++++++------------
livestatus/src/LogCache.h | 10 +++++-----
livestatus/src/Logfile.cc | 2 +-
livestatus/src/TableLog.cc | 13 ++++++-------
livestatus/src/TableStateHistory.cc | 11 +++++------
5 files changed, 27 insertions(+), 31 deletions(-)
diff --git a/livestatus/src/LogCache.cc b/livestatus/src/LogCache.cc
index 2506863..f14380a 100644
--- a/livestatus/src/LogCache.cc
+++ b/livestatus/src/LogCache.cc
@@ -57,27 +57,20 @@ void LogCache::setMaxCachedMessages(unsigned long m) {
}
#endif
-bool LogCache::logCachePreChecks() {
+bool LogCache::update() {
if (_logfiles.empty() ||
_mc->last_logfile_rotation() > _last_index_update) {
- forgetLogfiles();
updateLogfileIndex();
}
- if (_logfiles.empty()) {
- Notice(logger()) << "no log file found, not even "
- << _mc->historyFilePath();
- }
return !_logfiles.empty();
}
-void LogCache::forgetLogfiles() {
- Notice(logger()) << "flushing log file index";
+void LogCache::updateLogfileIndex() {
+ Informational(logger()) << "updating log file index";
+
_logfiles.clear();
num_cached_log_messages = 0;
-}
-void LogCache::updateLogfileIndex() {
- Informational(logger()) << "updating log file index";
_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.
@@ -91,6 +84,11 @@ void LogCache::updateLogfileIndex() {
} catch (const fs::filesystem_error &e) {
Warning(logger()) << "updating log file index: " <<
e.what();
}
+
+ if (_logfiles.empty()) {
+ Notice(logger()) << "no log file found, not even "
+ << _mc->historyFilePath();
+ }
}
void LogCache::addToIndex(std::unique_ptr<Logfile> logfile) {
@@ -117,7 +115,7 @@ void LogCache::addToIndex(std::unique_ptr<Logfile> logfile) {
The parameters to this method reflect the current query,
not the messages that just has been loaded.
*/
-void LogCache::handleNewMessage(Logfile *logfile, unsigned logclasses) {
+void LogCache::logLineHasBeenAdded(Logfile *logfile, unsigned logclasses) {
if (static_cast<unsigned long>(++num_cached_log_messages) <=
_max_cached_messages) {
return; // current message count still allowed, everything ok
diff --git a/livestatus/src/LogCache.h b/livestatus/src/LogCache.h
index 84e9ceb..0eea98c 100644
--- a/livestatus/src/LogCache.h
+++ b/livestatus/src/LogCache.h
@@ -45,11 +45,10 @@ public:
#ifdef CMC
void setMaxCachedMessages(unsigned long m);
#endif
- void handleNewMessage(Logfile *logfile, unsigned logclasses);
- logfiles_t *logfiles() { return &_logfiles; };
- void forgetLogfiles();
- void updateLogfileIndex();
- bool logCachePreChecks();
+ void logLineHasBeenAdded(Logfile *logfile, unsigned logclasses);
+ bool update(); // returns true if at least one log file has been found
+ auto begin() { return _logfiles.begin(); }
+ auto end() { return _logfiles.end(); }
private:
MonitoringCore *const _mc;
@@ -58,6 +57,7 @@ private:
logfiles_t _logfiles;
std::chrono::system_clock::time_point _last_index_update;
+ void updateLogfileIndex();
void addToIndex(std::unique_ptr<Logfile> logfile);
Logger *logger() const;
};
diff --git a/livestatus/src/Logfile.cc b/livestatus/src/Logfile.cc
index dc162ec..64c7288 100644
--- a/livestatus/src/Logfile.cc
+++ b/livestatus/src/Logfile.cc
@@ -156,7 +156,7 @@ void Logfile::loadRange(FILE *file, unsigned missing_types, LogCache
*logcache,
}
}
if (processLogLine(_lineno, &linebuffer[0], missing_types)) {
- logcache->handleNewMessage(this, logclasses);
+ logcache->logLineHasBeenAdded(this, logclasses);
}
}
}
diff --git a/livestatus/src/TableLog.cc b/livestatus/src/TableLog.cc
index cee4b56..bb23e12 100644
--- a/livestatus/src/TableLog.cc
+++ b/livestatus/src/TableLog.cc
@@ -126,7 +126,7 @@ std::string TableLog::namePrefix() const { return "log_"; }
void TableLog::answerQuery(Query *query) {
std::lock_guard<std::mutex> lg(_log_cache->_lock);
- if (!_log_cache->logCachePreChecks()) {
+ if (!_log_cache->update()) {
return;
}
@@ -151,16 +151,15 @@ void TableLog::answerQuery(Query *query) {
the Limit: header produces more reasonable results. */
/* NEW CODE - NEWEST FIRST */
- logfiles_t::iterator it;
- it = _log_cache->logfiles()->end(); // it now points beyond last log file
+ auto it = _log_cache->end(); // it now points beyond last log file
--it; // switch to last logfile (we have at least one)
// Now find newest log where 'until' is contained. The problem
// here: For each logfile we only know the time of the *first* entry,
// not that of the last.
- while (it != _log_cache->logfiles()->begin() &&
- it->first > until) { // while logfiles are too new...
- --it; // go back in history
+ while (it != _log_cache->begin() && it->first > until) {
+ // while logfiles are too new go back in history
+ --it;
}
if (it->first > until) {
return; // all logfiles are too new
@@ -171,7 +170,7 @@ void TableLog::answerQuery(Query *query) {
classmask)) {
break; // end of time range found
}
- if (it == _log_cache->logfiles()->begin()) {
+ if (it == _log_cache->begin()) {
break; // this was the oldest one
}
--it;
diff --git a/livestatus/src/TableStateHistory.cc b/livestatus/src/TableStateHistory.cc
index e0d6162..86b98ff 100644
--- a/livestatus/src/TableStateHistory.cc
+++ b/livestatus/src/TableStateHistory.cc
@@ -208,7 +208,7 @@ std::string TableStateHistory::namePrefix() const { return
"statehist_"; }
void TableStateHistory::getPreviousLogentry() {
while (_it_entries == _entries->begin()) {
// open previous logfile
- if (_it_logs == _log_cache->logfiles()->begin()) {
+ if (_it_logs == _log_cache->begin()) {
return;
}
--_it_logs;
@@ -226,7 +226,7 @@ LogEntry *TableStateHistory::getNextLogentry() {
while (_it_entries == _entries->end()) {
auto it_logs_cpy = _it_logs;
- if (++it_logs_cpy == _log_cache->logfiles()->end()) {
+ if (++it_logs_cpy == _log_cache->end()) {
return nullptr;
}
++_it_logs;
@@ -278,7 +278,7 @@ std::unique_ptr<Filter> TableStateHistory::createPartialFilter(
void TableStateHistory::answerQuery(Query *query) {
auto object_filter = createPartialFilter(*query);
std::lock_guard<std::mutex> lg(_log_cache->_lock);
- if (!_log_cache->logCachePreChecks()) {
+ if (!_log_cache->update()) {
return;
}
@@ -313,13 +313,12 @@ void TableStateHistory::answerQuery(Query *query) {
}
// Switch to last logfile (we have at least one)
- _it_logs = _log_cache->logfiles()->end();
+ _it_logs = _log_cache->end();
--_it_logs;
auto newest_log = _it_logs;
// Now find the log where 'since' starts.
- while (_it_logs != _log_cache->logfiles()->begin() &&
- _it_logs->first >= _since) {
+ while (_it_logs != _log_cache->begin() && _it_logs->first >= _since)
{
--_it_logs; // go back in history
}