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];
49 int pidfile_from_cli = 0;
53 #endif /* HAVE_LIBKSTAT */
57 static void *do_flush (void __attribute__((unused)) *arg)
59 INFO ("Flushing all data.");
60 plugin_flush (/* plugin = */ NULL,
63 INFO ("Finished flushing all data.");
68 static void sig_int_handler (int __attribute__((unused)) signal)
73 static void sig_term_handler (int __attribute__((unused)) signal)
78 static void sig_usr1_handler (int __attribute__((unused)) signal)
83 /* flushing the data might take a while,
84 * so it should be done asynchronously */
85 pthread_attr_init (&attr);
86 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
87 pthread_create (&thread, &attr, do_flush, NULL);
88 pthread_attr_destroy (&attr);
91 static int init_hostname (void)
95 struct addrinfo ai_hints;
96 struct addrinfo *ai_list;
97 struct addrinfo *ai_ptr;
100 str = global_option_get ("Hostname");
103 sstrncpy (hostname_g, str, sizeof (hostname_g));
107 if (gethostname (hostname_g, sizeof (hostname_g)) != 0)
109 fprintf (stderr, "`gethostname' failed and no "
110 "hostname was configured.\n");
114 str = global_option_get ("FQDNLookup");
118 memset (&ai_hints, '\0', sizeof (ai_hints));
119 ai_hints.ai_flags = AI_CANONNAME;
121 status = getaddrinfo (hostname_g, NULL, &ai_hints, &ai_list);
124 ERROR ("Looking up \"%s\" failed. You have set the "
125 "\"FQDNLookup\" option, but I cannot resolve "
126 "my hostname to a fully qualified domain "
127 "name. Please fix the network "
128 "configuration.", hostname_g);
132 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
134 if (ai_ptr->ai_canonname == NULL)
137 sstrncpy (hostname_g, ai_ptr->ai_canonname, sizeof (hostname_g));
141 freeaddrinfo (ai_list);
143 } /* int init_hostname */
145 static int init_global_variables (void)
149 interval_g = cf_get_default_interval ();
150 assert (interval_g > 0);
151 DEBUG ("interval_g = %.3f;", CDTIME_T_TO_DOUBLE (interval_g));
153 str = global_option_get ("Timeout");
156 timeout_g = atoi (str);
159 fprintf (stderr, "Cannot set the timeout to a correct value.\n"
160 "Please check your settings.\n");
163 DEBUG ("timeout_g = %i;", timeout_g);
165 if (init_hostname () != 0)
167 DEBUG ("hostname_g = %s;", hostname_g);
170 } /* int init_global_variables */
172 static int change_basedir (const char *orig_dir)
178 dir = strdup (orig_dir);
182 ERROR ("strdup failed: %s",
183 sstrerror (errno, errbuf, sizeof (errbuf)));
187 dirlen = strlen (dir);
188 while ((dirlen > 0) && (dir[dirlen - 1] == '/'))
189 dir[--dirlen] = '\0';
194 status = chdir (dir);
200 else if (errno != ENOENT)
203 ERROR ("change_basedir: chdir (%s): %s", dir,
204 sstrerror (errno, errbuf, sizeof (errbuf)));
209 status = mkdir (dir, S_IRWXU | S_IRWXG | S_IRWXO);
213 ERROR ("change_basedir: mkdir (%s): %s", dir,
214 sstrerror (errno, errbuf, sizeof (errbuf)));
219 status = chdir (dir);
223 ERROR ("change_basedir: chdir (%s): %s", dir,
224 sstrerror (errno, errbuf, sizeof (errbuf)));
231 } /* static int change_basedir (char *dir) */
234 static void update_kstat (void)
238 if ((kc = kstat_open ()) == NULL)
239 ERROR ("Unable to open kstat control structure");
244 kid = kstat_chain_update (kc);
247 INFO ("kstat chain has been updated");
251 ERROR ("kstat chain update failed");
252 /* else: everything works as expected */
256 } /* static void update_kstat (void) */
257 #endif /* HAVE_LIBKSTAT */
260 * Remove all settings but `-f' and `-C'
262 static void exit_usage (int status)
264 printf ("Usage: "PACKAGE" [OPTIONS]\n\n"
266 "Available options:\n"
268 " -C <file> Configuration file.\n"
269 " Default: "CONFIGFILE"\n"
270 " -t Test config and exit.\n"
271 " -T Test plugin read and exit.\n"
272 " -P <file> PID-file.\n"
273 " Default: "PIDFILE"\n"
275 " -f Don't fork to the background.\n"
277 " -h Display help (this message)\n"
278 "\nBuiltin defaults:\n"
279 " Config file "CONFIGFILE"\n"
280 " PID file "PIDFILE"\n"
281 " Plugin directory "PLUGINDIR"\n"
282 " Data directory "PKGLOCALSTATEDIR"\n"
283 "\n"PACKAGE" "VERSION", http://collectd.org/\n"
284 "by Florian octo Forster <octo@collectd.org>\n"
285 "for contributions see `AUTHORS'\n");
287 } /* static void exit_usage (int status) */
289 static int do_init (void)
299 ERROR ("sg_init: %s", sg_str_error (sg_get_error ()));
303 if (sg_drop_privileges ())
305 ERROR ("sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
313 } /* int do_init () */
316 static int do_loop (void)
318 cdtime_t interval = cf_get_default_interval ();
321 wait_until = cdtime () + interval;
325 struct timespec ts_wait = { 0, 0 };
332 /* Issue all plugins */
336 if (now >= wait_until)
338 WARNING ("Not sleeping because the next interval is "
339 "%.3f seconds in the past!",
340 CDTIME_T_TO_DOUBLE (now - wait_until));
341 wait_until = now + interval;
345 CDTIME_T_TO_TIMESPEC (wait_until - now, &ts_wait);
346 wait_until = wait_until + interval;
348 while ((loop == 0) && (nanosleep (&ts_wait, &ts_wait) != 0))
353 ERROR ("nanosleep failed: %s",
354 sstrerror (errno, errbuf,
359 } /* while (loop == 0) */
364 static int do_shutdown (void)
366 plugin_shutdown_all ();
368 } /* int do_shutdown */
371 static int pidfile_create (void)
374 const char *file = global_option_get ("PIDFile");
376 if ((fh = fopen (file, "w")) == NULL)
379 ERROR ("fopen (%s): %s", file,
380 sstrerror (errno, errbuf, sizeof (errbuf)));
384 fprintf (fh, "%i\n", (int) getpid ());
388 } /* static int pidfile_create (const char *file) */
390 static int pidfile_remove (void)
392 const char *file = global_option_get ("PIDFile");
394 DEBUG ("unlink (%s)", (file != NULL) ? file : "<null>");
395 return (unlink (file));
396 } /* static int pidfile_remove (const char *file) */
397 #endif /* COLLECT_DAEMON */
399 int main (int argc, char **argv)
401 struct sigaction sig_int_action;
402 struct sigaction sig_term_action;
403 struct sigaction sig_usr1_action;
404 struct sigaction sig_pipe_action;
405 char *configfile = CONFIGFILE;
407 int test_readall = 0;
410 struct sigaction sig_chld_action;
421 c = getopt (argc, argv, "htTC:"
440 global_option_set ("ReadThreads", "-1");
443 #endif /* COLLECT_DAEMON */
447 global_option_set ("PIDFile", optarg);
448 pidfile_from_cli = 1;
453 #endif /* COLLECT_DAEMON */
468 * Read options from the config file, the environment and the command
469 * line (in that order, with later options overwriting previous ones in
471 * Also, this will automatically load modules.
473 if (cf_read (configfile))
475 fprintf (stderr, "Error: Reading the config file failed!\n"
476 "Read the syslog for details.\n");
481 * Change directory. We do this _after_ reading the config and loading
482 * modules to relative paths work as expected.
484 if ((basedir = global_option_get ("BaseDir")) == NULL)
486 fprintf (stderr, "Don't have a basedir to use. This should not happen. Ever.");
489 else if (change_basedir (basedir))
491 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", basedir);
496 * Set global variables or, if that failes, exit. We cannot run with
497 * them being uninitialized. If nothing is configured, then defaults
498 * are being used. So this means that the user has actually done
501 if (init_global_variables () != 0)
511 memset (&sig_chld_action, '\0', sizeof (sig_chld_action));
512 sig_chld_action.sa_handler = SIG_IGN;
513 sigaction (SIGCHLD, &sig_chld_action, NULL);
517 if ((pid = fork ()) == -1)
521 fprintf (stderr, "fork: %s",
522 sstrerror (errno, errbuf,
529 /* printf ("Running (PID %i)\n", pid); */
533 /* Detach from session */
537 if (pidfile_create ())
540 /* close standard descriptors */
545 if (open ("/dev/null", O_RDWR) != 0)
547 ERROR ("Error: Could not connect `STDIN' to `/dev/null'");
552 ERROR ("Error: Could not connect `STDOUT' to `/dev/null'");
557 ERROR ("Error: Could not connect `STDERR' to `/dev/null'");
560 } /* if (daemonize) */
561 #endif /* COLLECT_DAEMON */
563 memset (&sig_pipe_action, '\0', sizeof (sig_pipe_action));
564 sig_pipe_action.sa_handler = SIG_IGN;
565 sigaction (SIGPIPE, &sig_pipe_action, NULL);
568 * install signal handlers
570 memset (&sig_int_action, '\0', sizeof (sig_int_action));
571 sig_int_action.sa_handler = sig_int_handler;
572 if (0 != sigaction (SIGINT, &sig_int_action, NULL)) {
574 ERROR ("Error: Failed to install a signal handler for signal INT: %s",
575 sstrerror (errno, errbuf, sizeof (errbuf)));
579 memset (&sig_term_action, '\0', sizeof (sig_term_action));
580 sig_term_action.sa_handler = sig_term_handler;
581 if (0 != sigaction (SIGTERM, &sig_term_action, NULL)) {
583 ERROR ("Error: Failed to install a signal handler for signal TERM: %s",
584 sstrerror (errno, errbuf, sizeof (errbuf)));
588 memset (&sig_usr1_action, '\0', sizeof (sig_usr1_action));
589 sig_usr1_action.sa_handler = sig_usr1_handler;
590 if (0 != sigaction (SIGUSR1, &sig_usr1_action, NULL)) {
592 ERROR ("Error: Failed to install a signal handler for signal USR1: %s",
593 sstrerror (errno, errbuf, sizeof (errbuf)));
598 * run the actual loops
604 if (plugin_read_all_once () != 0)
609 INFO ("Initialization complete, entering read-loop.");
614 INFO ("Exiting normally.");
621 #endif /* COLLECT_DAEMON */
623 return (exit_status);