2 * collectd - src/collectd.c
3 * Copyright (C) 2005-2007 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
25 * Alvaro Barcellos <alvaro.barcellos at gmail.com>
31 #include <sys/types.h>
32 #include <sys/socket.h>
38 #include "configfile.h"
41 # include <statgrab.h>
47 char hostname_g[DATA_MAX_NAME_LEN];
52 #endif /* HAVE_LIBKSTAT */
56 static void *do_flush (void __attribute__((unused)) *arg)
58 INFO ("Flushing all data.");
59 plugin_flush (/* plugin = */ NULL,
62 INFO ("Finished flushing all data.");
67 static void sig_int_handler (int __attribute__((unused)) signal)
72 static void sig_term_handler (int __attribute__((unused)) signal)
77 static void sig_usr1_handler (int __attribute__((unused)) signal)
82 /* flushing the data might take a while,
83 * so it should be done asynchronously */
84 pthread_attr_init (&attr);
85 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
86 pthread_create (&thread, &attr, do_flush, NULL);
87 pthread_attr_destroy (&attr);
90 static int init_hostname (void)
94 struct addrinfo ai_hints;
95 struct addrinfo *ai_list;
96 struct addrinfo *ai_ptr;
99 str = global_option_get ("Hostname");
102 sstrncpy (hostname_g, str, sizeof (hostname_g));
106 if (gethostname (hostname_g, sizeof (hostname_g)) != 0)
108 fprintf (stderr, "`gethostname' failed and no "
109 "hostname was configured.\n");
113 str = global_option_get ("FQDNLookup");
117 memset (&ai_hints, '\0', sizeof (ai_hints));
118 ai_hints.ai_flags = AI_CANONNAME;
120 status = getaddrinfo (hostname_g, NULL, &ai_hints, &ai_list);
123 ERROR ("Looking up \"%s\" failed. You have set the "
124 "\"FQDNLookup\" option, but I cannot resolve "
125 "my hostname to a fully qualified domain "
126 "name. Please fix the network "
127 "configuration.", hostname_g);
131 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
133 if (ai_ptr->ai_canonname == NULL)
136 sstrncpy (hostname_g, ai_ptr->ai_canonname, sizeof (hostname_g));
140 freeaddrinfo (ai_list);
142 } /* int init_hostname */
144 static int init_global_variables (void)
148 interval_g = cf_get_default_interval ();
149 assert (interval_g > 0);
150 DEBUG ("interval_g = %.3f;", CDTIME_T_TO_DOUBLE (interval_g));
152 str = global_option_get ("Timeout");
155 timeout_g = atoi (str);
158 fprintf (stderr, "Cannot set the timeout to a correct value.\n"
159 "Please check your settings.\n");
162 DEBUG ("timeout_g = %i;", timeout_g);
164 if (init_hostname () != 0)
166 DEBUG ("hostname_g = %s;", hostname_g);
169 } /* int init_global_variables */
171 static int change_basedir (const char *orig_dir)
177 dir = strdup (orig_dir);
181 ERROR ("strdup failed: %s",
182 sstrerror (errno, errbuf, sizeof (errbuf)));
186 dirlen = strlen (dir);
187 while ((dirlen > 0) && (dir[dirlen - 1] == '/'))
188 dir[--dirlen] = '\0';
193 status = chdir (dir);
199 else if (errno != ENOENT)
202 ERROR ("change_basedir: chdir (%s): %s", dir,
203 sstrerror (errno, errbuf, sizeof (errbuf)));
208 status = mkdir (dir, S_IRWXU | S_IRWXG | S_IRWXO);
212 ERROR ("change_basedir: mkdir (%s): %s", dir,
213 sstrerror (errno, errbuf, sizeof (errbuf)));
218 status = chdir (dir);
222 ERROR ("change_basedir: chdir (%s): %s", dir,
223 sstrerror (errno, errbuf, sizeof (errbuf)));
230 } /* static int change_basedir (char *dir) */
233 static void update_kstat (void)
237 if ((kc = kstat_open ()) == NULL)
238 ERROR ("Unable to open kstat control structure");
243 kid = kstat_chain_update (kc);
246 INFO ("kstat chain has been updated");
250 ERROR ("kstat chain update failed");
251 /* else: everything works as expected */
255 } /* static void update_kstat (void) */
256 #endif /* HAVE_LIBKSTAT */
259 * Remove all settings but `-f' and `-C'
261 static void exit_usage (int status)
263 printf ("Usage: "PACKAGE" [OPTIONS]\n\n"
265 "Available options:\n"
267 " -C <file> Configuration file.\n"
268 " Default: "CONFIGFILE"\n"
269 " -t Test config and exit.\n"
270 " -T Test plugin read and exit.\n"
271 " -P <file> PID-file.\n"
272 " Default: "PIDFILE"\n"
274 " -f Don't fork to the background.\n"
276 " -h Display help (this message)\n"
277 "\nBuiltin defaults:\n"
278 " Config file "CONFIGFILE"\n"
279 " PID file "PIDFILE"\n"
280 " Plugin directory "PLUGINDIR"\n"
281 " Data directory "PKGLOCALSTATEDIR"\n"
282 "\n"PACKAGE" "VERSION", http://collectd.org/\n"
283 "by Florian octo Forster <octo@collectd.org>\n"
284 "for contributions see `AUTHORS'\n");
286 } /* static void exit_usage (int status) */
288 static int do_init (void)
298 ERROR ("sg_init: %s", sg_str_error (sg_get_error ()));
302 if (sg_drop_privileges ())
304 ERROR ("sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
312 } /* int do_init () */
315 static int do_loop (void)
317 cdtime_t interval = cf_get_default_interval ();
320 wait_until = cdtime () + interval;
324 struct timespec ts_wait = { 0, 0 };
331 /* Issue all plugins */
335 if (now >= wait_until)
337 WARNING ("Not sleeping because the next interval is "
338 "%.3f seconds in the past!",
339 CDTIME_T_TO_DOUBLE (now - wait_until));
340 wait_until = now + interval;
344 CDTIME_T_TO_TIMESPEC (wait_until - now, &ts_wait);
345 wait_until = wait_until + interval;
347 while ((loop == 0) && (nanosleep (&ts_wait, &ts_wait) != 0))
352 ERROR ("nanosleep failed: %s",
353 sstrerror (errno, errbuf,
358 } /* while (loop == 0) */
363 static int do_shutdown (void)
365 plugin_shutdown_all ();
367 } /* int do_shutdown */
370 static int pidfile_create (void)
373 const char *file = global_option_get ("PIDFile");
375 if ((fh = fopen (file, "w")) == NULL)
378 ERROR ("fopen (%s): %s", file,
379 sstrerror (errno, errbuf, sizeof (errbuf)));
383 fprintf (fh, "%i\n", (int) getpid ());
387 } /* static int pidfile_create (const char *file) */
389 static int pidfile_remove (void)
391 const char *file = global_option_get ("PIDFile");
393 DEBUG ("unlink (%s)", (file != NULL) ? file : "<null>");
394 return (unlink (file));
395 } /* static int pidfile_remove (const char *file) */
396 #endif /* COLLECT_DAEMON */
398 int main (int argc, char **argv)
400 struct sigaction sig_int_action;
401 struct sigaction sig_term_action;
402 struct sigaction sig_usr1_action;
403 struct sigaction sig_pipe_action;
404 char *configfile = CONFIGFILE;
406 int test_readall = 0;
409 struct sigaction sig_chld_action;
420 c = getopt (argc, argv, "htTC:"
439 global_option_set ("ReadThreads", "-1");
442 #endif /* COLLECT_DAEMON */
446 global_option_set ("PIDFile", optarg);
451 #endif /* COLLECT_DAEMON */
466 * Read options from the config file, the environment and the command
467 * line (in that order, with later options overwriting previous ones in
469 * Also, this will automatically load modules.
471 if (cf_read (configfile))
473 fprintf (stderr, "Error: Reading the config file failed!\n"
474 "Read the syslog for details.\n");
479 * Change directory. We do this _after_ reading the config and loading
480 * modules to relative paths work as expected.
482 if ((basedir = global_option_get ("BaseDir")) == NULL)
484 fprintf (stderr, "Don't have a basedir to use. This should not happen. Ever.");
487 else if (change_basedir (basedir))
489 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", basedir);
494 * Set global variables or, if that failes, exit. We cannot run with
495 * them being uninitialized. If nothing is configured, then defaults
496 * are being used. So this means that the user has actually done
499 if (init_global_variables () != 0)
509 memset (&sig_chld_action, '\0', sizeof (sig_chld_action));
510 sig_chld_action.sa_handler = SIG_IGN;
511 sigaction (SIGCHLD, &sig_chld_action, NULL);
515 if ((pid = fork ()) == -1)
519 fprintf (stderr, "fork: %s",
520 sstrerror (errno, errbuf,
527 /* printf ("Running (PID %i)\n", pid); */
531 /* Detach from session */
535 if (pidfile_create ())
538 /* close standard descriptors */
543 if (open ("/dev/null", O_RDWR) != 0)
545 ERROR ("Error: Could not connect `STDIN' to `/dev/null'");
550 ERROR ("Error: Could not connect `STDOUT' to `/dev/null'");
555 ERROR ("Error: Could not connect `STDERR' to `/dev/null'");
558 } /* if (daemonize) */
559 #endif /* COLLECT_DAEMON */
561 memset (&sig_pipe_action, '\0', sizeof (sig_pipe_action));
562 sig_pipe_action.sa_handler = SIG_IGN;
563 sigaction (SIGPIPE, &sig_pipe_action, NULL);
566 * install signal handlers
568 memset (&sig_int_action, '\0', sizeof (sig_int_action));
569 sig_int_action.sa_handler = sig_int_handler;
570 if (0 != sigaction (SIGINT, &sig_int_action, NULL)) {
572 ERROR ("Error: Failed to install a signal handler for signal INT: %s",
573 sstrerror (errno, errbuf, sizeof (errbuf)));
577 memset (&sig_term_action, '\0', sizeof (sig_term_action));
578 sig_term_action.sa_handler = sig_term_handler;
579 if (0 != sigaction (SIGTERM, &sig_term_action, NULL)) {
581 ERROR ("Error: Failed to install a signal handler for signal TERM: %s",
582 sstrerror (errno, errbuf, sizeof (errbuf)));
586 memset (&sig_usr1_action, '\0', sizeof (sig_usr1_action));
587 sig_usr1_action.sa_handler = sig_usr1_handler;
588 if (0 != sigaction (SIGUSR1, &sig_usr1_action, NULL)) {
590 ERROR ("Error: Failed to install a signal handler for signal USR1: %s",
591 sstrerror (errno, errbuf, sizeof (errbuf)));
596 * run the actual loops
602 if (plugin_read_all_once () != 0)
607 INFO ("Initialization complete, entering read-loop.");
612 INFO ("Exiting normally.");
619 #endif /* COLLECT_DAEMON */
621 return (exit_status);