Module: check_mk
Branch: master
Commit: 75ab5efe3e220c127aed90b9e57336ab281c4824
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=75ab5efe3e220c…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Wed Oct 25 16:04:12 2017 +0200
Unified HostListFilter implementations.
Doesn't win a beauty contest yet, but better than before...
Change-Id: Idb08e8176c630ceb25834359085b729c07b2f92d
---
livestatus/src/HostListColumn.cc | 5 +--
livestatus/src/HostListColumn.h | 6 ++--
livestatus/src/HostListFilter.cc | 76 ++++++++++++++++++++++++++--------------
3 files changed, 56 insertions(+), 31 deletions(-)
diff --git a/livestatus/src/HostListColumn.cc b/livestatus/src/HostListColumn.cc
index 105946f..46b6b38 100644
--- a/livestatus/src/HostListColumn.cc
+++ b/livestatus/src/HostListColumn.cc
@@ -29,7 +29,7 @@
#include "Row.h"
#include "auth.h"
-hostsmember *HostListColumn::getMembers(Row row) const {
+HostListColumn::host_list HostListColumn::getMembers(Row row) const {
if (auto p = columnData<hostsmember *>(row)) {
return *p;
}
@@ -39,7 +39,8 @@ hostsmember *HostListColumn::getMembers(Row row) const {
void HostListColumn::output(Row row, RowRenderer &r,
const contact *auth_user) const {
ListRenderer l(r);
- for (hostsmember *mem = getMembers(row); mem != nullptr; mem = mem->next) {
+ for (const hostsmember *mem = getMembers(row); mem != nullptr;
+ mem = mem->next) {
host *hst = mem->host_ptr;
if (auth_user == nullptr ||
is_authorized_for(_mc, auth_user, hst, nullptr)) {
diff --git a/livestatus/src/HostListColumn.h b/livestatus/src/HostListColumn.h
index 965195e..f0eb856 100644
--- a/livestatus/src/HostListColumn.h
+++ b/livestatus/src/HostListColumn.h
@@ -58,11 +58,13 @@ public:
const contact *auth_user) const override;
std::unique_ptr<Filter> createFilter(
RelationalOperator relOp, const std::string &value) const override;
+
#ifdef CMC
- const std::unordered_set<Host *> *getMembers(Row row) const;
+ using host_list = const std::unordered_set<Host *> *;
#else
- hostsmember *getMembers(Row row) const;
+ using host_list = const hostsmember *;
#endif
+ host_list getMembers(Row row) const;
private:
MonitoringCore *_mc;
diff --git a/livestatus/src/HostListFilter.cc b/livestatus/src/HostListFilter.cc
index 0ce01fe..5e18c6b 100644
--- a/livestatus/src/HostListFilter.cc
+++ b/livestatus/src/HostListFilter.cc
@@ -28,48 +28,70 @@
#include "HostListColumn.h"
#include "Logger.h"
#include "Row.h"
+
+#ifdef CMC
+#include <unordered_set>
+#include "World.h"
+#include "cmc.h"
+class Host;
+#else
#include "nagios.h"
+#endif
HostListFilter::HostListFilter(const HostListColumn &column,
RelationalOperator relOp, std::string value)
: _column(column), _relOp(relOp), _ref_value(std::move(value)) {}
-bool HostListFilter::accepts(Row row, const contact * /* auth_user */,
- std::chrono::seconds /* timezone_offset */) const {
- // data points to a primary data object. We need to extract a pointer to a
- // host list
- hostsmember *mem = _column.getMembers(row);
-
- // test for empty list
- if (_ref_value.empty()) {
- if (_relOp == RelationalOperator::equal) {
- return mem == nullptr;
- }
- if (_relOp == RelationalOperator::not_equal) {
- return mem != nullptr;
- }
- }
+namespace {
+bool isEmpty(HostListColumn::host_list hostlist) {
+#ifdef CMC
+ return hostlist->empty();
+#else
+ return hostlist == nullptr;
+#endif
+}
- bool is_member = false;
- for (; mem != nullptr; mem = mem->next) {
- char *host_name = mem->host_name;
+bool contains(HostListColumn::host_list hostlist,
+ const std::string &ref_value) {
+#ifdef CMC
+ return hostlist->find(g_live_world->getHost(ref_value)) != hostlist->end();
+#else
+ for (; hostlist != nullptr; hostlist = hostlist->next) {
+ char *host_name = hostlist->host_name;
if (host_name == nullptr) {
- host_name = mem->host_ptr->name;
+ host_name = hostlist->host_ptr->name;
}
-
- if (host_name == _ref_value) {
- is_member = true;
- break;
+ if (host_name == ref_value) {
+ return true;
}
}
+ return false;
+#endif
+}
+} // namespace
+bool HostListFilter::accepts(Row row, const contact * /* auth_user */,
+ std::chrono::seconds /* timezone_offset */) const {
+ auto hostlist = _column.getMembers(row);
switch (_relOp) {
- case RelationalOperator::less:
- return !is_member;
- case RelationalOperator::greater_or_equal:
- return is_member;
case RelationalOperator::equal:
+ if (!_ref_value.empty()) {
+ Informational(_column.logger())
+ << "Sorry, equality for host lists implemented only for
emptiness";
+ return false;
+ }
+ return isEmpty(hostlist);
case RelationalOperator::not_equal:
+ if (!_ref_value.empty()) {
+ Informational(_column.logger())
+ << "Sorry, inequality for host lists implemented only for
emptiness";
+ return false;
+ }
+ return !isEmpty(hostlist);
+ case RelationalOperator::less:
+ return !contains(hostlist, _ref_value);
+ case RelationalOperator::greater_or_equal:
+ return contains(hostlist, _ref_value);
case RelationalOperator::matches:
case RelationalOperator::doesnt_match:
case RelationalOperator::equal_icase: