Module: check_mk
Branch: master
Commit: bfda5bd548d5b2188d25c9120311fe54cacd1920
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=bfda5bd548d5b2…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Fri Sep 9 15:30:16 2011 +0200
Write messages after initialization into an own livestatus.log
---
ChangeLog | 3 ++
livestatus/src/logger.c | 60 ++++++++++++++++++++++++++++++-----------
livestatus/src/logger.h | 6 ++--
livestatus/src/module.c | 69 ++++++++++++++++++++++++++++-------------------
4 files changed, 91 insertions(+), 47 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 4192dfb..6b94ff5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -5,6 +5,9 @@
* Added display_option "w" to disable limit messages and livestatus errors
in views
* Service Perfdata Sorters are sorting correctly now
+ Livestatus:
+ * Write messages after initialization into an own livestatus.log
+
1.1.11i4:
Core, Setup, etc.:
* FIX: use hostgroups instead of host_groups in Nagios configuration.
diff --git a/livestatus/src/logger.c b/livestatus/src/logger.c
index 471370c..c422ae4 100644
--- a/livestatus/src/logger.c
+++ b/livestatus/src/logger.c
@@ -27,26 +27,54 @@
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
+#include <time.h>
+#include <pthread.h>
-void logger(int priority, const char *loginfo, ...)
+extern char g_logfile_path[];
+pthread_t g_mainthread_id;
+FILE *g_logfile = 0;
+
+void open_logfile()
+{
+ g_logfile = fopen(g_logfile_path, "a");
+ g_mainthread_id = pthread_self(); /* needed to determine main thread later */
+ if (!g_logfile)
+ logger(LG_WARN, "Cannot open logfile %s: %s", g_logfile_path,
strerror(errno));
+}
+
+void close_logfile()
{
- char buffer[8192];
- snprintf(buffer, 20, "livestatus: ");
+ if (g_logfile)
+ fclose(g_logfile);
+}
+void logger(int priority, const char *loginfo, ...)
+{
va_list ap;
va_start(ap, loginfo);
- vsnprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), loginfo, ap);
- va_end(ap);
- write_to_all_logs(buffer, priority);
-
- /* DEBUGING
- FILE *x = fopen("/tmp/hirn.log", "a+");
- va_start(ap, loginfo);
- vfprintf(x, loginfo, ap);
- fputc('\n', x);
- va_end(ap);
- fclose(x);
- */
-}
+ /* Only the main process may use the Nagios log methods */
+ if (g_mainthread_id == pthread_self()) {
+ char buffer[8192];
+ snprintf(buffer, 20, "livestatus: ");
+ vsnprintf(buffer + strlen(buffer),
+ sizeof(buffer) - strlen(buffer), loginfo, ap);
+ va_end(ap);
+ write_to_all_logs(buffer, priority);
+ } else {
+ if (g_logfile) {
+ /* write date/time */
+ char timestring[64];
+ time_t now_t = time(0);
+ struct tm now; localtime_r(&now_t, &now);
+ strftime(timestring, 64, "%F %T ", &now); fputs(timestring,
g_logfile);
+
+ /* write log message */
+ vfprintf(g_logfile, loginfo, ap);
+ fputc('\n', g_logfile);
+ fflush(g_logfile);
+ va_end(ap);
+ }
+ }
+}
diff --git a/livestatus/src/logger.h b/livestatus/src/logger.h
index 29ef47a..b80c65a 100644
--- a/livestatus/src/logger.h
+++ b/livestatus/src/logger.h
@@ -39,9 +39,9 @@
extern "C" {
#endif
-
-
- void logger(int priority, const char *loginfo, ...);
+void logger(int priority, const char *loginfo, ...);
+void open_logfile();
+void close_logfile();
#ifdef __cplusplus
}
diff --git a/livestatus/src/module.c b/livestatus/src/module.c
index 49a23e8..b28aa8a 100644
--- a/livestatus/src/module.c
+++ b/livestatus/src/module.c
@@ -63,36 +63,38 @@
#endif
NEB_API_VERSION(CURRENT_NEB_API_VERSION)
- extern int event_broker_options;
+extern int event_broker_options;
+extern char *log_file;
- int g_idle_timeout_msec = 300 * 1000; /* maximum idle time for connection in keep
alive state */
- int g_query_timeout_msec = 10 * 1000; /* maximum time for reading a query */
+int g_idle_timeout_msec = 300 * 1000; /* maximum idle time for connection in keep alive
state */
+int g_query_timeout_msec = 10 * 1000; /* maximum time for reading a query */
- unsigned g_num_clientthreads = 10; /* allow 10 concurrent connections per default
*/
- size_t g_thread_stack_size = 65536; /* stack size of threads */
+unsigned g_num_clientthreads = 10; /* allow 10 concurrent connections per default */
+size_t g_thread_stack_size = 65536; /* stack size of threads */
#define false 0
#define true 1
- void *g_nagios_handle;
- int g_unix_socket = -1;
- int g_max_fd_ever = 0;
- char g_socket_path[4096];
- char g_pnp_path[4096];
- int g_debug_level = 0;
- int g_should_terminate = false;
- pthread_t g_mainthread_id;
- pthread_t *g_clientthread_id;
- unsigned long g_max_cached_messages = 500000;
- unsigned long g_max_response_size = 100 * 1024 * 1024; // limit answer to 10 MB
- int g_thread_running = 0;
- int g_thread_pid = 0;
- int g_service_authorization = AUTH_LOOSE;
- int g_group_authorization = AUTH_STRICT;
- int g_data_encoding = ENCODING_UTF8;
-
- void* voidp;
+void *g_nagios_handle;
+int g_unix_socket = -1;
+int g_max_fd_ever = 0;
+char g_socket_path[4096];
+char g_pnp_path[4096];
+char g_logfile_path[4096];
+int g_debug_level = 0;
+int g_should_terminate = false;
+pthread_t g_mainthread_id;
+pthread_t *g_clientthread_id;
+unsigned long g_max_cached_messages = 500000;
+unsigned long g_max_response_size = 100 * 1024 * 1024; // limit answer to 10 MB
+int g_thread_running = 0;
+int g_thread_pid = 0;
+int g_service_authorization = AUTH_LOOSE;
+int g_group_authorization = AUTH_STRICT;
+int g_data_encoding = ENCODING_UTF8;
+
+void* voidp;
void livestatus_count_fork()
{
@@ -193,7 +195,6 @@ void *client_thread(void *data __attribute__ ((__unused__)))
void start_threads()
{
- logger(LG_INFO, "Going to open socket and starting threads");
if (!g_thread_running) {
/* start thread that listens on socket */
pthread_atfork(livestatus_count_fork, NULL, livestatus_cleanup_after_fork);
@@ -511,7 +512,15 @@ void check_pnp_path()
void livestatus_parse_arguments(const char *args_orig)
{
/* set default socket path */
- strncpy(g_socket_path, DEFAULT_SOCKET_PATH, sizeof(g_socket_path) - 1);
+ strncpy(g_socket_path, DEFAULT_SOCKET_PATH, sizeof(g_socket_path));
+
+ /* set default path to our logfile to be in the same path as nagios.log */
+ strncpy(g_logfile_path, log_file, sizeof(g_logfile_path) - 16 /* len of
"livestatus.log" */);
+ char *slash = strrchr(g_logfile_path, '/');
+ if (!slash)
+ strcpy(g_logfile_path, "/tmp/livestatus.log");
+ else
+ strcpy(slash + 1, "livestatus.log");
/* there is no default PNP path */
g_pnp_path[0] = 0;
@@ -528,13 +537,16 @@ void livestatus_parse_arguments(const char *args_orig)
char *left = next_token(&part, '=');
char *right = next_token(&part, 0);
if (!right) {
- strncpy(g_socket_path, left, sizeof(g_socket_path) - 1);
+ strncpy(g_socket_path, left, sizeof(g_socket_path));
}
else {
if (!strcmp(left, "debug")) {
g_debug_level = atoi(right);
logger(LG_INFO, "Setting debug level to %d", g_debug_level);
}
+ else if (!strcmp(left, "log_file")) {
+ strncpy(g_logfile_path, right, sizeof(g_logfile_path));
+ }
else if (!strcmp(left, "max_cached_messages")) {
g_max_cached_messages = strtoul(right, 0, 10);
logger(LG_INFO, "Setting max number of cached log messages to
%lu", g_max_cached_messages);
@@ -643,6 +655,7 @@ int nebmodule_init(int flags __attribute__ ((__unused__)), char *args,
void *han
{
g_nagios_handle = handle;
livestatus_parse_arguments(args);
+ open_logfile();
logger(LG_INFO, "Livestatus %s by Mathias Kettner. Socket: '%s'",
VERSION, g_socket_path);
logger(LG_INFO, "Please visit us at
http://mathias-kettner.de/")uot;);
@@ -669,8 +682,7 @@ int nebmodule_init(int flags __attribute__ ((__unused__)), char *args,
void *han
thread the first time one of our callbacks is called. Before
that happens, we haven't got any data anyway... */
- if (g_debug_level > 0)
- logger(LG_INFO, "successfully finished initialization");
+ logger(LG_INFO, "Finished initialization. Further log messages go to %s",
g_logfile_path);
return 0;
}
@@ -682,6 +694,7 @@ int nebmodule_deinit(int flags __attribute__ ((__unused__)), int
reason __attrib
close_unix_socket();
store_deinit();
deregister_callbacks();
+ close_logfile();
return 0;
}