Module: check_mk
Branch: master
Commit: 665267687fef050c28985ea9ab9cd4a6b18e13b9
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=665267687fef05…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Thu Apr 11 16:19:55 2019 +0200
Untie the knot between Store and NagiosCore, part 3.
Change-Id: I18bbe87ace688052dc97e1dd45a5423829b4e724
---
livestatus/src/DynamicLogwatchFileColumn.cc | 4 +++-
livestatus/src/HostFileColumn.cc | 10 ++++++----
livestatus/src/HostFileColumn.h | 6 ++++--
livestatus/src/TableHosts.cc | 9 +++++----
livestatus/src/test/test_OffsetStringMacroColumn.cc | 9 +++++++++
5 files changed, 27 insertions(+), 11 deletions(-)
diff --git a/livestatus/src/DynamicLogwatchFileColumn.cc
b/livestatus/src/DynamicLogwatchFileColumn.cc
index 21f2831..197bec5 100644
--- a/livestatus/src/DynamicLogwatchFileColumn.cc
+++ b/livestatus/src/DynamicLogwatchFileColumn.cc
@@ -63,7 +63,9 @@ std::unique_ptr<Column> DynamicLogwatchFileColumn::createColumn(
"': file name '" + arguments +
"' contains slash");
}
+ auto mc = _mc;
+ auto suffix = "/" + unescape_filename(arguments);
return std::make_unique<HostFileColumn>(
name, "Contents of logwatch file", _indirect_offset, _extra_offset,
-1,
- 0, _mc->mkLogwatchPath(), "/" + unescape_filename(arguments));
+ 0, [mc]() { return mc->mkLogwatchPath(); }, suffix);
}
diff --git a/livestatus/src/HostFileColumn.cc b/livestatus/src/HostFileColumn.cc
index c201343..62437ec 100644
--- a/livestatus/src/HostFileColumn.cc
+++ b/livestatus/src/HostFileColumn.cc
@@ -43,14 +43,16 @@ HostFileColumn::HostFileColumn(const std::string& name,
const std::string& description,
int indirect_offset, int extra_offset,
int extra_extra_offset, int offset,
- std::string base_dir, std::string suffix)
+ std::function<std::string()> get_base_dir,
+ std::string suffix)
: BlobColumn(name, description, indirect_offset, extra_offset,
extra_extra_offset, offset)
- , _base_dir(std::move(base_dir))
+ , _get_base_dir(std::move(get_base_dir))
, _suffix(std::move(suffix)) {}
std::unique_ptr<std::vector<char>> HostFileColumn::getValue(Row row) const {
- if (_base_dir.empty()) {
+ auto base_dir = _get_base_dir();
+ if (base_dir.empty()) {
return nullptr; // Path is not configured
}
@@ -68,7 +70,7 @@ std::unique_ptr<std::vector<char>>
HostFileColumn::getValue(Row row) const {
std::string host_name = hst->name;
#endif
- std::string path = _base_dir + "/" + host_name + _suffix;
+ std::string path = base_dir + "/" + host_name + _suffix;
int fd = open(path.c_str(), O_RDONLY);
if (fd == -1) {
// It is OK when inventory/logwatch files do not exist.
diff --git a/livestatus/src/HostFileColumn.h b/livestatus/src/HostFileColumn.h
index d122c98..f242843 100644
--- a/livestatus/src/HostFileColumn.h
+++ b/livestatus/src/HostFileColumn.h
@@ -26,6 +26,7 @@
#define HostFileColumn_h
#include "config.h" // IWYU pragma: keep
+#include <functional>
#include <memory>
#include <string>
#include <vector>
@@ -36,13 +37,14 @@ class HostFileColumn : public BlobColumn {
public:
HostFileColumn(const std::string& name, const std::string& description,
int indirect_offset, int extra_offset,
- int extra_extra_offset, int offset, std::string base_dir,
+ int extra_extra_offset, int offset,
+ std::function<std::string()> get_base_dir,
std::string suffix);
std::unique_ptr<std::vector<char>> getValue(Row row) const override;
private:
- std::string _base_dir;
+ std::function<std::string()> _get_base_dir;
std::string _suffix;
};
diff --git a/livestatus/src/TableHosts.cc b/livestatus/src/TableHosts.cc
index d3e7de8..c1e7c44 100644
--- a/livestatus/src/TableHosts.cc
+++ b/livestatus/src/TableHosts.cc
@@ -75,6 +75,7 @@ std::string TableHosts::namePrefix() const { return "host_";
}
// static
void TableHosts::addColumns(Table *table, const std::string &prefix,
int indirect_offset, int extra_offset) {
+ auto mc = table->core();
table->addColumn(std::make_unique<OffsetStringColumn>(
prefix + "name", "Host name", indirect_offset, extra_offset,
-1,
DANGEROUS_OFFSETOF(host, name)));
@@ -643,17 +644,17 @@ void TableHosts::addColumns(Table *table, const std::string
&prefix,
table->addColumn(std::make_unique<HostFileColumn>(
prefix + "mk_inventory",
"The file content of the Check_MK HW/SW-Inventory", indirect_offset,
- extra_offset, -1, 0, table->core()->mkInventoryPath(), ""));
+ extra_offset, -1, 0, [mc]() { return mc->mkInventoryPath(); },
""));
table->addColumn(std::make_unique<HostFileColumn>(
prefix + "mk_inventory_gz",
"The gzipped file content of the Check_MK HW/SW-Inventory",
- indirect_offset, extra_offset, -1, 0, table->core()->mkInventoryPath(),
- ".gz"));
+ indirect_offset, extra_offset, -1, 0,
+ [mc]() { return mc->mkInventoryPath(); }, ".gz"));
table->addColumn(std::make_unique<HostFileColumn>(
prefix + "structured_status",
"The file content of the structured status of the Check_MK
HW/SW-Inventory",
indirect_offset, extra_offset, -1, 0,
- table->core()->structuredStatusPath(), ""));
+ [mc]() { return mc->structuredStatusPath(); }, ""));
table->addColumn(std::make_unique<LogwatchListColumn>(
prefix + "mk_logwatch_files",
diff --git a/livestatus/src/test/test_OffsetStringMacroColumn.cc
b/livestatus/src/test/test_OffsetStringMacroColumn.cc
index b98766c..ca721b3 100644
--- a/livestatus/src/test/test_OffsetStringMacroColumn.cc
+++ b/livestatus/src/test/test_OffsetStringMacroColumn.cc
@@ -6,9 +6,18 @@
#include "OffsetStringHostMacroColumn.h"
#include "OffsetStringServiceMacroColumn.h"
#include "Row.h"
+#include "Store.h"
#include "gtest/gtest.h"
#include "nagios.h"
+// TODO(sp) Move this to a better place.
+TEST(Store, dont_use_mc) {
+ // Make sure that the MonitoringCore abstraction is not accessed during the
+ // construction of Store. This is a bit fragile, but it is needed to tie the
+ // knot between NagiosCore and Store.
+ Store store{nullptr};
+}
+
extern char *macro_user[MAX_USER_MACROS];
namespace {