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];
44 int pidfile_from_cli = 0;
48 #endif /* HAVE_LIBKSTAT */
52 static void *do_flush (void __attribute__((unused)) *arg)
54 INFO ("Flushing all data.");
55 plugin_flush (/* plugin = */ NULL,
58 INFO ("Finished flushing all data.");
63 static void sig_int_handler (int __attribute__((unused)) signal)
68 static void sig_term_handler (int __attribute__((unused)) signal)
73 static void sig_usr1_handler (int __attribute__((unused)) signal)
78 /* flushing the data might take a while,
79 * so it should be done asynchronously */
80 pthread_attr_init (&attr);
81 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
82 pthread_create (&thread, &attr, do_flush, NULL);
83 pthread_attr_destroy (&attr);
86 static int init_hostname (void)
90 struct addrinfo ai_hints;
91 struct addrinfo *ai_list;
92 struct addrinfo *ai_ptr;
95 str = global_option_get ("Hostname");
98 sstrncpy (hostname_g, str, sizeof (hostname_g));
102 if (gethostname (hostname_g, sizeof (hostname_g)) != 0)
104 fprintf (stderr, "`gethostname' failed and no "
105 "hostname was configured.\n");
109 str = global_option_get ("FQDNLookup");
113 memset (&ai_hints, '\0', sizeof (ai_hints));
114 ai_hints.ai_flags = AI_CANONNAME;
116 status = getaddrinfo (hostname_g, NULL, &ai_hints, &ai_list);
119 ERROR ("Looking up \"%s\" failed. You have set the "
120 "\"FQDNLookup\" option, but I cannot resolve "
121 "my hostname to a fully qualified domain "
122 "name. Please fix the network "
123 "configuration.", hostname_g);
127 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
129 if (ai_ptr->ai_canonname == NULL)
132 sstrncpy (hostname_g, ai_ptr->ai_canonname, sizeof (hostname_g));
136 freeaddrinfo (ai_list);
138 } /* int init_hostname */
140 static int init_global_variables (void)
144 interval_g = cf_get_default_interval ();
145 assert (interval_g > 0);
146 DEBUG ("interval_g = %.3f;", CDTIME_T_TO_DOUBLE (interval_g));
148 str = global_option_get ("Timeout");
151 timeout_g = atoi (str);
154 fprintf (stderr, "Cannot set the timeout to a correct value.\n"
155 "Please check your settings.\n");
158 DEBUG ("timeout_g = %i;", timeout_g);
160 if (init_hostname () != 0)
162 DEBUG ("hostname_g = %s;", hostname_g);
165 } /* int init_global_variables */
167 static int change_basedir (const char *orig_dir)
173 dir = strdup (orig_dir);
177 ERROR ("strdup failed: %s",
178 sstrerror (errno, errbuf, sizeof (errbuf)));
182 dirlen = strlen (dir);
183 while ((dirlen > 0) && (dir[dirlen - 1] == '/'))
184 dir[--dirlen] = '\0';
189 status = chdir (dir);
195 else if (errno != ENOENT)
198 ERROR ("change_basedir: chdir (%s): %s", dir,
199 sstrerror (errno, errbuf, sizeof (errbuf)));
204 status = mkdir (dir, S_IRWXU | S_IRWXG | S_IRWXO);
208 ERROR ("change_basedir: mkdir (%s): %s", dir,
209 sstrerror (errno, errbuf, sizeof (errbuf)));
214 status = chdir (dir);
218 ERROR ("change_basedir: chdir (%s): %s", dir,
219 sstrerror (errno, errbuf, sizeof (errbuf)));
226 } /* static int change_basedir (char *dir) */
229 static void update_kstat (void)
233 if ((kc = kstat_open ()) == NULL)
234 ERROR ("Unable to open kstat control structure");
239 kid = kstat_chain_update (kc);
242 INFO ("kstat chain has been updated");
246 ERROR ("kstat chain update failed");
247 /* else: everything works as expected */
251 } /* static void update_kstat (void) */
252 #endif /* HAVE_LIBKSTAT */
255 * Remove all settings but `-f' and `-C'
257 static void exit_usage (int status)
259 printf ("Usage: "PACKAGE" [OPTIONS]\n\n"
261 "Available options:\n"
263 " -C <file> Configuration file.\n"
264 " Default: "CONFIGFILE"\n"
265 " -t Test config and exit.\n"
266 " -T Test plugin read and exit.\n"
267 " -P <file> PID-file.\n"
268 " Default: "PIDFILE"\n"
270 " -f Don't fork to the background.\n"
272 " -h Display help (this message)\n"
273 "\nBuiltin defaults:\n"
274 " Config file "CONFIGFILE"\n"
275 " PID file "PIDFILE"\n"
276 " Plugin directory "PLUGINDIR"\n"
277 " Data directory "PKGLOCALSTATEDIR"\n"
278 "\n"PACKAGE" "VERSION", http://collectd.org/\n"
279 "by Florian octo Forster <octo@verplant.org>\n"
280 "for contributions see `AUTHORS'\n");
282 } /* static void exit_usage (int status) */
284 static int do_init (void)
294 ERROR ("sg_init: %s", sg_str_error (sg_get_error ()));
298 if (sg_drop_privileges ())
300 ERROR ("sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
308 } /* int do_init () */
311 static int do_loop (void)
313 cdtime_t interval = cf_get_default_interval ();
316 wait_until = cdtime () + interval;
320 struct timespec ts_wait = { 0, 0 };
327 /* Issue all plugins */
331 if (now >= wait_until)
333 WARNING ("Not sleeping because the next interval is "
334 "%.3f seconds in the past!",
335 CDTIME_T_TO_DOUBLE (now - wait_until));
336 wait_until = now + interval;
340 CDTIME_T_TO_TIMESPEC (wait_until - now, &ts_wait);
341 wait_until = wait_until + interval;
343 while ((loop == 0) && (nanosleep (&ts_wait, &ts_wait) != 0))
348 ERROR ("nanosleep failed: %s",
349 sstrerror (errno, errbuf,
354 } /* while (loop == 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);
443 pidfile_from_cli = 1;
448 #endif /* COLLECT_DAEMON */
463 * Read options from the config file, the environment and the command
464 * line (in that order, with later options overwriting previous ones in
466 * Also, this will automatically load modules.
468 if (cf_read (configfile))
470 fprintf (stderr, "Error: Reading the config file failed!\n"
471 "Read the syslog for details.\n");
476 * Change directory. We do this _after_ reading the config and loading
477 * modules to relative paths work as expected.
479 if ((basedir = global_option_get ("BaseDir")) == NULL)
481 fprintf (stderr, "Don't have a basedir to use. This should not happen. Ever.");
484 else if (change_basedir (basedir))
486 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", basedir);
491 * Set global variables or, if that failes, exit. We cannot run with
492 * them being uninitialized. If nothing is configured, then defaults
493 * are being used. So this means that the user has actually done
496 if (init_global_variables () != 0)
506 memset (&sig_chld_action, '\0', sizeof (sig_chld_action));
507 sig_chld_action.sa_handler = SIG_IGN;
508 sigaction (SIGCHLD, &sig_chld_action, NULL);
512 if ((pid = fork ()) == -1)
516 fprintf (stderr, "fork: %s",
517 sstrerror (errno, errbuf,
524 /* printf ("Running (PID %i)\n", pid); */
528 /* Detach from session */
532 if (pidfile_create ())
535 /* close standard descriptors */
540 if (open ("/dev/null", O_RDWR) != 0)
542 ERROR ("Error: Could not connect `STDIN' to `/dev/null'");
547 ERROR ("Error: Could not connect `STDOUT' to `/dev/null'");
552 ERROR ("Error: Could not connect `STDERR' to `/dev/null'");
555 } /* if (daemonize) */
556 #endif /* COLLECT_DAEMON */
558 memset (&sig_pipe_action, '\0', sizeof (sig_pipe_action));
559 sig_pipe_action.sa_handler = SIG_IGN;
560 sigaction (SIGPIPE, &sig_pipe_action, NULL);
563 * install signal handlers
565 memset (&sig_int_action, '\0', sizeof (sig_int_action));
566 sig_int_action.sa_handler = sig_int_handler;
567 if (0 != sigaction (SIGINT, &sig_int_action, NULL)) {
569 ERROR ("Error: Failed to install a signal handler for signal INT: %s",
570 sstrerror (errno, errbuf, sizeof (errbuf)));
574 memset (&sig_term_action, '\0', sizeof (sig_term_action));
575 sig_term_action.sa_handler = sig_term_handler;
576 if (0 != sigaction (SIGTERM, &sig_term_action, NULL)) {
578 ERROR ("Error: Failed to install a signal handler for signal TERM: %s",
579 sstrerror (errno, errbuf, sizeof (errbuf)));
583 memset (&sig_usr1_action, '\0', sizeof (sig_usr1_action));
584 sig_usr1_action.sa_handler = sig_usr1_handler;
585 if (0 != sigaction (SIGUSR1, &sig_usr1_action, NULL)) {
587 ERROR ("Error: Failed to install a signal handler for signal USR1: %s",
588 sstrerror (errno, errbuf, sizeof (errbuf)));
593 * run the actual loops
599 if (plugin_read_all_once () != 0)
604 INFO ("Initialization complete, entering read-loop.");
609 INFO ("Exiting normally.");
616 #endif /* COLLECT_DAEMON */
618 return (exit_status);