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"
38 char hostname_g[DATA_MAX_NAME_LEN];
42 #endif /* HAVE_LIBKSTAT */
46 static void *do_flush (void *arg)
48 INFO ("Flushing all data.");
49 plugin_flush_all (-1);
50 INFO ("Finished flushing all data.");
55 static void sigIntHandler (int signal)
60 static void sigTermHandler (int signal)
65 static void sigUsr1Handler (int signal)
70 /* flushing the data might take a while,
71 * so it should be done asynchronously */
72 pthread_attr_init (&attr);
73 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
74 pthread_create (&thread, &attr, do_flush, NULL);
77 static int init_hostname (void)
81 struct addrinfo ai_hints;
82 struct addrinfo *ai_list;
83 struct addrinfo *ai_ptr;
86 str = global_option_get ("Hostname");
89 strncpy (hostname_g, str, sizeof (hostname_g));
90 hostname_g[sizeof (hostname_g) - 1] = '\0';
94 if (gethostname (hostname_g, sizeof (hostname_g)) != 0)
96 fprintf (stderr, "`gethostname' failed and no "
97 "hostname was configured.\n");
101 str = global_option_get ("FQDNLookup");
102 if ((strcasecmp ("false", str) == 0)
103 || (strcasecmp ("no", str) == 0)
104 || (strcasecmp ("off", str) == 0))
107 memset (&ai_hints, '\0', sizeof (ai_hints));
108 ai_hints.ai_flags = AI_CANONNAME;
110 status = getaddrinfo (hostname_g, NULL, &ai_hints, &ai_list);
113 ERROR ("Looking up \"%s\" failed. You have set the "
114 "\"FQDNLookup\" option, but I cannot resolve "
115 "my hostname to a fully qualified domain "
116 "name. Please fix you network "
117 "configuration.", hostname_g);
121 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
123 if (ai_ptr->ai_canonname == NULL)
126 strncpy (hostname_g, ai_ptr->ai_canonname, sizeof (hostname_g));
127 hostname_g[sizeof (hostname_g) - 1] = '\0';
131 freeaddrinfo (ai_list);
133 } /* int init_hostname */
135 static int init_global_variables (void)
139 str = global_option_get ("Interval");
142 interval_g = atoi (str);
145 fprintf (stderr, "Cannot set the interval to a correct value.\n"
146 "Please check your settings.\n");
149 DEBUG ("interval_g = %i;", interval_g);
151 if (init_hostname () != 0)
153 DEBUG ("hostname_g = %s;", hostname_g);
156 } /* int init_global_variables */
158 static int change_basedir (const char *orig_dir)
160 char *dir = strdup (orig_dir);
167 ERROR ("strdup failed: %s",
168 sstrerror (errno, errbuf, sizeof (errbuf)));
172 dirlen = strlen (dir);
173 while ((dirlen > 0) && (dir[dirlen - 1] == '/'))
174 dir[--dirlen] = '\0';
179 status = chdir (dir);
186 if (mkdir (orig_dir, 0755) == -1)
189 ERROR ("change_basedir: mkdir (%s): %s", orig_dir,
190 sstrerror (errno, errbuf,
194 else if (chdir (orig_dir) == -1)
197 ERROR ("chdir (%s): %s", orig_dir,
198 sstrerror (errno, errbuf,
206 ERROR ("chdir (%s): %s", orig_dir,
207 sstrerror (errno, errbuf,
214 } /* static int change_basedir (char *dir) */
217 static void update_kstat (void)
221 if ((kc = kstat_open ()) == NULL)
222 ERROR ("Unable to open kstat control structure");
227 kid = kstat_chain_update (kc);
230 INFO ("kstat chain has been updated");
234 ERROR ("kstat chain update failed");
235 /* else: everything works as expected */
239 } /* static void update_kstat (void) */
240 #endif /* HAVE_LIBKSTAT */
243 * Remove all settings but `-f' and `-C'
245 static void exit_usage (void)
247 printf ("Usage: "PACKAGE" [OPTIONS]\n\n"
249 "Available options:\n"
251 " -C <file> Configuration file.\n"
252 " Default: "CONFIGFILE"\n"
253 " -P <file> PID-file.\n"
254 " Default: "PIDFILE"\n"
256 " -f Don't fork to the background.\n"
258 "\nBuiltin defaults:\n"
259 " Config-File "CONFIGFILE"\n"
260 " PID-File "PIDFILE"\n"
261 " Data-Directory "PKGLOCALSTATEDIR"\n"
262 "\n"PACKAGE" "VERSION", http://collectd.org/\n"
263 "by Florian octo Forster <octo@verplant.org>\n"
264 "for contributions see `AUTHORS'\n");
266 } /* static void exit_usage (char *name) */
268 static int do_init (void)
278 ERROR ("sg_init: %s", sg_str_error (sg_get_error ()));
282 if (sg_drop_privileges ())
284 ERROR ("sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
292 } /* int do_init () */
295 static int do_loop (void)
297 struct timeval tv_now;
298 struct timeval tv_next;
299 struct timespec ts_wait;
303 if (gettimeofday (&tv_next, NULL) < 0)
306 ERROR ("gettimeofday failed: %s",
307 sstrerror (errno, errbuf,
311 tv_next.tv_sec += interval_g;
317 /* Issue all plugins */
320 if (gettimeofday (&tv_now, NULL) < 0)
323 ERROR ("gettimeofday failed: %s",
324 sstrerror (errno, errbuf,
329 if (timeval_sub_timespec (&tv_next, &tv_now, &ts_wait) != 0)
331 WARNING ("Not sleeping because "
332 "`timeval_sub_timespec' returned "
337 while ((loop == 0) && (nanosleep (&ts_wait, &ts_wait) == -1))
342 ERROR ("nanosleep failed: %s",
343 sstrerror (errno, errbuf,
348 } /* while (loop == 0) */
350 DEBUG ("return (0);");
354 static int do_shutdown (void)
356 plugin_shutdown_all ();
358 } /* int do_shutdown */
361 static int pidfile_create (void)
364 const char *file = global_option_get ("PIDFile");
366 if ((fh = fopen (file, "w")) == NULL)
369 ERROR ("fopen (%s): %s", file,
370 sstrerror (errno, errbuf, sizeof (errbuf)));
374 fprintf (fh, "%i\n", (int) getpid ());
378 } /* static int pidfile_create (const char *file) */
380 static int pidfile_remove (void)
382 const char *file = global_option_get ("PIDFile");
384 DEBUG ("unlink (%s)", (file != NULL) ? file : "<null>");
385 return (unlink (file));
386 } /* static int pidfile_remove (const char *file) */
387 #endif /* COLLECT_DAEMON */
389 int main (int argc, char **argv)
391 struct sigaction sigIntAction;
392 struct sigaction sigTermAction;
393 struct sigaction sigUsr1Action;
394 char *configfile = CONFIGFILE;
398 struct sigaction sigChldAction;
408 c = getopt (argc, argv, "htC:"
427 global_option_set ("PIDFile", optarg);
432 #endif /* COLLECT_DAEMON */
440 * Read options from the config file, the environment and the command
441 * line (in that order, with later options overwriting previous ones in
443 * Also, this will automatically load modules.
445 if (cf_read (configfile))
447 fprintf (stderr, "Error: Reading the config file failed!\n"
448 "Read the syslog for details.\n");
453 * Change directory. We do this _after_ reading the config and loading
454 * modules to relative paths work as expected.
456 if ((basedir = global_option_get ("BaseDir")) == NULL)
458 fprintf (stderr, "Don't have a basedir to use. This should not happen. Ever.");
461 else if (change_basedir (basedir))
463 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", basedir);
468 * Set global variables or, if that failes, exit. We cannot run with
469 * them being uninitialized. If nothing is configured, then defaults
470 * are being used. So this means that the user has actually done
473 if (init_global_variables () != 0)
483 memset (&sigChldAction, '\0', sizeof (sigChldAction));
484 sigChldAction.sa_handler = SIG_IGN;
485 sigaction (SIGCHLD, &sigChldAction, NULL);
489 if ((pid = fork ()) == -1)
493 fprintf (stderr, "fork: %s",
494 sstrerror (errno, errbuf,
501 /* printf ("Running (PID %i)\n", pid); */
505 /* Detach from session */
509 if (pidfile_create ())
512 /* close standard descriptors */
517 if (open ("/dev/null", O_RDWR) != 0)
519 ERROR ("Error: Could not connect `STDIN' to `/dev/null'");
524 ERROR ("Error: Could not connect `STDOUT' to `/dev/null'");
529 ERROR ("Error: Could not connect `STDERR' to `/dev/null'");
532 } /* if (daemonize) */
533 #endif /* COLLECT_DAEMON */
536 * install signal handlers
538 memset (&sigIntAction, '\0', sizeof (sigIntAction));
539 sigIntAction.sa_handler = sigIntHandler;
540 sigaction (SIGINT, &sigIntAction, NULL);
542 memset (&sigTermAction, '\0', sizeof (sigTermAction));
543 sigTermAction.sa_handler = sigTermHandler;
544 sigaction (SIGTERM, &sigTermAction, NULL);
546 memset (&sigUsr1Action, '\0', sizeof (sigUsr1Action));
547 sigUsr1Action.sa_handler = sigUsr1Handler;
548 sigaction (SIGUSR1, &sigUsr1Action, NULL);
551 * run the actual loops
557 INFO ("Exiting normally");
564 #endif /* COLLECT_DAEMON */