[Pkg-nagios-changes] [SCM] Debian packaging for mod gearman. branch, master, updated. debian/1.0.3-1-28-ga8ba0c2

Sven Nierlein sven at nierlein.org
Fri May 20 10:20:56 UTC 2011


The following commit has been merged in the master branch:
commit 4d32e639175c01c8554173ea21b83f2c227231bd
Author: Sven Nierlein <sven at nierlein.org>
Date:   Thu Apr 14 23:40:48 2011 +0200

    added generic logger
    
     - enables logging to file, stdout, syslog or nagios

diff --git a/.gitignore b/.gitignore
index fbdd5ab..49044d9 100644
--- a/.gitignore
+++ b/.gitignore
@@ -18,6 +18,7 @@ mod_gearman_worker
 01_utils
 02_full
 03_exec
+04_log
 images
 send_gearman
 send_multi
diff --git a/Changes b/Changes
index 1a16926..62e48fc 100644
--- a/Changes
+++ b/Changes
@@ -1,6 +1,8 @@
 This file documents the revision history for mod_gearman.
 
-1.0.4  Wed...
+1.0.4  Sun Apr 17 17:58:47 CEST 2011
+          - added generic logger
+            - enables logging to stdout, file, syslog or nagios
           - changed latency calculation (use time of next_check instead of time of job submission)
           - added nsca replacements docs
 
diff --git a/Makefile.am b/Makefile.am
index a196fcc..6ad0125 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -29,7 +29,6 @@ common_SOURCES             = common/base64.c \
 
 pkglib_LIBRARIES           = mod_gearman.so
 mod_gearman_so_SOURCES     = $(common_SOURCES) \
-                             neb_module/gm_log.c \
                              neb_module/result_thread.c \
                              neb_module/mod_gearman.c
 
@@ -40,32 +39,28 @@ bin_PROGRAMS               = mod_gearman_worker \
                              gearman_top
 
 mod_gearman_worker_SOURCES = $(common_SOURCES) \
-                             worker/worker_logger.c   \
                              worker/worker_client.c   \
                              worker/worker.c
 
 send_gearman_SOURCES       = $(common_SOURCES) \
-                             tools/tools_logger.c \
                              tools/send_gearman.c
 
 send_multi_SOURCES         = $(common_SOURCES) \
-                             tools/tools_logger.c \
                              tools/send_multi.c
 
 check_gearman_SOURCES      = $(common_SOURCES) \
-                             tools/tools_logger.c \
                              tools/check_gearman.c
 
 gearman_top_SOURCES        = $(common_SOURCES) \
-                             tools/tools_logger.c \
                              tools/gearman_top.c
 gearman_top_LDFLAGS        = -lncurses
 
 # tests
-check_PROGRAMS   = 01_utils 02_full 03_exec
-01_utils_SOURCES = $(common_SOURCES) t/tap.h t/tap.c worker/worker_logger.c t/01-utils.c
-02_full_SOURCES  = $(common_SOURCES) t/tap.h t/tap.c worker/worker_logger.c t/02-full.c
-03_exec_SOURCES  = $(common_SOURCES) t/tap.h t/tap.c worker/worker_logger.c t/03-exec_checks.c
+check_PROGRAMS   = 01_utils 02_full 03_exec 04_log
+01_utils_SOURCES = $(common_SOURCES) t/tap.h t/tap.c t/01-utils.c
+02_full_SOURCES  = $(common_SOURCES) t/tap.h t/tap.c t/02-full.c
+03_exec_SOURCES  = $(common_SOURCES) t/tap.h t/tap.c t/03-exec_checks.c
+04_log_SOURCES   = $(common_SOURCES) t/tap.h t/tap.c t/04-log.c
 TESTS            = $(check_PROGRAMS)
 
 
diff --git a/common/utils.c b/common/utils.c
index 57fcd14..48e9542 100644
--- a/common/utils.c
+++ b/common/utils.c
@@ -22,7 +22,6 @@
  *****************************************************************************/
 
 #include "utils.h"
-#include "gm_log.h"
 #include "crypt.h"
 #include "base64.h"
 
@@ -198,6 +197,7 @@ int set_default_options(mod_gm_opt_t *opt) {
     opt->result_queue       = NULL;
     opt->keyfile            = NULL;
     opt->logfile            = NULL;
+    opt->logmode            = GM_LOG_MODE_AUTO;
     opt->logfile_fp         = NULL;
     opt->message            = NULL;
     opt->return_code        = 0;
@@ -384,6 +384,29 @@ int parse_args_line(mod_gm_opt_t *opt, char * arg, int recursion_level) {
         if(opt->debug_level < 0) { opt->debug_level = 0; }
     }
 
+    /* logmode */
+    else if ( !strcmp( key, "logmode" ) ) {
+        opt->logmode = GM_LOG_MODE_AUTO;
+        if ( !strcmp( value, "automatic" ) ) {
+            opt->logmode = GM_LOG_MODE_AUTO;
+        }
+        else if ( !strcmp( value, "file" ) ) {
+            opt->logmode = GM_LOG_MODE_FILE;
+        }
+        else if ( !strcmp( value, "stdout" ) ) {
+            opt->logmode = GM_LOG_MODE_STDOUT;
+        }
+        else if ( !strcmp( value, "syslog" ) ) {
+            opt->logmode = GM_LOG_MODE_SYSLOG;
+        }
+        else if ( !strcmp( value, "core" ) ) {
+            opt->logmode = GM_LOG_MODE_CORE;
+        }
+        else {
+            gm_log( GM_LOG_ERROR, "unknown log mode '%s', use one of 'automatic', 'file', 'stdout', 'syslog' and 'core'\n", value );
+        }
+    }
+
     /* result worker */
     else if ( !strcmp( key, "result_workers" ) ) {
         opt->result_workers = atoi( value );
@@ -710,6 +733,20 @@ void dumpconfig(mod_gm_opt_t *opt, int mode) {
     gm_log( GM_LOG_DEBUG, "--------------------------------\n" );
     gm_log( GM_LOG_DEBUG, "configuration:\n" );
     gm_log( GM_LOG_DEBUG, "log level:           %d\n", opt->debug_level);
+
+    if(opt->logmode == GM_LOG_MODE_AUTO)
+        gm_log( GM_LOG_DEBUG, "log mode:            auto (%d)\n", opt->logmode);
+    if(opt->logmode == GM_LOG_MODE_FILE)
+        gm_log( GM_LOG_DEBUG, "log mode:            file (%d)\n", opt->logmode);
+    if(opt->logmode == GM_LOG_MODE_STDOUT)
+        gm_log( GM_LOG_DEBUG, "log mode:            stdout (%d)\n", opt->logmode);
+    if(opt->logmode == GM_LOG_MODE_CORE)
+        gm_log( GM_LOG_DEBUG, "log mode:            core (%d)\n", opt->logmode);
+    if(opt->logmode == GM_LOG_MODE_SYSLOG)
+        gm_log( GM_LOG_DEBUG, "log mode:            syslog (%d)\n", opt->logmode);
+    if(opt->logmode == GM_LOG_MODE_TOOLS)
+        gm_log( GM_LOG_DEBUG, "log mode:            tools (%d)\n", opt->logmode);
+
     if(mode == GM_WORKER_MODE) {
         gm_log( GM_LOG_DEBUG, "identifier:          %s\n", opt->identifier);
         gm_log( GM_LOG_DEBUG, "pidfile:             %s\n", opt->pidfile == NULL ? "no" : opt->pidfile);
@@ -1218,7 +1255,7 @@ int execute_safe_command(gm_job_t * exec_job, int fork_exec, char * hostname) {
 
 
 /* set empty default job */
-int set_default_job(gm_job_t *job, mod_gm_opt_t *mod_gm_opt) {
+int set_default_job(gm_job_t *job, mod_gm_opt_t *opt) {
 
     job->type                = NULL;
     job->host_name           = NULL;
@@ -1231,7 +1268,7 @@ int set_default_job(gm_job_t *job, mod_gm_opt_t *mod_gm_opt) {
     job->reschedule_check    = TRUE;
     job->return_code         = STATE_OK;
     job->latency             = 0.0;
-    job->timeout             = mod_gm_opt->job_timeout;
+    job->timeout             = opt->job_timeout;
     job->start_time.tv_sec   = 0L;
     job->start_time.tv_usec  = 0L;
 
@@ -1645,3 +1682,103 @@ char * eventtype2str(int i) {
     }
     return strdup("UNKNOWN");
 }
+
+/* generic logger function */
+void gm_log( int lvl, const char *text, ... ) {
+    FILE * fp       = NULL;
+    int debug_level = GM_LOG_ERROR;
+    int logmode     = GM_LOG_MODE_STDOUT;
+    int slevel;
+    char * level;
+    char buffer1[GM_BUFFERSIZE];
+    char buffer2[GM_BUFFERSIZE];
+    char buffer3[GM_BUFFERSIZE];
+    time_t t;
+    va_list ap;
+    struct tm now;
+
+    if(mod_gm_opt != NULL) {
+        debug_level = mod_gm_opt->debug_level;
+        logmode     = mod_gm_opt->logmode;
+        fp          = mod_gm_opt->logfile_fp;
+    }
+
+    if(logmode == GM_LOG_MODE_CORE) {
+        if ( debug_level < 0 )
+            return;
+        if ( lvl != GM_LOG_ERROR && lvl > debug_level )
+            return;
+
+        if ( lvl == GM_LOG_ERROR ) {
+            snprintf( buffer1, 22, "mod_gearman: ERROR - " );
+        } else {
+            snprintf( buffer1, 14, "mod_gearman: " );
+        }
+        va_start( ap, text );
+        vsnprintf( buffer1 + strlen( buffer1 ), sizeof( buffer1 ) - strlen( buffer1 ), text, ap );
+        va_end( ap );
+
+        if ( debug_level >= GM_LOG_STDOUT ) {
+            printf( "%s", buffer1 );
+            return;
+        }
+        write_core_log( buffer1 );
+        return;
+    }
+
+    /* check log level */
+    if ( lvl != GM_LOG_ERROR && lvl > debug_level ) {
+        return;
+    }
+    if ( lvl == GM_LOG_ERROR ) {
+        level  = "ERROR";
+        slevel = LOG_ERR;
+    }
+    else if ( lvl == GM_LOG_INFO ) {
+        level  = "INFO ";
+        slevel = LOG_INFO;
+    }
+    else if ( lvl == GM_LOG_DEBUG ) {
+        level  = "DEBUG";
+        slevel = LOG_DEBUG;
+    }
+    else if ( lvl == GM_LOG_TRACE ) {
+        level  = "TRACE";
+        slevel = LOG_DEBUG;
+    }
+    else {
+        level = "UNKNOWN";
+        slevel = LOG_DEBUG;
+    }
+
+    /* set timestring */
+    t = time(NULL);
+    localtime_r(&t, &now);
+    strftime(buffer1, sizeof(buffer1), "[%Y-%m-%d %H:%M:%S]", &now );
+
+    /* set timestring */
+    snprintf(buffer2, sizeof(buffer2), "[%i][%s]", getpid(), level );
+
+    va_start( ap, text );
+    vsnprintf( buffer3, GM_BUFFERSIZE, text, ap );
+    va_end( ap );
+
+    if ( debug_level >= GM_LOG_STDOUT || logmode == GM_LOG_MODE_TOOLS ) {
+        printf( "%s", buffer3 );
+        return;
+    }
+
+    if(logmode == GM_LOG_MODE_FILE && fp != NULL) {
+        fprintf( fp, "%s%s %s", buffer1, buffer2, buffer3 );
+        fflush( fp );
+    }
+    else if(logmode == GM_LOG_MODE_SYSLOG) {
+        syslog(slevel , "%s %s", buffer2, buffer3 );
+    }
+    else {
+        /* stdout logging */
+        printf( "%s%s %s", buffer1, buffer2, buffer3 );
+    }
+
+    return;
+}
diff --git a/extras/shared.conf b/extras/shared.conf
index bb1a5cd..8e0d603 100644
--- a/extras/shared.conf
+++ b/extras/shared.conf
@@ -1,11 +1,11 @@
 ###############################################################################
-# 
+#
 #  mod_gearman - distribute checks with gearman
-# 
+#
 #  Copyright (c) 2010 Sven Nierlein
-# 
+#
 #  Sample Shared Worker / NEB Module Config
-# 
+#
 ###############################################################################
 
 # use debug to increase the verbosity of the module.
@@ -18,6 +18,22 @@
 debug=0
 
 
+# set the way of logging
+# Possible values are:
+#     automatic
+#          -> logfile when a logfile is specified
+#          -> stdout for tools
+#     file
+#          -> use logfile
+#     stdout
+#           -> just print all messages to stdout
+#     core
+#           -> use nagios internal loging (not thread safe! Use with care)
+#     syslog
+#           -> use syslog for all log events
+logmode=automatic
+
+
 # sets the addess of your gearman job server. Can be specified
 # more than once to add more server.
 server=localhost:4730
diff --git a/extras/standalone_worker.conf b/extras/standalone_worker.conf
index 2eb405e..771800e 100644
--- a/extras/standalone_worker.conf
+++ b/extras/standalone_worker.conf
@@ -1,11 +1,11 @@
 ###############################################################################
-# 
+#
 #  mod_gearman - distribute checks with gearman
-# 
+#
 #  Copyright (c) 2010 Sven Nierlein
-# 
+#
 #  Sample Standalone Worker Config
-# 
+#
 ###############################################################################
 
 # use debug to increase the verbosity of the module.
@@ -18,6 +18,20 @@
 debug=0
 
 
+# set the way of logging
+# Possible values are:
+#     automatic
+#          -> logfile when a logfile is specified
+#          -> stdout for tools
+#     file
+#          -> use logfile
+#     stdout
+#           -> just print all messages to stdout
+#     syslog
+#           -> use syslog for all log events
+logmode=automatic
+
+
 # sets the addess of your gearman job server. Can be specified
 # more than once to add more server.
 server=localhost:4730
diff --git a/include/check_gearman.h b/include/check_gearman.h
index a7650ee..5bd970a 100644
--- a/include/check_gearman.h
+++ b/include/check_gearman.h
@@ -40,9 +40,6 @@
 #include <signal.h>
 #include "common.h"
 
-mod_gm_opt_t *mod_gm_opt;                /**< global options structure */
-
-
 /** check_gearman
  *
  * main function of check_gearman
@@ -105,4 +102,3 @@ int check_worker(char * queue, char * send, char * expect);
 /**
  * @}
  */
-
diff --git a/include/common.h b/include/common.h
index 2311938..3072bf2 100644
--- a/include/common.h
+++ b/include/common.h
@@ -73,6 +73,14 @@
 #define GM_LOG_TRACE                    2
 #define GM_LOG_STDOUT                   3
 
+/* log modes */
+#define GM_LOG_MODE_AUTO                0
+#define GM_LOG_MODE_FILE                1
+#define GM_LOG_MODE_STDOUT              2
+#define GM_LOG_MODE_CORE                3
+#define GM_LOG_MODE_SYSLOG              4
+#define GM_LOG_MODE_TOOLS               5
+
 /* job priorities */
 #define GM_JOB_PRIO_LOW                 1
 #define GM_JOB_PRIO_NORMAL              2
@@ -165,6 +173,9 @@ typedef struct mod_gm_opt_struct {
     int            job_timeout;                             /**< override job timeout */
     int            encryption;                              /**< flag wheter messages are encrypted */
     int            transportmode;                           /**< flag for the transportmode, base64 only or base64 and encrypted  */
+    int            logmode;                                 /**< logmode: auto, syslog, file or nagios */
+    char         * logfile;                                 /**< path for the logfile */
+    FILE         * logfile_fp;                              /**< filedescriptor for the logfile */
 /* neb module */
     char         * result_queue;                            /**< name of the result queue used by the neb module */
     int            result_workers;                          /**< number of result worker threads started */
@@ -180,8 +191,6 @@ typedef struct mod_gm_opt_struct {
 /* worker */
     char         * identifier;                              /**< identifier for this worker */
     char         * pidfile;                                 /**< path to a pidfile */
-    char         * logfile;                                 /**< path for the logfile */
-    FILE         * logfile_fp;                              /**< filedescriptor for the logfile */
     int            daemon_mode;                             /**< running as daemon ot not? */
     int            debug_result;                            /**< flag to write a debug file for each result */
     int            max_age;                                 /**< max age in seconds for new jobs */
@@ -226,20 +235,8 @@ typedef struct gm_job_struct {
 } gm_job_t;
 
 
-/**
- * general logger
- *
- * logger is then defined in worker_logger.c
- * and the neb logger in logger.c
- * tools logger is in the tools_logger.c
- *
- * @param[in] lvl  - debug level for this message
- * @param[in] text - text to log
- *
- * @return nothing
- */
-void gm_log( int lvl, const char *text, ... );
-
+/** options structure */
+mod_gm_opt_t *mod_gm_opt;
 
 /*
  * @}
diff --git a/include/gearman_top.h b/include/gearman_top.h
index 508fba1..3b99dac 100644
--- a/include/gearman_top.h
+++ b/include/gearman_top.h
@@ -38,8 +38,6 @@
 #include <time.h>
 #include "common.h"
 
-mod_gm_opt_t *mod_gm_opt;            /**< global options structure */
-
 /** gearman_top
  *
  * main function of gearman_top
diff --git a/include/gm_log.h b/include/gm_log.h
deleted file mode 100644
index 0114800..0000000
--- a/include/gm_log.h
+++ /dev/null
@@ -1,29 +0,0 @@
-/******************************************************************************
- *
- * mod_gearman - distribute checks with gearman
- *
- * Copyright (c) 2010 Sven Nierlein - sven.nierlein at consol.de
- *
- * This file is part of mod_gearman.
- *
- *  mod_gearman is free software: you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation, either version 3 of the License, or
- *  (at your option) any later version.
- *
- *  mod_gearman is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with mod_gearman.  If not, see <http://www.gnu.org/licenses/>.
- *
- *****************************************************************************/
-
-/** @file
- *  @brief header for the log module
- */
-
-#include "nagios/nagios.h"
-
diff --git a/include/mod_gearman.h b/include/mod_gearman.h
index 9bc6616..70a1e99 100644
--- a/include/mod_gearman.h
+++ b/include/mod_gearman.h
@@ -22,8 +22,10 @@
  *****************************************************************************/
 
 #define MOD_GM_NEB  /**< set mod_gearman neb features */
+#define NSCORE      /**< enable core features         */
 
-#include <common.h>
+#include "utils.h"
+#include "common.h"
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -32,7 +34,6 @@
 #include <pthread.h>
 
 #define GM_PERFDATA_QUEUE    "perfdata"  /**< default performance data queue */
-#define NSCORE                           /**< enable core features           */
 
 /** @file
  *  @brief Mod-Gearman NEB Module
@@ -91,9 +92,6 @@ int nebmodule_deinit( int flags, int reason );
  */
 void mod_gm_add_result_to_list(check_result * newcheckresult);
 
-/** options structure */
-mod_gm_opt_t *mod_gm_opt;
-
 /**
  * @}
  */
diff --git a/include/send_gearman.h b/include/send_gearman.h
index fdfbcab..c41e2e2 100644
--- a/include/send_gearman.h
+++ b/include/send_gearman.h
@@ -42,9 +42,6 @@
 #include <libgearman/gearman.h>
 #include "common.h"
 
-mod_gm_opt_t *mod_gm_opt;            /**< global options structure */
-
-
 /** send_gearman
  *
  * main function of gearman_top
@@ -115,4 +112,3 @@ void alarm_sighandler(int sig);
 /**
  * @}
  */
-
diff --git a/include/send_multi.h b/include/send_multi.h
index 6e40a7c..3cc2147 100644
--- a/include/send_multi.h
+++ b/include/send_multi.h
@@ -43,8 +43,6 @@
 #include <libgearman/gearman.h>
 #include "common.h"
 
-mod_gm_opt_t *mod_gm_opt;   /**< options structure */
-
 /** send_multi
  *
  * main function of send_multi
@@ -155,4 +153,3 @@ char *decode_xml(char * xml);
 /**
  * @}
  */
-
diff --git a/include/tools_logger.h b/include/tools_logger.h
deleted file mode 100644
index aec8cb6..0000000
--- a/include/tools_logger.h
+++ /dev/null
@@ -1,33 +0,0 @@
-/******************************************************************************
- *
- * mod_gearman - distribute checks with gearman
- *
- * Copyright (c) 2010 Sven Nierlein - sven.nierlein at consol.de
- *
- * This file is part of mod_gearman.
- *
- *  mod_gearman is free software: you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation, either version 3 of the License, or
- *  (at your option) any later version.
- *
- *  mod_gearman is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with mod_gearman.  If not, see <http://www.gnu.org/licenses/>.
- *
- *****************************************************************************/
-
-/** @file
- *  @brief header for the tools logger component
- */
-
-#include <stdio.h>
-#include <string.h>
-#include <time.h>
-#include <stdarg.h>
-#include <unistd.h>
-
diff --git a/include/utils.h b/include/utils.h
index a3687d7..b90514f 100644
--- a/include/utils.h
+++ b/include/utils.h
@@ -27,6 +27,19 @@
  *  @{
  */
 
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+#include <stdarg.h>
+#include <unistd.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include <syslog.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <sys/stat.h>
+
 #include "common.h"
 
 /**
@@ -431,8 +444,26 @@ char * nebcallback2str(int i);
  */
 char * eventtype2str(int i);
 
+/**
+ * gm_log
+ *
+ * general logger
+ *
+ * @param[in] lvl  - debug level for this message
+ * @param[in] text - text to log
+ *
+ * @return nothing
+ */
+void gm_log( int lvl, const char *text, ... );
+
+/** write log line with core logger
+ *
+ * @param[in] data - log message
+ *
+ * @return nothing
+ */
+void write_core_log(char *data);
 
 /**
  * @}
  */
-
diff --git a/include/worker.h b/include/worker.h
index 02cfb0c..0e73833 100644
--- a/include/worker.h
+++ b/include/worker.h
@@ -44,7 +44,6 @@
  */
 
 int mod_gm_shm_key;        /**< key for the shared memory segment */
-mod_gm_opt_t *mod_gm_opt;  /**< global options structure          */
 
 /** Mod-Gearman Worker
  *
@@ -193,4 +192,3 @@ void count_current_worker(void);
 /**
  * @}
  */
-
diff --git a/include/worker_logger.h b/include/worker_logger.h
deleted file mode 100644
index a7a096e..0000000
--- a/include/worker_logger.h
+++ /dev/null
@@ -1,33 +0,0 @@
-/******************************************************************************
- *
- * mod_gearman - distribute checks with gearman
- *
- * Copyright (c) 2010 Sven Nierlein - sven.nierlein at consol.de
- *
- * This file is part of mod_gearman.
- *
- *  mod_gearman is free software: you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation, either version 3 of the License, or
- *  (at your option) any later version.
- *
- *  mod_gearman is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with mod_gearman.  If not, see <http://www.gnu.org/licenses/>.
- *
- *****************************************************************************/
-
-/** @file
- *  @brief header for mod-gearman worker logger
- */
-
-#include <stdio.h>
-#include <string.h>
-#include <time.h>
-#include <stdarg.h>
-#include <unistd.h>
-
diff --git a/neb_module/gm_log.c b/neb_module/gm_log.c
deleted file mode 100644
index cdc7b8d..0000000
--- a/neb_module/gm_log.c
+++ /dev/null
@@ -1,55 +0,0 @@
-/******************************************************************************
- *
- * mod_gearman - distribute checks with gearman
- *
- * Copyright (c) 2010 Sven Nierlein - sven.nierlein at consol.de
- *
- * This file is part of mod_gearman.
- *
- *  mod_gearman is free software: you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation, either version 3 of the License, or
- *  (at your option) any later version.
- *
- *  mod_gearman is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with mod_gearman.  If not, see <http://www.gnu.org/licenses/>.
- *
- *****************************************************************************/
-
-#include "common.h"
-#include "mod_gearman.h"
-#include "gm_log.h"
-
-void gm_log( int lvl, const char *text, ... ) {
-    char buffer[GM_BUFFERSIZE];
-    va_list ap;
-
-    /* check log level */
-    if ( mod_gm_opt->debug_level < 0 )
-        return;
-    if ( lvl != GM_LOG_ERROR && lvl > mod_gm_opt->debug_level )
-        return;
-
-    if ( lvl == GM_LOG_ERROR ) {
-        snprintf( buffer, 22, "mod_gearman: ERROR - " );
-    } else {
-        snprintf( buffer, 14, "mod_gearman: " );
-    }
-    va_start( ap, text );
-    vsnprintf( buffer + strlen( buffer ), sizeof( buffer ) - strlen( buffer ), text, ap );
-    va_end( ap );
-
-    /* in case of stdout logging just print and return */
-    if ( mod_gm_opt->debug_level >= GM_LOG_STDOUT ) {
-        printf( "%s", buffer );
-        return;
-    }
-
-    /* send everything as info message to the core */
-    write_to_all_logs( buffer, NSLOG_INFO_MESSAGE );
-}
diff --git a/neb_module/mod_gearman.c b/neb_module/mod_gearman.c
index 8957e59..eafa4e1 100644
--- a/neb_module/mod_gearman.c
+++ b/neb_module/mod_gearman.c
@@ -22,10 +22,8 @@
  *****************************************************************************/
 
 /* include header */
-#include "utils.h"
 #include "result_thread.h"
 #include "mod_gearman.h"
-#include "gm_log.h"
 #include "gearman.h"
 
 /* specify event broker API version (required) */
@@ -712,14 +710,26 @@ static int read_arguments( const char *args_orig ) {
 
 /* verify our option */
 static int verify_options(mod_gm_opt_t *opt) {
+
+    /* open new logfile */
+    if ( opt->logmode == GM_LOG_MODE_AUTO && opt->logfile ) {
+        opt->logmode = GM_LOG_MODE_FILE;
+    }
+    if(opt->logmode == GM_LOG_MODE_FILE && opt->logfile && opt->debug_level < GM_LOG_STDOUT) {
+        opt->logfile_fp = fopen(opt->logfile, "a+");
+        if(opt->logfile_fp == NULL) {
+            gm_log( GM_LOG_ERROR, "error opening logfile: %s\n", opt->logfile );
+        }
+    }
+
     /* did we get any server? */
     if(opt->server_num == 0) {
         gm_log( GM_LOG_ERROR, "please specify at least one server\n" );
         return(GM_ERROR);
     }
 
-    if ( mod_gm_opt->result_queue == NULL )
-        mod_gm_opt->result_queue = GM_DEFAULT_RESULT_QUEUE;
+    if ( opt->result_queue == NULL )
+        opt->result_queue = GM_DEFAULT_RESULT_QUEUE;
 
     /* nothing set by hand -> defaults */
     if( opt->set_queues_by_hand == 0 ) {
@@ -1111,3 +1121,10 @@ int handle_export(int callback_type, void *data) {
     mod_gm_opt->debug_level = debug_level_orig;
     return return_code;
 }
+
+
+/* core log wrapper */
+void write_core_log(char *data) {
+    write_to_all_logs( data, NSLOG_INFO_MESSAGE );
+    return;
+}
\ No newline at end of file
diff --git a/neb_module/result_thread.c b/neb_module/result_thread.c
index f796446..38917b8 100644
--- a/neb_module/result_thread.c
+++ b/neb_module/result_thread.c
@@ -26,7 +26,6 @@
 #include "result_thread.h"
 #include "utils.h"
 #include "mod_gearman.h"
-#include "gm_log.h"
 #include "gearman.h"
 
 /* cleanup and exit this thread */
diff --git a/t/01-utils.c b/t/01-utils.c
index a1bf65e..e018a26 100644
--- a/t/01-utils.c
+++ b/t/01-utils.c
@@ -4,7 +4,6 @@
 #include <unistd.h>
 
 #include <t/tap.h>
-#include <worker_logger.h>
 #include <common.h>
 #include <utils.h>
 
diff --git a/t/02-full.c b/t/02-full.c
index 2ff8ba8..dc8f36d 100644
--- a/t/02-full.c
+++ b/t/02-full.c
@@ -7,7 +7,6 @@
 #include <sys/time.h>
 
 #include <t/tap.h>
-#include <worker_logger.h>
 #include <common.h>
 #include <utils.h>
 #include <gearman.h>
diff --git a/t/03-exec_checks.c b/t/03-exec_checks.c
index 497541e..e7260ff 100644
--- a/t/03-exec_checks.c
+++ b/t/03-exec_checks.c
@@ -4,7 +4,6 @@
 #include <unistd.h>
 
 #include <t/tap.h>
-#include <worker_logger.h>
 #include <common.h>
 #include <utils.h>
 
diff --git a/t/04-log.c b/t/04-log.c
new file mode 100644
index 0000000..8a50308
--- /dev/null
+++ b/t/04-log.c
@@ -0,0 +1,43 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <signal.h>
+#include <sys/wait.h>
+#include <sys/time.h>
+
+#include <t/tap.h>
+#include <common.h>
+#include <utils.h>
+
+
+mod_gm_opt_t *mod_gm_opt;
+
+/* main tests */
+int main(void) {
+    int tests = 4;
+    plan(tests);
+
+    mod_gm_opt = malloc(sizeof(mod_gm_opt_t));
+    set_default_options(mod_gm_opt);
+
+    mod_gm_opt->logmode = GM_LOG_MODE_AUTO;
+    lives_ok({gm_log(GM_LOG_INFO, "info message auto\n");}, "info message in auto mode");
+
+    mod_gm_opt->logmode = GM_LOG_MODE_STDOUT;
+    lives_ok({gm_log(GM_LOG_INFO, "info message stdout\n");}, "info message in stdout mode");
+
+    mod_gm_opt->logmode = GM_LOG_MODE_SYSLOG;
+    lives_ok({gm_log(GM_LOG_INFO, "info message syslog\n");}, "info message in syslog mode");
+
+    mod_gm_opt->logmode = GM_LOG_MODE_CORE;
+    lives_ok({gm_log(GM_LOG_INFO, "info message core\n");}, "info message in core mode");
+
+    return exit_status();
+}
+
+/* core log wrapper */
+void write_core_log(char *data) {
+    printf("core logger is not available for tests: %s", data);
+    return;
+}
diff --git a/tools/check_gearman.c b/tools/check_gearman.c
index 48a4c67..6b79f22 100644
--- a/tools/check_gearman.c
+++ b/tools/check_gearman.c
@@ -88,6 +88,7 @@ int main (int argc, char **argv) {
         }
     }
     mod_gm_opt->debug_level = opt_verbose;
+    mod_gm_opt->logmode     = GM_LOG_MODE_TOOLS;
     server_list[server_list_num] = NULL;
 
     if(opt_server == NULL) {
@@ -355,3 +356,10 @@ int check_worker(char * queue, char * to_send, char * expect) {
     printf("%s OK - %s\n", PLUGIN_NAME, result );
     return( STATE_OK );
 }
+
+
+/* core log wrapper */
+void write_core_log(char *data) {
+    printf("core logger is not available for tools: %s", data);
+    return;
+}
diff --git a/tools/gearman_top.c b/tools/gearman_top.c
index f0b1dc1..7e3420f 100644
--- a/tools/gearman_top.c
+++ b/tools/gearman_top.c
@@ -42,6 +42,7 @@ int main (int argc, char **argv) {
     mod_gm_opt = malloc(sizeof(mod_gm_opt_t));
     set_default_options(mod_gm_opt);
 
+
     /*
      * and parse command line
      */
@@ -66,6 +67,7 @@ int main (int argc, char **argv) {
         }
     }
     mod_gm_opt->debug_level = opt_verbose;
+    mod_gm_opt->logmode     = GM_LOG_MODE_TOOLS;
     if(server_list_num == 0)
         server_list[server_list_num++] = "localhost";
     server_list[server_list_num] = NULL;
@@ -211,3 +213,10 @@ void print_stats(char * hostname) {
     free_mod_gm_status_server(stats);
     return;
 }
+
+
+/* core log wrapper */
+void write_core_log(char *data) {
+    printf("core logger is not available for tools: %s", data);
+    return;
+}
diff --git a/tools/send_gearman.c b/tools/send_gearman.c
index d05d168..45a3b83 100644
--- a/tools/send_gearman.c
+++ b/tools/send_gearman.c
@@ -24,7 +24,6 @@
 /* include header */
 #include "send_gearman.h"
 #include "utils.h"
-#include "worker_logger.h"
 #include "gearman.h"
 
 gearman_client_st client;
@@ -42,6 +41,10 @@ int main (int argc, char **argv) {
         exit( EXIT_FAILURE );
     }
 
+    /* set logging */
+    mod_gm_opt->debug_level = GM_LOG_INFO;
+    mod_gm_opt->logmode     = GM_LOG_MODE_TOOLS;
+
     /* init crypto functions */
     if(mod_gm_opt->encryption == GM_ENABLED) {
         mod_gm_crypt_init(mod_gm_opt->crypt_key);
@@ -125,6 +128,8 @@ int verify_options(mod_gm_opt_t *opt) {
     if ( mod_gm_opt->result_queue == NULL )
         mod_gm_opt->result_queue = GM_DEFAULT_RESULT_QUEUE;
 
+    mod_gm_opt->logmode = GM_LOG_MODE_STDOUT;
+
     return(GM_OK);
 }
 
@@ -280,3 +285,10 @@ void print_version() {
     printf("\n");
     exit( STATE_UNKNOWN );
 }
+
+
+/* core log wrapper */
+void write_core_log(char *data) {
+    printf("core logger is not available for tools: %s", data);
+    return;
+}
diff --git a/tools/send_multi.c b/tools/send_multi.c
index 13d905f..09b1df4 100644
--- a/tools/send_multi.c
+++ b/tools/send_multi.c
@@ -25,7 +25,6 @@
 /* include header */
 #include "send_multi.h"
 #include "utils.h"
-#include "worker_logger.h"
 #include "gearman.h"
 
 gearman_client_st client;
@@ -43,6 +42,10 @@ int main (int argc, char **argv) {
         exit( 3 );
     }
 
+    /* set logging */
+    mod_gm_opt->debug_level = GM_LOG_INFO;
+    mod_gm_opt->logmode     = GM_LOG_MODE_TOOLS;
+
     /* init crypto functions */
     if(mod_gm_opt->encryption == GM_ENABLED) {
         mod_gm_crypt_init(mod_gm_opt->crypt_key);
@@ -463,3 +466,10 @@ char *decode_xml(char *string) {
     }
     return string;
 }
+
+
+/* core log wrapper */
+void write_core_log(char *data) {
+    printf("core logger is not available for tools: %s", data);
+    return;
+}
diff --git a/tools/tools_logger.c b/tools/tools_logger.c
deleted file mode 100644
index 958f549..0000000
--- a/tools/tools_logger.c
+++ /dev/null
@@ -1,79 +0,0 @@
-/******************************************************************************
- *
- * mod_gearman - distribute checks with gearman
- *
- * Copyright (c) 2010 Sven Nierlein - sven.nierlein at consol.de
- *
- * This file is part of mod_gearman.
- *
- *  mod_gearman is free software: you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation, either version 3 of the License, or
- *  (at your option) any later version.
- *
- *  mod_gearman is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with mod_gearman.  If not, see <http://www.gnu.org/licenses/>.
- *
- *****************************************************************************/
-
-#include "common.h"
-#include "worker.h"
-#include "tools_logger.h"
-
-struct tm now;
-
-void gm_log( int lvl, const char *text, ... ) {
-    FILE * fp       = NULL;
-    int debug_level = GM_LOG_ERROR;
-    char buffer[GM_BUFFERSIZE];
-    time_t t;
-    char * level;
-    va_list ap;
-    char buffer2[GM_BUFFERSIZE];
-
-    if(mod_gm_opt != NULL) {
-        debug_level = mod_gm_opt->debug_level;
-        fp          = mod_gm_opt->logfile_fp;
-    }
-
-    /* check log level */
-    if ( lvl != GM_LOG_ERROR && lvl > debug_level ) {
-        return;
-    }
-
-    if ( lvl == GM_LOG_ERROR )
-        level = "ERROR";
-    else if ( lvl == GM_LOG_INFO )
-        level = "INFO ";
-    else if ( lvl == GM_LOG_DEBUG )
-        level = "DEBUG";
-    else if ( lvl == GM_LOG_TRACE )
-        level = "TRACE";
-    else
-        level = "UNKNO";
-
-    t   = time(NULL);
-    now = *(localtime(&t));
-
-    strftime(buffer, sizeof(buffer), "[%Y-%m-%d %H:%M:%S]", &now );
-
-    snprintf(buffer2, sizeof(buffer2), "[%i][%s] ", getpid(), level );
-    strncat(buffer, buffer2, (sizeof(buffer)-1));
-
-    va_start( ap, text );
-    vsnprintf( buffer + strlen( buffer ), sizeof( buffer ) - strlen( buffer ), text, ap );
-    va_end( ap );
-
-    if(fp != NULL) {
-        fprintf( fp, "%s", buffer );
-        fflush( fp );
-    } else {
-        printf( "%s", buffer );
-    }
-    return;
-}
diff --git a/worker/worker.c b/worker/worker.c
index b45c23b..242f4f2 100644
--- a/worker/worker.c
+++ b/worker/worker.c
@@ -24,7 +24,6 @@
 /* include header */
 #include "worker.h"
 #include "utils.h"
-#include "worker_logger.h"
 #include "worker_client.h"
 
 int current_number_of_workers                = 0;
@@ -347,7 +346,10 @@ int parse_arguments(int argc, char **argv) {
     }
 
     /* open new logfile */
-    if(mod_gm_opt->logfile && mod_gm_opt->debug_level < GM_LOG_STDOUT) {
+    if ( mod_gm_new_opt->logmode == GM_LOG_MODE_AUTO && mod_gm_new_opt->logfile ) {
+        mod_gm_opt->logmode = GM_LOG_MODE_FILE;
+    }
+    if(mod_gm_new_opt->logmode == GM_LOG_MODE_FILE && mod_gm_opt->logfile && mod_gm_opt->debug_level < GM_LOG_STDOUT) {
         mod_gm_opt->logfile_fp = fopen(mod_gm_opt->logfile, "a+");
         if(mod_gm_opt->logfile_fp == NULL) {
             perror(mod_gm_opt->logfile);
@@ -755,6 +757,12 @@ int get_next_shm_index() {
 }
 
 
+/* core log wrapper */
+void write_core_log(char *data) {
+    printf("core logger is not available for worker: %s", data);
+    return;
+}
+
 /* print version */
 void print_version() {
     printf("mod_gearman_worker: version %s running on libgearman %s\n", GM_VERSION, gearman_version());
diff --git a/worker/worker_client.c b/worker/worker_client.c
index f3a9503..dc9b913 100644
--- a/worker/worker_client.c
+++ b/worker/worker_client.c
@@ -26,7 +26,6 @@
 #include "common.h"
 #include "worker_client.h"
 #include "utils.h"
-#include "worker_logger.h"
 #include "gearman.h"
 
 char temp_buffer1[GM_BUFFERSIZE];
diff --git a/worker/worker_logger.c b/worker/worker_logger.c
deleted file mode 100644
index 84c6906..0000000
--- a/worker/worker_logger.c
+++ /dev/null
@@ -1,79 +0,0 @@
-/******************************************************************************
- *
- * mod_gearman - distribute checks with gearman
- *
- * Copyright (c) 2010 Sven Nierlein - sven.nierlein at consol.de
- *
- * This file is part of mod_gearman.
- *
- *  mod_gearman is free software: you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation, either version 3 of the License, or
- *  (at your option) any later version.
- *
- *  mod_gearman is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with mod_gearman.  If not, see <http://www.gnu.org/licenses/>.
- *
- *****************************************************************************/
-
-#include "common.h"
-#include "worker.h"
-#include "worker_logger.h"
-
-void gm_log( int lvl, const char *text, ... ) {
-    FILE * fp       = NULL;
-    int debug_level = GM_LOG_ERROR;
-    char buffer[GM_BUFFERSIZE];
-    char * level;
-    char buffer2[GM_BUFFERSIZE];
-    time_t t;
-    va_list ap;
-    struct tm now;
-
-    if(mod_gm_opt != NULL) {
-        debug_level = mod_gm_opt->debug_level;
-        fp          = mod_gm_opt->logfile_fp;
-    }
-
-    /* check log level */
-    if ( lvl != GM_LOG_ERROR && lvl > debug_level ) {
-        return;
-    }
-
-    t = time(NULL);
-
-    if ( lvl == GM_LOG_ERROR )
-        level = "ERROR";
-    else if ( lvl == GM_LOG_INFO )
-        level = "INFO ";
-    else if ( lvl == GM_LOG_DEBUG )
-        level = "DEBUG";
-    else if ( lvl == GM_LOG_TRACE )
-        level = "TRACE";
-    else
-        level = "UNKNO";
-
-    localtime_r(&t, &now);
-
-    strftime(buffer, sizeof(buffer), "[%Y-%m-%d %H:%M:%S]", &now );
-
-    snprintf(buffer2, sizeof(buffer2), "[%i][%s] ", getpid(), level );
-    strncat(buffer, buffer2, (sizeof(buffer)-1));
-
-    va_start( ap, text );
-    vsnprintf( buffer + strlen( buffer ), sizeof( buffer ) - strlen( buffer ), text, ap );
-    va_end( ap );
-
-    if(fp != NULL) {
-        fprintf( fp, "%s", buffer );
-        fflush( fp );
-    } else {
-        printf( "%s", buffer );
-    }
-    return;
-}

-- 
Debian packaging for mod gearman.



More information about the Pkg-nagios-changes mailing list