Merge branch 'dm/t_option'
authorFlorian Forster <octo@leeloo.lan.home.verplant.org>
Sat, 17 Jan 2009 10:04:24 +0000 (11:04 +0100)
committerFlorian Forster <octo@leeloo.lan.home.verplant.org>
Sat, 17 Jan 2009 10:04:24 +0000 (11:04 +0100)
src/collectd.c
src/collectd.conf.pod
src/collectd.h
src/collectd.pod
src/csv.c
src/plugin.c
src/plugin.h

index 9526ec9..548a8fd 100644 (file)
@@ -253,6 +253,7 @@ static void exit_usage (int status)
                        "    -C <file>       Configuration file.\n"
                        "                    Default: "CONFIGFILE"\n"
                        "    -t              Test config and exit.\n"
+                       "    -T              Test plugin read and exit.\n"
                        "    -P <file>       PID-file.\n"
                        "                    Default: "PIDFILE"\n"
 #if COLLECT_DAEMON
@@ -398,19 +399,21 @@ int main (int argc, char **argv)
        struct sigaction sig_pipe_action;
        char *configfile = CONFIGFILE;
        int test_config  = 0;
+       int test_readall = 0;
        const char *basedir;
 #if COLLECT_DAEMON
        struct sigaction sig_chld_action;
        pid_t pid;
        int daemonize    = 1;
 #endif
+       int exit_status = 0;
 
        /* read options */
        while (1)
        {
                int c;
 
-               c = getopt (argc, argv, "htC:"
+               c = getopt (argc, argv, "htTC:"
 #if COLLECT_DAEMON
                                "fP:"
 #endif
@@ -427,6 +430,13 @@ int main (int argc, char **argv)
                        case 't':
                                test_config = 1;
                                break;
+                       case 'T':
+                               test_readall = 1;
+                               global_option_set ("ReadThreads", "-1");
+#if COLLECT_DAEMON
+                               daemonize = 0;
+#endif /* COLLECT_DAEMON */
+                               break;
 #if COLLECT_DAEMON
                        case 'P':
                                global_option_set ("PIDFile", optarg);
@@ -580,10 +590,20 @@ int main (int argc, char **argv)
         * run the actual loops
         */
        do_init ();
-       do_loop ();
+
+       if (test_readall)
+       {
+               if (plugin_read_all_once () != 0)
+                       exit_status = 1;
+       }
+       else
+       {
+               INFO ("Initialization complete, entering read-loop.");
+               do_loop ();
+       }
 
        /* close syslog */
-       INFO ("Exiting normally");
+       INFO ("Exiting normally.");
 
        do_shutdown ();
 
@@ -592,5 +612,5 @@ int main (int argc, char **argv)
                pidfile_remove ();
 #endif /* COLLECT_DAEMON */
 
-       return (0);
+       return (exit_status);
 } /* int main */
index 5a25368..1b07fa8 100644 (file)
@@ -273,6 +273,9 @@ installed and an "cpu governor" (that's a kernel module) is loaded.
 
 Set the directory to store CSV-files under. Per default CSV-files are generated
 beneath the daemon's working directory, i.E<nbsp>e. the B<BaseDir>.
+The special strings B<stdout> and B<stderr> can be used to write to the standard
+output and standard error channels, respectively. This, of course, only makes
+much sense when collectd is running in foreground- or non-daemon-mode.
 
 =item B<StoreRates> B<true|false>
 
index 6fed900..277a610 100644 (file)
 #endif
 
 #if defined(COLLECT_DEBUG) && COLLECT_DEBUG && defined(__GNUC__) && __GNUC__
+# undef strcpy
+# undef strcat
+# undef strtok
 # pragma GCC poison strcpy strcat strtok
 #endif
 
  */
 #ifndef DONT_POISON_SPRINTF_YET
 # if defined(COLLECT_DEBUG) && COLLECT_DEBUG && defined(__GNUC__) && __GNUC__
+#  undef sprintf
 #  pragma GCC poison sprintf
 # endif
 #endif
index c10ae78..e36dcdf 100644 (file)
@@ -31,6 +31,11 @@ directory.
 Test the configuration only. The program immediately exits after parsing the
 config file. A return code not equal to zero indicates an error.
 
+=item B<-T>
+
+Test the plugin read callbacks only. The program immediately exits after invoking
+the read callbacks once. A return code not equal to zero indicates an error.
+
 =item B<-P> I<E<lt>pid-fileE<gt>>
 
 Specify an alternative pid file. This overwrites any settings in the config 
index a94b700..b5333b4 100644 (file)
--- a/src/csv.c
+++ b/src/csv.c
@@ -36,6 +36,7 @@ static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
 
 static char *datadir   = NULL;
 static int store_rates = 0;
+static int use_stdio   = 0;
 
 static int value_list_to_string (char *buffer, int buffer_len,
                const data_set_t *ds, const value_list_t *vl)
@@ -146,6 +147,7 @@ static int value_list_to_filename (char *buffer, int buffer_len,
                return (-1);
        offset += status;
 
+       if (!use_stdio)
        {
                time_t now;
                struct tm stm;
@@ -200,6 +202,16 @@ static int csv_config (const char *key, const char *value)
        {
                if (datadir != NULL)
                        free (datadir);
+               if (strcasecmp ("stdout", value) == 0)
+               {
+                       use_stdio = 1;
+                       return (0);
+               }
+               else if (strcasecmp ("stderr", value) == 0)
+               {
+                       use_stdio = 2;
+                       return (0);
+               }
                datadir = strdup (value);
                if (datadir != NULL)
                {
@@ -259,6 +271,13 @@ static int csv_write (const data_set_t *ds, const value_list_t *vl)
        if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
                return (-1);
 
+       if (use_stdio)
+       {
+               fprintf (use_stdio == 1 ? stdout : stderr,
+                        "%s=%s\n", filename, values);
+               return (0);
+       }
+
        if (stat (filename, &statbuf) == -1)
        {
                if (errno == ENOENT)
index 4ad7366..bf707ec 100644 (file)
@@ -276,6 +276,9 @@ static void stop_threads (void)
 {
        int i;
 
+       if (read_threads == NULL)
+               return;
+
        pthread_mutex_lock (&read_lock);
        read_loop = 0;
        DEBUG ("plugin: stop_threads: Signalling `read_cond'");
@@ -635,7 +638,8 @@ void plugin_init_all (void)
                int num;
                rt = global_option_get ("ReadThreads");
                num = atoi (rt);
-               start_threads ((num > 0) ? num : 5);
+               if (num != -1)
+                       start_threads ((num > 0) ? num : 5);
        }
 } /* void plugin_init_all */
 
@@ -678,6 +682,37 @@ void plugin_read_all (void)
        pthread_mutex_unlock (&read_lock);
 } /* void plugin_read_all */
 
+/* Read function called when the `-T' command line argument is given. */
+int plugin_read_all_once (void)
+{
+       llentry_t   *le;
+       read_func_t *rf;
+       int status;
+       int return_status = 0;
+
+       if (list_read == NULL)
+       {
+               NOTICE ("No read-functions are registered.");
+               return (0);
+       }
+
+       for (le = llist_head (list_read);
+            le != NULL;
+            le = le->next)
+       {
+               rf = (read_func_t *) le->value;
+               status = rf->callback ();
+               if (status != 0)
+               {
+                       NOTICE ("read-function of plugin `%s' failed.",
+                               le->key);
+                       return_status = -1;
+               }
+       }
+
+       return (return_status);
+} /* int plugin_read_all_once */
+
 int plugin_write (const char *plugin, /* {{{ */
                const data_set_t *ds, const value_list_t *vl)
 {
index a6f89a0..4f4a360 100644 (file)
@@ -181,6 +181,7 @@ int plugin_load (const char *name);
 
 void plugin_init_all (void);
 void plugin_read_all (void);
+int plugin_read_all_once (void);
 void plugin_shutdown_all (void);
 
 /*