Module: check_mk
Branch: master
Commit: 305f1a8a662d906deeb50d77a7b8a3285dad61b7
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=305f1a8a662d90…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Tue Jul 12 16:32:05 2016 +0200
Fetched Livestatus from upstream.
---
livestatus/src/ContactgroupsColumn.cc | 2 +-
livestatus/src/DownCommColumn.cc | 7 ++--
livestatus/src/HostgroupsColumn.cc | 2 +-
livestatus/src/ServicegroupsColumn.cc | 2 +-
livestatus/src/TableEventConsole.h | 35 ++++++++++++++++---
livestatus/src/TableEventConsoleEvents.cc | 49 +++++++++++++++++++++++---
livestatus/src/TableEventConsoleHistory.cc | 52 +++++++++++++++++++++++++---
7 files changed, 130 insertions(+), 19 deletions(-)
diff --git a/livestatus/src/ContactgroupsColumn.cc
b/livestatus/src/ContactgroupsColumn.cc
index 850a7b5..529ad18 100644
--- a/livestatus/src/ContactgroupsColumn.cc
+++ b/livestatus/src/ContactgroupsColumn.cc
@@ -60,7 +60,7 @@ unique_ptr<ListColumn::Contains>
ContactgroupsColumn::makeContains(
const string &name) {
class ContainsContactGroup : public Contains {
public:
- explicit ContainsContactGroup(contactgroup *element, int offset)
+ ContainsContactGroup(contactgroup *element, int offset)
: _element(element), _offset(offset) {}
bool operator()(void *row) override {
diff --git a/livestatus/src/DownCommColumn.cc b/livestatus/src/DownCommColumn.cc
index f54e0dd..49797d2 100644
--- a/livestatus/src/DownCommColumn.cc
+++ b/livestatus/src/DownCommColumn.cc
@@ -91,11 +91,12 @@ bool DownCommColumn::match(DowntimeOrComment *dt, void *data) {
return dt->_host->name == h->name;
}
-unique_ptr<ListColumn::Contains> DownCommColumn::makeContains(const string
&name) {
+unique_ptr<ListColumn::Contains> DownCommColumn::makeContains(
+ const string &name) {
class ContainsDownCommID : public Contains {
public:
- explicit ContainsDownCommID(unsigned long element,
- const DowntimesOrComments &holder)
+ ContainsDownCommID(unsigned long element,
+ const DowntimesOrComments &holder)
: _element(element), _holder(holder) {}
bool operator()(void *row) override {
diff --git a/livestatus/src/HostgroupsColumn.cc b/livestatus/src/HostgroupsColumn.cc
index 9e1166c..85b222d 100644
--- a/livestatus/src/HostgroupsColumn.cc
+++ b/livestatus/src/HostgroupsColumn.cc
@@ -63,7 +63,7 @@ unique_ptr<ListColumn::Contains> HostgroupsColumn::makeContains(
const string &name) {
class ContainsHostGroup : public Contains {
public:
- explicit ContainsHostGroup(hostgroup *element, int offset)
+ ContainsHostGroup(hostgroup *element, int offset)
: _element(element), _offset(offset) {}
bool operator()(void *row) override {
diff --git a/livestatus/src/ServicegroupsColumn.cc
b/livestatus/src/ServicegroupsColumn.cc
index f5a4094..988425a 100644
--- a/livestatus/src/ServicegroupsColumn.cc
+++ b/livestatus/src/ServicegroupsColumn.cc
@@ -65,7 +65,7 @@ unique_ptr<ListColumn::Contains>
ServicegroupsColumn::makeContains(
const string &name) {
class ContainsServiceGroup : public Contains {
public:
- explicit ContainsServiceGroup(servicegroup *element, int offset)
+ ContainsServiceGroup(servicegroup *element, int offset)
: _element(element), _offset(offset) {}
bool operator()(void *row) override {
diff --git a/livestatus/src/TableEventConsole.h b/livestatus/src/TableEventConsole.h
index e37338a..c4a2104 100644
--- a/livestatus/src/TableEventConsole.h
+++ b/livestatus/src/TableEventConsole.h
@@ -28,6 +28,7 @@
#include "config.h" // IWYU pragma: keep
#include <stdlib.h>
#include <sys/types.h>
+#include <algorithm>
#include <cstdint>
#include <functional>
#include <map>
@@ -75,6 +76,11 @@ protected:
std::function<T(std::string)> f)
: _name(name), _default_value(default_value), _f(f) {}
+ std::string getRaw(void *data) const {
+ auto row = static_cast<Row *>(data);
+ return row == nullptr ? "" : row->_map.at(_name);
+ }
+
T getValue(void *data) const {
auto row = static_cast<Row *>(data);
return row == nullptr ? _default_value : _f(row->_map.at(_name));
@@ -148,6 +154,14 @@ protected:
: mk::split(x.substr(1), '\001');
}) {}
+ bool isNone(void *data) {
+ return _ecc.getRaw(data) == "\002";
+ }
+
+ _column_t getValue(void *data) {
+ return _ecc.getValue(data);
+ }
+
void output(void *data, Query *query) override {
query->outputBeginList();
bool first = true;
@@ -167,12 +181,23 @@ protected:
}
std::unique_ptr<Contains> makeContains(
- const std::string & /*unused*/) override {
- struct ContainsElem : public Contains {
- // TODO(sp) Actually implement this.
- bool operator()(void * /*unused*/) override { return false; }
+ const std::string &name) override {
+ class ContainsElem : public Contains {
+ public:
+ ContainsElem(const std::string &name,
+ const EventConsoleColumn<_column_t> &ecc)
+ : _name(name), _ecc(ecc) {}
+ bool operator()(void *row) override {
+ const _column_t &values = _ecc.getValue(row);
+ return std::find(values.begin(), values.end(), _name) !=
+ values.end();
+ }
+
+ private:
+ std::string _name;
+ const EventConsoleColumn<_column_t> &_ecc;
};
- return std::make_unique<ContainsElem>();
+ return std::make_unique<ContainsElem>(name, _ecc);
}
};
diff --git a/livestatus/src/TableEventConsoleEvents.cc
b/livestatus/src/TableEventConsoleEvents.cc
index afc8037..2f08f2c 100644
--- a/livestatus/src/TableEventConsoleEvents.cc
+++ b/livestatus/src/TableEventConsoleEvents.cc
@@ -28,7 +28,9 @@
#include "TableHosts.h"
#ifdef CMC
+#include "ContactGroup.h"
#include "Host.h"
+#include "World.h"
#else
#include "auth.h"
#endif
@@ -129,11 +131,50 @@ const char *TableEventConsoleEvents::namePrefix() const {
return "eventconsoleevents_";
}
-bool TableEventConsoleEvents::isAuthorized(contact *ctc, void *data) {
- host *host = static_cast<Row *>(data)->_host;
+// TODO(sp) Move this into some kind of abstraction layer.
+namespace {
+bool hasContact(host *hst, contact *ctc) {
+#ifdef CMC
+ return hst != nullptr && hst->hasContact(ctc);
+#else
+ return is_authorized_for(ctc, hst, nullptr);
+#endif
+}
+
+bool hasContact(contactgroup *cg, contact *ctc) {
+#ifdef CMC
+ return cg != nullptr && cg->isMember(ctc);
+#else
+ return is_contact_member_of_contactgroup(cg, ctc) != 0;
+#endif
+}
+
+contactgroup *getContactGroup(const string &name) {
#ifdef CMC
- return host == nullptr || host->hasContact(ctc);
+ return g_live_world->getContactGroup(name);
#else
- return host == nullptr || is_authorized_for(ctc, host, nullptr);
+ // Older Nagios headers are not const-correct... :-P
+ return find_contactgroup(const_cast<char *>(name.c_str()));
#endif
}
+} // namespace
+
+// TODO(sp) This is copy-n-pasted in TableEventConsoleHistory.
+bool TableEventConsoleEvents::isAuthorized(contact *ctc, void *data) {
+ if (host *hst = static_cast<Row *>(data)->_host) {
+ return hasContact(hst, ctc);
+ }
+
+ ListEventConsoleColumn *col =
+ static_cast<ListEventConsoleColumn
*>(column("event_contact_groups"));
+ if (col->isNone(data)) {
+ return true;
+ }
+
+ for (const auto &name : col->getValue(data)) {
+ if (hasContact(getContactGroup(name), ctc)) {
+ return true;
+ }
+ }
+ return false;
+}
diff --git a/livestatus/src/TableEventConsoleHistory.cc
b/livestatus/src/TableEventConsoleHistory.cc
index 055a4be..85a5fed 100644
--- a/livestatus/src/TableEventConsoleHistory.cc
+++ b/livestatus/src/TableEventConsoleHistory.cc
@@ -23,14 +23,19 @@
// Boston, MA 02110-1301 USA.
#include "TableEventConsoleHistory.h"
+#include <string>
#include "TableEventConsoleEvents.h"
#ifdef CMC
+#include "ContactGroup.h"
#include "Host.h"
+#include "World.h"
#else
#include "auth.h"
#endif
+using std::string;
+
#ifdef CMC
TableEventConsoleHistory::TableEventConsoleHistory(
const Notes &downtimes_holder, const Notes &comments_holder,
@@ -73,11 +78,50 @@ const char *TableEventConsoleHistory::namePrefix() const {
return "eventconsolehistory_";
}
-bool TableEventConsoleHistory::isAuthorized(contact *ctc, void *data) {
- host *host = static_cast<Row *>(data)->_host;
+// TODO(sp) Move this into some kind of abstraction layer.
+namespace {
+bool hasContact(host *hst, contact *ctc) {
#ifdef CMC
- return host == nullptr || host->hasContact(ctc);
+ return hst != nullptr && hst->hasContact(ctc);
#else
- return host == nullptr || is_authorized_for(ctc, host, nullptr);
+ return is_authorized_for(ctc, hst, nullptr);
#endif
}
+
+bool hasContact(contactgroup *cg, contact *ctc) {
+#ifdef CMC
+ return cg != nullptr && cg->isMember(ctc);
+#else
+ return is_contact_member_of_contactgroup(cg, ctc) != 0;
+#endif
+}
+
+contactgroup *getContactGroup(const string &name) {
+#ifdef CMC
+ return g_live_world->getContactGroup(name);
+#else
+ // Older Nagios headers are not const-correct... :-P
+ return find_contactgroup(const_cast<char *>(name.c_str()));
+#endif
+}
+} // namespace
+
+// TODO(sp) This is copy-n-pasted in TableEventConsoleEvents.
+bool TableEventConsoleHistory::isAuthorized(contact *ctc, void *data) {
+ if (host *hst = static_cast<Row *>(data)->_host) {
+ return hasContact(hst, ctc);
+ }
+
+ ListEventConsoleColumn *col =
+ static_cast<ListEventConsoleColumn
*>(column("event_contact_groups"));
+ if (col->isNone(data)) {
+ return true;
+ }
+
+ for (const auto &name : col->getValue(data)) {
+ if (hasContact(getContactGroup(name), ctc)) {
+ return true;
+ }
+ }
+ return false;
+}