Module: check_mk
Branch: master
Commit: ed156042e2f13cb6a36ec59936fe68d0f3af012a
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=ed156042e2f13c…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Fri Dec 16 16:45:23 2016 +0100
Fetched livestatus from downstream.
Change-Id: I6e014c1aef5eff91a384c6678fa1d587368bd23d
---
.../src/DynamicEventConsoleReplicationColumn.cc | 10 +++--
livestatus/src/EventConsoleConnection.cc | 45 ++++++++++++----------
livestatus/src/EventConsoleConnection.h | 8 +++-
livestatus/src/TableEventConsole.cc | 9 +++--
4 files changed, 44 insertions(+), 28 deletions(-)
diff --git a/livestatus/src/DynamicEventConsoleReplicationColumn.cc
b/livestatus/src/DynamicEventConsoleReplicationColumn.cc
index 0763e62..a36454d 100644
--- a/livestatus/src/DynamicEventConsoleReplicationColumn.cc
+++ b/livestatus/src/DynamicEventConsoleReplicationColumn.cc
@@ -82,9 +82,13 @@ unique_ptr<Column>
DynamicEventConsoleReplicationColumn::createColumn(
const std::string &name, const std::string &arguments) {
string result;
if (_core->mkeventdEnabled()) {
- ECTableConnection ec(_core, "REPLICATE " + arguments);
- ec.run();
- result = ec.getResult();
+ try {
+ ECTableConnection ec(_core, "REPLICATE " + arguments);
+ ec.run();
+ result = ec.getResult();
+ } catch (const generic_error &ge) {
+ // Nothing to do here, returning an empty result is OK.
+ }
}
return make_unique<ReplicationColumn>(name, "replication value", -1,
-1,
result);
diff --git a/livestatus/src/EventConsoleConnection.cc
b/livestatus/src/EventConsoleConnection.cc
index 87606cc..46874c8 100644
--- a/livestatus/src/EventConsoleConnection.cc
+++ b/livestatus/src/EventConsoleConnection.cc
@@ -36,40 +36,31 @@ using std::ostringstream;
using std::string;
EventConsoleConnection::EventConsoleConnection(Logger *logger, string path)
- : _path(move(path)), _socket(-1), _logger(logger, [this](ostream &os) {
- os << "[mkeventd at " << _path << "] ";
- }) {}
+ : _path(move(path))
+ , _name("[mkeventd at " + _path + "]")
+ , _socket(-1)
+ , _logger(logger, [this](ostream &os) { os << _name; }) {}
void EventConsoleConnection::run() {
_socket = socket(PF_UNIX, SOCK_STREAM, 0);
if (_socket == -1) {
- generic_error ge("cannot create socket");
- Alert(&_logger) << ge;
- return;
+ throwGenericError("cannot create socket");
}
-
struct sockaddr_un sa;
sa.sun_family = AF_UNIX;
strncpy(sa.sun_path, _path.c_str(), sizeof(sa.sun_path));
if (connect(_socket, reinterpret_cast<const struct sockaddr *>(&sa),
sizeof(sockaddr_un)) == -1) {
- generic_error ge("cannot connect");
- Alert(&_logger) << ge;
- close(_socket);
- return;
+ throwGenericError("cannot connect");
}
Debug(&_logger) << "successfully connected";
-
if (!writeRequest()) {
- generic_error ge("cannot write");
- Alert(&_logger) << ge;
- } else if (!receiveReply()) {
- generic_error ge("cannot read");
- Alert(&_logger) << ge;
+ throwGenericError("cannot write");
}
-
- Debug(&_logger) << "closing connection";
- close(_socket);
+ if (!receiveReply()) {
+ throwGenericError("cannot read");
+ }
+ close();
}
// TODO(sp) Horribly inefficient, must be replaced.
@@ -95,6 +86,20 @@ bool EventConsoleConnection::getline(string &line) {
return false; // unreachable
}
+void EventConsoleConnection::close() {
+ if (_socket != -1) {
+ Debug(&_logger) << "closing connection";
+ ::close(_socket);
+ }
+}
+
+void EventConsoleConnection::throwGenericError(const string &what_arg) {
+ generic_error ge(_name + " " + what_arg);
+ Alert(&_logger) << ge;
+ close();
+ throw move(ge);
+}
+
bool EventConsoleConnection::writeRequest() {
ostringstream os;
sendRequest(os);
diff --git a/livestatus/src/EventConsoleConnection.h
b/livestatus/src/EventConsoleConnection.h
index cb65078..443f59b 100644
--- a/livestatus/src/EventConsoleConnection.h
+++ b/livestatus/src/EventConsoleConnection.h
@@ -42,9 +42,13 @@ private:
virtual void sendRequest(std::ostream &os) = 0;
virtual bool receiveReply() = 0;
- std::string _path;
- int _socket;
+ void close();
+ void throwGenericError(const std::string &what_arg);
bool writeRequest();
+
+ const std::string _path;
+ const std::string _name;
+ int _socket;
ContextLogger _logger;
};
diff --git a/livestatus/src/TableEventConsole.cc b/livestatus/src/TableEventConsole.cc
index 0237aa2..b5bf706 100644
--- a/livestatus/src/TableEventConsole.cc
+++ b/livestatus/src/TableEventConsole.cc
@@ -100,8 +100,11 @@ TableEventConsole::TableEventConsole(MonitoringCore *core)
void TableEventConsole::answerQuery(Query *query) {
if (_core->mkeventdEnabled()) {
- string internal_name =
- name().substr(12); // skip "eventconsole" prefix :-P
- ECTableConnection(_core, internal_name, query).run();
+ try {
+ // skip "eventconsole" prefix :-P
+ ECTableConnection(_core, name().substr(12), query).run();
+ } catch (const generic_error &ge) {
+ query->invalidRequest(ge.what());
+ }
}
}