Module: check_mk
Branch: master
Commit: b6159a7981e8eacf6e4382f2046f2f0aaecd6490
URL: http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=b6159a7981e8ea…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Thu Apr 11 10:58:41 2019 +0200
Untie the knot between Store and NagiosCore.
We should really fix this is a nicer and less fragile way...
Change-Id: I066ad29d8d03c7c8e4b9606d6b860efc988baee7
---
livestatus/src/LogCache.cc | 4 +---
livestatus/src/LogCache.h | 12 ++++++++++--
livestatus/src/Store.cc | 5 ++++-
livestatus/src/Store.h | 2 +-
4 files changed, 16 insertions(+), 7 deletions(-)
diff --git a/livestatus/src/LogCache.cc b/livestatus/src/LogCache.cc
index 094a233..16e5c59 100644
--- a/livestatus/src/LogCache.cc
+++ b/livestatus/src/LogCache.cc
@@ -37,9 +37,7 @@ constexpr unsigned long check_mem_cycle = 1000;
} // namespace
LogCache::LogCache(MonitoringCore *mc)
- : _mc(mc), _num_cached_log_messages(0), _num_at_last_check(0) {
- update();
-}
+ : _mc(mc), _num_cached_log_messages(0), _num_at_last_check(0) {}
void LogCache::update() {
if (!_logfiles.empty() &&
diff --git a/livestatus/src/LogCache.h b/livestatus/src/LogCache.h
index 79cd9f2..2be3331 100644
--- a/livestatus/src/LogCache.h
+++ b/livestatus/src/LogCache.h
@@ -37,14 +37,22 @@ class MonitoringCore;
using logfiles_t = std::map<time_t, std::unique_ptr<Logfile>>;
+// TODO(sp) Split this class into 2 parts: One is really only a cache for the
+// logfiles to monitor, the other part is about the lines in them.
class LogCache {
public:
+ // TODO(sp) Using this class requires a very tricky and fragile protocol:
+ // You have to get the lock and call update() before you use any of the
+ // other methods. Furthermore, the constructor is not allowed to call any
+ // method of the MonitoringCore it gets, because there is a knot between the
+ // Store and the NagiosCore classes, so the MonitoringCore is not yet fully
+ // constructed. :-P
std::mutex _lock;
-
explicit LogCache(MonitoringCore *mc);
+ void update();
+
size_t numCachedLogMessages() const;
void logLineHasBeenAdded(Logfile *logfile, unsigned logclasses);
- void update();
auto begin() { return _logfiles.begin(); }
auto end() { return _logfiles.end(); }
diff --git a/livestatus/src/Store.cc b/livestatus/src/Store.cc
index cd2c3ad..6984400 100644
--- a/livestatus/src/Store.cc
+++ b/livestatus/src/Store.cc
@@ -25,6 +25,7 @@
#include "Store.h"
#include <ctime>
#include <memory>
+#include <mutex>
#include <sstream>
#include <stdexcept>
#include <utility>
@@ -308,6 +309,8 @@ bool Store::answerGetRequest(const std::list<std::string> &lines,
Logger *Store::logger() const { return _mc->loggerLivestatus(); }
-size_t Store::numCachedLogMessages() const {
+size_t Store::numCachedLogMessages() {
+ std::lock_guard<std::mutex> lg(_log_cache._lock);
+ _log_cache.update();
return _log_cache.numCachedLogMessages();
}
diff --git a/livestatus/src/Store.h b/livestatus/src/Store.h
index 9230d8c..f107a23 100644
--- a/livestatus/src/Store.h
+++ b/livestatus/src/Store.h
@@ -100,7 +100,7 @@ public:
void registerComment(nebstruct_comment_data *data);
#endif
Logger *logger() const;
- size_t numCachedLogMessages() const;
+ size_t numCachedLogMessages();
private:
struct TableDummy : public Table {