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];
47 #endif /* HAVE_LIBKSTAT */
51 static void *do_flush (void __attribute__((unused)) *arg)
53 INFO ("Flushing all data.");
54 plugin_flush (NULL, -1, NULL);
55 INFO ("Finished flushing all data.");
60 static void sig_int_handler (int __attribute__((unused)) signal)
65 static void sig_term_handler (int __attribute__((unused)) signal)
70 static void sig_usr1_handler (int __attribute__((unused)) signal)
75 /* flushing the data might take a while,
76 * so it should be done asynchronously */
77 pthread_attr_init (&attr);
78 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
79 pthread_create (&thread, &attr, do_flush, NULL);
82 static int init_hostname (void)
86 struct addrinfo ai_hints;
87 struct addrinfo *ai_list;
88 struct addrinfo *ai_ptr;
91 str = global_option_get ("Hostname");
94 sstrncpy (hostname_g, str, sizeof (hostname_g));
98 if (gethostname (hostname_g, sizeof (hostname_g)) != 0)
100 fprintf (stderr, "`gethostname' failed and no "
101 "hostname was configured.\n");
105 str = global_option_get ("FQDNLookup");
109 memset (&ai_hints, '\0', sizeof (ai_hints));
110 ai_hints.ai_flags = AI_CANONNAME;
112 status = getaddrinfo (hostname_g, NULL, &ai_hints, &ai_list);
115 ERROR ("Looking up \"%s\" failed. You have set the "
116 "\"FQDNLookup\" option, but I cannot resolve "
117 "my hostname to a fully qualified domain "
118 "name. Please fix you network "
119 "configuration.", hostname_g);
123 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
125 if (ai_ptr->ai_canonname == NULL)
128 sstrncpy (hostname_g, ai_ptr->ai_canonname, sizeof (hostname_g));
132 freeaddrinfo (ai_list);
134 } /* int init_hostname */
136 static int init_global_variables (void)
140 str = global_option_get ("Interval");
143 interval_g = atoi (str);
146 fprintf (stderr, "Cannot set the interval to a correct value.\n"
147 "Please check your settings.\n");
150 DEBUG ("interval_g = %i;", 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@verplant.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 struct timeval tv_now;
318 struct timeval tv_next;
319 struct timeval tv_wait;
320 struct timespec ts_wait;
324 if (gettimeofday (&tv_next, NULL) < 0)
327 ERROR ("gettimeofday failed: %s",
328 sstrerror (errno, errbuf,
332 tv_next.tv_sec += interval_g;
338 /* Issue all plugins */
341 if (gettimeofday (&tv_now, NULL) < 0)
344 ERROR ("gettimeofday failed: %s",
345 sstrerror (errno, errbuf,
350 if (timeval_cmp (tv_next, tv_now, &tv_wait) <= 0)
352 WARNING ("Not sleeping because the next interval is "
353 "%i.%06i seconds in the past!",
354 (int) tv_wait.tv_sec, (int) tv_wait.tv_usec);
358 ts_wait.tv_sec = tv_wait.tv_sec;
359 ts_wait.tv_nsec = (long) (1000 * tv_wait.tv_usec);
361 while ((loop == 0) && (nanosleep (&ts_wait, &ts_wait) == -1))
366 ERROR ("nanosleep failed: %s",
367 sstrerror (errno, errbuf,
372 } /* while (loop == 0) */
374 DEBUG ("return (0);");
378 static int do_shutdown (void)
380 plugin_shutdown_all ();
382 } /* int do_shutdown */
385 static int pidfile_create (void)
388 const char *file = global_option_get ("PIDFile");
390 if ((fh = fopen (file, "w")) == NULL)
393 ERROR ("fopen (%s): %s", file,
394 sstrerror (errno, errbuf, sizeof (errbuf)));
398 fprintf (fh, "%i\n", (int) getpid ());
402 } /* static int pidfile_create (const char *file) */
404 static int pidfile_remove (void)
406 const char *file = global_option_get ("PIDFile");
408 DEBUG ("unlink (%s)", (file != NULL) ? file : "<null>");
409 return (unlink (file));
410 } /* static int pidfile_remove (const char *file) */
411 #endif /* COLLECT_DAEMON */
413 int main (int argc, char **argv)
415 struct sigaction sig_int_action;
416 struct sigaction sig_term_action;
417 struct sigaction sig_usr1_action;
418 struct sigaction sig_pipe_action;
419 char *configfile = CONFIGFILE;
421 int test_readall = 0;
424 struct sigaction sig_chld_action;
435 c = getopt (argc, argv, "htTC:"
454 global_option_set ("ReadThreads", "-1");
457 #endif /* COLLECT_DAEMON */
461 global_option_set ("PIDFile", optarg);
466 #endif /* COLLECT_DAEMON */
479 * Read options from the config file, the environment and the command
480 * line (in that order, with later options overwriting previous ones in
482 * Also, this will automatically load modules.
484 if (cf_read (configfile))
486 fprintf (stderr, "Error: Reading the config file failed!\n"
487 "Read the syslog for details.\n");
492 * Change directory. We do this _after_ reading the config and loading
493 * modules to relative paths work as expected.
495 if ((basedir = global_option_get ("BaseDir")) == NULL)
497 fprintf (stderr, "Don't have a basedir to use. This should not happen. Ever.");
500 else if (change_basedir (basedir))
502 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", basedir);
507 * Set global variables or, if that failes, exit. We cannot run with
508 * them being uninitialized. If nothing is configured, then defaults
509 * are being used. So this means that the user has actually done
512 if (init_global_variables () != 0)
522 memset (&sig_chld_action, '\0', sizeof (sig_chld_action));
523 sig_chld_action.sa_handler = SIG_IGN;
524 sigaction (SIGCHLD, &sig_chld_action, NULL);
528 if ((pid = fork ()) == -1)
532 fprintf (stderr, "fork: %s",
533 sstrerror (errno, errbuf,
540 /* printf ("Running (PID %i)\n", pid); */
544 /* Detach from session */
548 if (pidfile_create ())
551 /* close standard descriptors */
556 if (open ("/dev/null", O_RDWR) != 0)
558 ERROR ("Error: Could not connect `STDIN' to `/dev/null'");
563 ERROR ("Error: Could not connect `STDOUT' to `/dev/null'");
568 ERROR ("Error: Could not connect `STDERR' to `/dev/null'");
571 } /* if (daemonize) */
572 #endif /* COLLECT_DAEMON */
574 memset (&sig_pipe_action, '\0', sizeof (sig_pipe_action));
575 sig_pipe_action.sa_handler = SIG_IGN;
576 sigaction (SIGPIPE, &sig_pipe_action, NULL);
579 * install signal handlers
581 memset (&sig_int_action, '\0', sizeof (sig_int_action));
582 sig_int_action.sa_handler = sig_int_handler;
583 if (0 != sigaction (SIGINT, &sig_int_action, NULL)) {
585 ERROR ("Error: Failed to install a signal handler for signal INT: %s",
586 sstrerror (errno, errbuf, sizeof (errbuf)));
590 memset (&sig_term_action, '\0', sizeof (sig_term_action));
591 sig_term_action.sa_handler = sig_term_handler;
592 if (0 != sigaction (SIGTERM, &sig_term_action, NULL)) {
594 ERROR ("Error: Failed to install a signal handler for signal TERM: %s",
595 sstrerror (errno, errbuf, sizeof (errbuf)));
599 memset (&sig_usr1_action, '\0', sizeof (sig_usr1_action));
600 sig_usr1_action.sa_handler = sig_usr1_handler;
601 if (0 != sigaction (SIGUSR1, &sig_usr1_action, NULL)) {
603 ERROR ("Error: Failed to install a signal handler for signal USR1: %s",
604 sstrerror (errno, errbuf, sizeof (errbuf)));
609 * run the actual loops
615 if (plugin_read_all_once () != 0)
620 INFO ("Initialization complete, entering read-loop.");
625 INFO ("Exiting normally.");
632 #endif /* COLLECT_DAEMON */
634 return (exit_status);