2 * collectd - src/collectd.c
3 * Copyright (C) 2005-2007 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
20 * Alvaro Barcellos <alvaro.barcellos at gmail.com>
26 #include <sys/types.h>
27 #include <sys/socket.h>
33 #include "configfile.h"
36 # include <statgrab.h>
42 char hostname_g[DATA_MAX_NAME_LEN];
46 #endif /* HAVE_LIBKSTAT */
50 static void *do_flush (void *arg)
52 INFO ("Flushing all data.");
53 plugin_flush (NULL, -1, NULL);
54 INFO ("Finished flushing all data.");
59 static void sig_int_handler (int signal)
64 static void sig_term_handler (int signal)
69 static void sig_usr1_handler (int signal)
74 /* flushing the data might take a while,
75 * so it should be done asynchronously */
76 pthread_attr_init (&attr);
77 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
78 pthread_create (&thread, &attr, do_flush, NULL);
81 static int init_hostname (void)
85 struct addrinfo ai_hints;
86 struct addrinfo *ai_list;
87 struct addrinfo *ai_ptr;
90 str = global_option_get ("Hostname");
93 sstrncpy (hostname_g, str, sizeof (hostname_g));
97 if (gethostname (hostname_g, sizeof (hostname_g)) != 0)
99 fprintf (stderr, "`gethostname' failed and no "
100 "hostname was configured.\n");
104 str = global_option_get ("FQDNLookup");
105 if ((strcasecmp ("false", str) == 0)
106 || (strcasecmp ("no", str) == 0)
107 || (strcasecmp ("off", str) == 0))
110 memset (&ai_hints, '\0', sizeof (ai_hints));
111 ai_hints.ai_flags = AI_CANONNAME;
113 status = getaddrinfo (hostname_g, NULL, &ai_hints, &ai_list);
116 ERROR ("Looking up \"%s\" failed. You have set the "
117 "\"FQDNLookup\" option, but I cannot resolve "
118 "my hostname to a fully qualified domain "
119 "name. Please fix you network "
120 "configuration.", hostname_g);
124 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
126 if (ai_ptr->ai_canonname == NULL)
129 sstrncpy (hostname_g, ai_ptr->ai_canonname, sizeof (hostname_g));
133 freeaddrinfo (ai_list);
135 } /* int init_hostname */
137 static int init_global_variables (void)
141 str = global_option_get ("Interval");
144 interval_g = atoi (str);
147 fprintf (stderr, "Cannot set the interval to a correct value.\n"
148 "Please check your settings.\n");
151 DEBUG ("interval_g = %i;", interval_g);
153 if (init_hostname () != 0)
155 DEBUG ("hostname_g = %s;", hostname_g);
158 } /* int init_global_variables */
160 static int change_basedir (const char *orig_dir)
162 char *dir = strdup (orig_dir);
169 ERROR ("strdup failed: %s",
170 sstrerror (errno, errbuf, sizeof (errbuf)));
174 dirlen = strlen (dir);
175 while ((dirlen > 0) && (dir[dirlen - 1] == '/'))
176 dir[--dirlen] = '\0';
181 status = chdir (dir);
188 if (mkdir (orig_dir, 0755) == -1)
191 ERROR ("change_basedir: mkdir (%s): %s", orig_dir,
192 sstrerror (errno, errbuf,
196 else if (chdir (orig_dir) == -1)
199 ERROR ("chdir (%s): %s", orig_dir,
200 sstrerror (errno, errbuf,
208 ERROR ("chdir (%s): %s", orig_dir,
209 sstrerror (errno, errbuf,
216 } /* static int change_basedir (char *dir) */
219 static void update_kstat (void)
223 if ((kc = kstat_open ()) == NULL)
224 ERROR ("Unable to open kstat control structure");
229 kid = kstat_chain_update (kc);
232 INFO ("kstat chain has been updated");
236 ERROR ("kstat chain update failed");
237 /* else: everything works as expected */
241 } /* static void update_kstat (void) */
242 #endif /* HAVE_LIBKSTAT */
245 * Remove all settings but `-f' and `-C'
247 static void exit_usage (int status)
249 printf ("Usage: "PACKAGE" [OPTIONS]\n\n"
251 "Available options:\n"
253 " -C <file> Configuration file.\n"
254 " Default: "CONFIGFILE"\n"
255 " -t Test config and exit.\n"
256 " -T Test plugin read and exit.\n"
257 " -P <file> PID-file.\n"
258 " Default: "PIDFILE"\n"
260 " -f Don't fork to the background.\n"
262 " -h Display help (this message)\n"
263 "\nBuiltin defaults:\n"
264 " Config-File "CONFIGFILE"\n"
265 " PID-File "PIDFILE"\n"
266 " Data-Directory "PKGLOCALSTATEDIR"\n"
267 "\n"PACKAGE" "VERSION", http://collectd.org/\n"
268 "by Florian octo Forster <octo@verplant.org>\n"
269 "for contributions see `AUTHORS'\n");
271 } /* static void exit_usage (int status) */
273 static int do_init (void)
283 ERROR ("sg_init: %s", sg_str_error (sg_get_error ()));
287 if (sg_drop_privileges ())
289 ERROR ("sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
297 } /* int do_init () */
300 static int do_loop (void)
302 struct timeval tv_now;
303 struct timeval tv_next;
304 struct timespec ts_wait;
308 if (gettimeofday (&tv_next, NULL) < 0)
311 ERROR ("gettimeofday failed: %s",
312 sstrerror (errno, errbuf,
316 tv_next.tv_sec += interval_g;
322 /* Issue all plugins */
325 if (gettimeofday (&tv_now, NULL) < 0)
328 ERROR ("gettimeofday failed: %s",
329 sstrerror (errno, errbuf,
334 if (timeval_sub_timespec (&tv_next, &tv_now, &ts_wait) != 0)
336 WARNING ("Not sleeping because "
337 "`timeval_sub_timespec' returned "
342 while ((loop == 0) && (nanosleep (&ts_wait, &ts_wait) == -1))
347 ERROR ("nanosleep failed: %s",
348 sstrerror (errno, errbuf,
353 } /* while (loop == 0) */
355 DEBUG ("return (0);");
359 static int do_shutdown (void)
361 plugin_shutdown_all ();
363 } /* int do_shutdown */
366 static int pidfile_create (void)
369 const char *file = global_option_get ("PIDFile");
371 if ((fh = fopen (file, "w")) == NULL)
374 ERROR ("fopen (%s): %s", file,
375 sstrerror (errno, errbuf, sizeof (errbuf)));
379 fprintf (fh, "%i\n", (int) getpid ());
383 } /* static int pidfile_create (const char *file) */
385 static int pidfile_remove (void)
387 const char *file = global_option_get ("PIDFile");
389 DEBUG ("unlink (%s)", (file != NULL) ? file : "<null>");
390 return (unlink (file));
391 } /* static int pidfile_remove (const char *file) */
392 #endif /* COLLECT_DAEMON */
394 int main (int argc, char **argv)
396 struct sigaction sig_int_action;
397 struct sigaction sig_term_action;
398 struct sigaction sig_usr1_action;
399 struct sigaction sig_pipe_action;
400 char *configfile = CONFIGFILE;
402 int test_readall = 0;
405 struct sigaction sig_chld_action;
416 c = getopt (argc, argv, "htTC:"
435 global_option_set ("ReadThreads", "-1");
438 #endif /* COLLECT_DAEMON */
442 global_option_set ("PIDFile", optarg);
447 #endif /* COLLECT_DAEMON */
460 * Read options from the config file, the environment and the command
461 * line (in that order, with later options overwriting previous ones in
463 * Also, this will automatically load modules.
465 if (cf_read (configfile))
467 fprintf (stderr, "Error: Reading the config file failed!\n"
468 "Read the syslog for details.\n");
473 * Change directory. We do this _after_ reading the config and loading
474 * modules to relative paths work as expected.
476 if ((basedir = global_option_get ("BaseDir")) == NULL)
478 fprintf (stderr, "Don't have a basedir to use. This should not happen. Ever.");
481 else if (change_basedir (basedir))
483 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", basedir);
488 * Set global variables or, if that failes, exit. We cannot run with
489 * them being uninitialized. If nothing is configured, then defaults
490 * are being used. So this means that the user has actually done
493 if (init_global_variables () != 0)
503 memset (&sig_chld_action, '\0', sizeof (sig_chld_action));
504 sig_chld_action.sa_handler = SIG_IGN;
505 sigaction (SIGCHLD, &sig_chld_action, NULL);
509 if ((pid = fork ()) == -1)
513 fprintf (stderr, "fork: %s",
514 sstrerror (errno, errbuf,
521 /* printf ("Running (PID %i)\n", pid); */
525 /* Detach from session */
529 if (pidfile_create ())
532 /* close standard descriptors */
537 if (open ("/dev/null", O_RDWR) != 0)
539 ERROR ("Error: Could not connect `STDIN' to `/dev/null'");
544 ERROR ("Error: Could not connect `STDOUT' to `/dev/null'");
549 ERROR ("Error: Could not connect `STDERR' to `/dev/null'");
552 } /* if (daemonize) */
553 #endif /* COLLECT_DAEMON */
555 memset (&sig_pipe_action, '\0', sizeof (sig_pipe_action));
556 sig_pipe_action.sa_handler = SIG_IGN;
557 sigaction (SIGPIPE, &sig_pipe_action, NULL);
560 * install signal handlers
562 memset (&sig_int_action, '\0', sizeof (sig_int_action));
563 sig_int_action.sa_handler = sig_int_handler;
564 if (0 != sigaction (SIGINT, &sig_int_action, NULL)) {
566 ERROR ("Error: Failed to install a signal handler for signal INT: %s",
567 sstrerror (errno, errbuf, sizeof (errbuf)));
571 memset (&sig_term_action, '\0', sizeof (sig_term_action));
572 sig_term_action.sa_handler = sig_term_handler;
573 if (0 != sigaction (SIGTERM, &sig_term_action, NULL)) {
575 ERROR ("Error: Failed to install a signal handler for signal TERM: %s",
576 sstrerror (errno, errbuf, sizeof (errbuf)));
580 memset (&sig_usr1_action, '\0', sizeof (sig_usr1_action));
581 sig_usr1_action.sa_handler = sig_usr1_handler;
582 if (0 != sigaction (SIGUSR1, &sig_usr1_action, NULL)) {
584 ERROR ("Error: Failed to install a signal handler for signal USR1: %s",
585 sstrerror (errno, errbuf, sizeof (errbuf)));
590 * run the actual loops
596 if (plugin_read_all_once () != 0)
601 INFO ("Initialization complete, entering read-loop.");
606 INFO ("Exiting normally.");
613 #endif /* COLLECT_DAEMON */
615 return (exit_status);