Module: check_mk
Branch: master
Commit: 0062ed33e50a248b54abb338709481d7ba753b71
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=0062ed33e50a24…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Wed Jun 1 13:22:00 2016 +0200
Better matching for hosts while joining the EC tables, Livestatus part.
This is basically handled like it is in CMC, but note one detail: The
initialization of some parts had to be moved to a later point in time in the
broker module lifecycle, because only then we know the hosts.
We should really introduce a kind of "resolver" object for stuff like this
and make it accessible to the parts which need it. Currently this is all
global stuff :-P in Livestatus, while in CMC it is part of the World.
Passing the World (or even the whole Core to which it belongs) around just
to access a tiny bit of it needlessly increases coupling, so this is a bit
ugly.
---
livestatus/src/TableEventConsole.cc | 5 +++--
livestatus/src/module.c | 2 +-
livestatus/src/store_c.cc | 27 +++++++++++++++++++++++++++
livestatus/src/store_c.h | 1 +
4 files changed, 32 insertions(+), 3 deletions(-)
diff --git a/livestatus/src/TableEventConsole.cc b/livestatus/src/TableEventConsole.cc
index e9dd5dc..b1cfc04 100644
--- a/livestatus/src/TableEventConsole.cc
+++ b/livestatus/src/TableEventConsole.cc
@@ -54,6 +54,8 @@
#ifdef CMC
#include "Core.h"
#include "World.h"
+#else
+#include "store_c.h"
#endif
// using boost::asio::local::stream_protocol;
@@ -268,10 +270,9 @@ bool TableEventConsole::receiveReply(int sock, Query *query) {
? nullptr
: _core->_world->getHostByDesignation(it->second);
#else
- // Older Nagios headers are not const-correct... :-P
row._host = it == row._map.end()
? nullptr
- : find_host(const_cast<char
*>(it->second.c_str()));
+ : getHostByDesignation(it->second.c_str());
#endif
query->processDataset(&row);
}
diff --git a/livestatus/src/module.c b/livestatus/src/module.c
index 1f61045..d58b5bc 100644
--- a/livestatus/src/module.c
+++ b/livestatus/src/module.c
@@ -515,6 +515,7 @@ int broker_process(int event_type __attribute__((__unused__)), void
*data) {
struct nebstruct_process_struct *ps =
(struct nebstruct_process_struct *)data;
if (ps->type == NEBTYPE_PROCESS_EVENTLOOPSTART) {
+ store_init();
update_timeperiods_cache(time(0));
start_threads();
}
@@ -874,7 +875,6 @@ int nebmodule_init(int flags __attribute__((__unused__)), char *args,
"the overall nagios performance");
}
- store_init();
register_callbacks();
/* Unfortunately, we cannot start our socket thread right now.
diff --git a/livestatus/src/store_c.cc b/livestatus/src/store_c.cc
index c5b8148..c9bc395 100644
--- a/livestatus/src/store_c.cc
+++ b/livestatus/src/store_c.cc
@@ -23,17 +23,39 @@
// Boston, MA 02110-1301 USA.
#include "store_c.h"
+#include <string>
+#include <unordered_map>
+#include <utility>
#include "ClientQueue.h"
#include "InputBuffer.h"
#include "OutputBuffer.h"
#include "Store.h"
+#include "StringUtils.h"
#include "TimeperiodsCache.h"
+extern host *host_list;
+
+using mk::unsafe_tolower;
+using std::string;
+using std::unordered_map;
+
+// Map to speed up access via name/alias/address
+unordered_map<string, host *> fl_hosts_by_designation;
+
static Store *fl_store = nullptr;
static ClientQueue *fl_client_queue = nullptr;
TimeperiodsCache *g_timeperiods_cache = nullptr;
void store_init() {
+ for (host *hst = host_list; hst != nullptr; hst = hst->next) {
+ if (const char *address = hst->address) {
+ fl_hosts_by_designation[unsafe_tolower(address)] = hst;
+ }
+ if (const char *alias = hst->alias) {
+ fl_hosts_by_designation[unsafe_tolower(alias)] = hst;
+ }
+ fl_hosts_by_designation[unsafe_tolower(hst->name)] = hst;
+ }
fl_store = new Store();
fl_client_queue = new ClientQueue();
g_timeperiods_cache = new TimeperiodsCache();
@@ -86,3 +108,8 @@ void delete_inputbuffer(void *ib) { delete static_cast<InputBuffer
*>(ib); }
void update_timeperiods_cache(time_t now) { g_timeperiods_cache->update(now); }
void log_timeperiods_cache() { g_timeperiods_cache->logCurrentTimeperiods(); }
+
+host *getHostByDesignation(const char *designation) {
+ auto it = fl_hosts_by_designation.find(unsafe_tolower(designation));
+ return it == fl_hosts_by_designation.end() ? nullptr : it->second;
+}
diff --git a/livestatus/src/store_c.h b/livestatus/src/store_c.h
index 7b18ed5..c3ac4c5 100644
--- a/livestatus/src/store_c.h
+++ b/livestatus/src/store_c.h
@@ -48,6 +48,7 @@ int queue_pop_connection();
void queue_terminate();
void update_timeperiods_cache(time_t);
void log_timeperiods_cache();
+host *getHostByDesignation(const char *designation);
#ifdef __cplusplus
}