Module: check_mk
Branch: master
Commit: 93a35a26791d1917e4c37c018a5729bed4560301
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=93a35a26791d19…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Tue Aug 30 14:43:52 2016 +0200
Centralize write_to_all_logs. Various tiny cleanups.
---
livestatus/src/Logger.cc | 38 +++++++++++++++---------------------
livestatus/src/TimeperiodsCache.cc | 12 ++++++------
livestatus/src/module.cc | 27 ++++++++++++-------------
3 files changed, 34 insertions(+), 43 deletions(-)
diff --git a/livestatus/src/Logger.cc b/livestatus/src/Logger.cc
index aaed892..7d41390 100644
--- a/livestatus/src/Logger.cc
+++ b/livestatus/src/Logger.cc
@@ -36,8 +36,6 @@ using std::string;
namespace {
FILE *fl_logfile = nullptr;
-int fl_log_level = 5;
-bool fl_log_microtime = false;
} // namespace
void open_logfile(const string &path) {
@@ -56,6 +54,20 @@ void close_logfile() {
}
#ifdef CMC
+
+#include <sys/time.h>
+#include <ctime>
+#include <mutex>
+
+using std::lock_guard;
+using std::mutex;
+
+namespace {
+std::mutex fl_logfile_mutex;
+int fl_log_level = 5;
+bool fl_log_microtime = false;
+} // namespace
+
// Called during a logfile rotation, triggered by an external command.
// This should only do somehting in case the logfile is really open.
void reopen_logfile(const string &path) {
@@ -74,21 +86,6 @@ void set_log_config(int log_level, bool log_microtime) {
bool should_log(int priority) { return priority <= fl_log_level; }
FILE *get_logfile() { return fl_logfile != nullptr ? fl_logfile : stdout; }
-#endif
-
-#ifdef CMC
-
-#include <sys/time.h>
-#include <ctime>
-#include <mutex>
-
-using std::lock_guard;
-using std::mutex;
-
-namespace {
-std::mutex fl_logfile_mutex;
-} // namespace
-
void logger(int priority, const string &message) {
if (!should_log(priority)) {
return; // msg not important enough
@@ -119,16 +116,13 @@ void logger(int priority, const string &message) {
#else
#include <ctime>
-#include "nagios.h"
void logger(int /*priority*/, const string &message) {
extern bool runningInLivestatusMainThread();
+ extern void writeToAllLogs(const string &message);
// Only the main process may use the Nagios log methods
if (fl_logfile == nullptr || runningInLivestatusMainThread()) {
- // TODO(sp) The Nagios headers are (once again) not const-correct...
- write_to_all_logs(
- const_cast<char *>(("livestatus: " + message).c_str()),
- NSLOG_INFO_MESSAGE);
+ writeToAllLogs("livestatus: " + message);
} else if (fl_logfile != nullptr) {
char timestring[64];
time_t now_t = time(nullptr);
diff --git a/livestatus/src/TimeperiodsCache.cc b/livestatus/src/TimeperiodsCache.cc
index eb56023..070b5d2 100644
--- a/livestatus/src/TimeperiodsCache.cc
+++ b/livestatus/src/TimeperiodsCache.cc
@@ -23,16 +23,17 @@
// Boston, MA 02110-1301 USA.
#include "TimeperiodsCache.h"
-#include <syslog.h>
-#include <cstdio>
#include <cstring>
#include <ctime>
#include <ostream>
+#include <string>
#include <utility>
#include "Logger.h"
using std::lock_guard;
using std::mutex;
+using std::string;
+using std::to_string;
extern timeperiod *timeperiod_list;
@@ -129,8 +130,7 @@ bool TimeperiodsCache::inTimeperiod(timeperiod *tp) {
}
void TimeperiodsCache::logTransition(char *name, int from, int to) {
- char buffer[256];
- snprintf(buffer, sizeof(buffer), "TIMEPERIOD TRANSITION: %s;%d;%d", name,
- from, to);
- write_to_all_logs(buffer, LOG_INFO);
+ extern void writeToAllLogs(const string &message);
+ writeToAllLogs(string("TIMEPERIOD TRANSITION: ") + name + ";" +
+ to_string(from) + ";" + to_string(to));
}
diff --git a/livestatus/src/module.cc b/livestatus/src/module.cc
index 54e462d..8e63566 100644
--- a/livestatus/src/module.cc
+++ b/livestatus/src/module.cc
@@ -38,7 +38,6 @@
#include <cerrno>
#include <cstddef>
#include <cstdint>
-#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
@@ -143,6 +142,11 @@ void count_services() {
}
}
+void writeToAllLogs(const string &message) {
+ // TODO(sp) The Nagios headers are (once again) not const-correct...
+ write_to_all_logs(const_cast<char *>(message.c_str()), NSLOG_INFO_MESSAGE);
+}
+
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;
@@ -486,22 +490,17 @@ void livestatus_log_initial_states() {
// Log DOWNTIME hosts
for (host *h = host_list; h != nullptr; h = h->next) {
if (h->scheduled_downtime_depth > 0) {
- char buffer[8192];
- snprintf(buffer, sizeof(buffer),
- "HOST DOWNTIME ALERT: %s;STARTED;%s", h->name,
- get_downtime_comment(h->name, nullptr));
- write_to_all_logs(buffer, NSLOG_INFO_MESSAGE);
+ writeToAllLogs(string("HOST DOWNTIME ALERT: ") + h->name +
+ ";STARTED;" +
+ get_downtime_comment(h->name, nullptr));
}
}
// Log DOWNTIME services
for (service *s = service_list; s != nullptr; s = s->next) {
if (s->scheduled_downtime_depth > 0) {
- char buffer[8192];
- snprintf(buffer, sizeof(buffer),
- "SERVICE DOWNTIME ALERT: %s;%s;STARTED;%s",
s->host_name,
- s->description,
- get_downtime_comment(s->host_name, s->description));
- write_to_all_logs(buffer, NSLOG_INFO_MESSAGE);
+ writeToAllLogs(string("SERVICE DOWNTIME ALERT: ") + s->host_name
+
+ ";" + s->description + ";STARTED;" +
+ get_downtime_comment(s->host_name, s->description));
}
}
// Log TIMERPERIODS
@@ -517,9 +516,7 @@ int broker_event(int event_type __attribute__((__unused__)), void
*data) {
livestatus_log_initial_states();
} else if (log_initial_states == 1) {
// initial info during startup
- // TODO(sp) The Nagios headers are (once again) not const-correct...
- write_to_all_logs(const_cast<char *>("logging initial
states"),
- NSLOG_INFO_MESSAGE);
+ writeToAllLogs("logging initial states");
}
}
g_timeperiods_cache->update(ts->timestamp.tv_sec);