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>
31 #include "configfile.h"
34 # include <statgrab.h>
40 char hostname_g[DATA_MAX_NAME_LEN];
44 #endif /* HAVE_LIBKSTAT */
48 static void sigIntHandler (int signal)
53 static void sigTermHandler (int signal)
58 static int init_hostname (void)
62 struct addrinfo ai_hints;
63 struct addrinfo *ai_list;
64 struct addrinfo *ai_ptr;
67 str = global_option_get ("Hostname");
70 strncpy (hostname_g, str, sizeof (hostname_g));
71 hostname_g[sizeof (hostname_g) - 1] = '\0';
75 if (gethostname (hostname_g, sizeof (hostname_g)) != 0)
77 fprintf (stderr, "`gethostname' failed and no "
78 "hostname was configured.\n");
82 str = global_option_get ("FQDNLookup");
83 if ((strcasecmp ("false", str) == 0)
84 || (strcasecmp ("no", str) == 0)
85 || (strcasecmp ("off", str) == 0))
88 memset (&ai_hints, '\0', sizeof (ai_hints));
89 ai_hints.ai_flags = AI_CANONNAME;
91 status = getaddrinfo (hostname_g, NULL, &ai_hints, &ai_list);
94 ERROR ("Looking up \"%s\" failed. You have set the "
95 "\"FQDNLookup\" option, but I cannot resolve "
96 "my hostname to a fully qualified domain "
97 "name. Please fix you network "
98 "configuration.", hostname_g);
102 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
104 if (ai_ptr->ai_canonname == NULL)
107 strncpy (hostname_g, ai_ptr->ai_canonname, sizeof (hostname_g));
108 hostname_g[sizeof (hostname_g) - 1] = '\0';
112 freeaddrinfo (ai_list);
114 } /* int init_hostname */
116 static int init_global_variables (void)
120 str = global_option_get ("Interval");
123 interval_g = atoi (str);
126 fprintf (stderr, "Cannot set the interval to a correct value.\n"
127 "Please check your settings.\n");
130 DEBUG ("interval_g = %i;", interval_g);
132 if (init_hostname () != 0)
134 DEBUG ("hostname_g = %s;", hostname_g);
137 } /* int init_global_variables */
139 static int change_basedir (const char *orig_dir)
141 char *dir = strdup (orig_dir);
148 ERROR ("strdup failed: %s",
149 sstrerror (errno, errbuf, sizeof (errbuf)));
153 dirlen = strlen (dir);
154 while ((dirlen > 0) && (dir[dirlen - 1] == '/'))
155 dir[--dirlen] = '\0';
160 status = chdir (dir);
167 if (mkdir (orig_dir, 0755) == -1)
170 ERROR ("change_basedir: mkdir (%s): %s", orig_dir,
171 sstrerror (errno, errbuf,
175 else if (chdir (orig_dir) == -1)
178 ERROR ("chdir (%s): %s", orig_dir,
179 sstrerror (errno, errbuf,
187 ERROR ("chdir (%s): %s", orig_dir,
188 sstrerror (errno, errbuf,
195 } /* static int change_basedir (char *dir) */
198 static void update_kstat (void)
202 if ((kc = kstat_open ()) == NULL)
203 ERROR ("Unable to open kstat control structure");
208 kid = kstat_chain_update (kc);
211 INFO ("kstat chain has been updated");
215 ERROR ("kstat chain update failed");
216 /* else: everything works as expected */
220 } /* static void update_kstat (void) */
221 #endif /* HAVE_LIBKSTAT */
224 * Remove all settings but `-f' and `-C'
226 static void exit_usage (void)
228 printf ("Usage: "PACKAGE" [OPTIONS]\n\n"
230 "Available options:\n"
232 " -C <file> Configuration file.\n"
233 " Default: "CONFIGFILE"\n"
234 " -P <file> PID-file.\n"
235 " Default: "PIDFILE"\n"
237 " -f Don't fork to the background.\n"
239 "\nBuiltin defaults:\n"
240 " Config-File "CONFIGFILE"\n"
241 " PID-File "PIDFILE"\n"
242 " Data-Directory "PKGLOCALSTATEDIR"\n"
243 "\n"PACKAGE" "VERSION", http://collectd.org/\n"
244 "by Florian octo Forster <octo@verplant.org>\n"
245 "for contributions see `AUTHORS'\n");
247 } /* static void exit_usage (char *name) */
249 static int do_init (void)
259 ERROR ("sg_init: %s", sg_str_error (sg_get_error ()));
263 if (sg_drop_privileges ())
265 ERROR ("sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
273 } /* int do_init () */
276 static int do_loop (void)
278 struct timeval tv_now;
279 struct timeval tv_next;
280 struct timespec ts_wait;
284 if (gettimeofday (&tv_next, NULL) < 0)
287 ERROR ("gettimeofday failed: %s",
288 sstrerror (errno, errbuf,
292 tv_next.tv_sec += interval_g;
298 /* Issue all plugins */
301 if (gettimeofday (&tv_now, NULL) < 0)
304 ERROR ("gettimeofday failed: %s",
305 sstrerror (errno, errbuf,
310 if (timeval_sub_timespec (&tv_next, &tv_now, &ts_wait) != 0)
312 WARNING ("Not sleeping because "
313 "`timeval_sub_timespec' returned "
318 while ((loop == 0) && (nanosleep (&ts_wait, &ts_wait) == -1))
323 ERROR ("nanosleep failed: %s",
324 sstrerror (errno, errbuf,
329 } /* while (loop == 0) */
331 DEBUG ("return (0);");
335 static int do_shutdown (void)
337 plugin_shutdown_all ();
339 } /* int do_shutdown */
342 static int pidfile_create (void)
345 const char *file = global_option_get ("PIDFile");
347 if ((fh = fopen (file, "w")) == NULL)
350 ERROR ("fopen (%s): %s", file,
351 sstrerror (errno, errbuf, sizeof (errbuf)));
355 fprintf (fh, "%i\n", (int) getpid ());
359 } /* static int pidfile_create (const char *file) */
361 static int pidfile_remove (void)
363 const char *file = global_option_get ("PIDFile");
365 DEBUG ("unlink (%s)", (file != NULL) ? file : "<null>");
366 return (unlink (file));
367 } /* static int pidfile_remove (const char *file) */
368 #endif /* COLLECT_DAEMON */
370 int main (int argc, char **argv)
372 struct sigaction sigIntAction;
373 struct sigaction sigTermAction;
374 char *configfile = CONFIGFILE;
378 struct sigaction sigChldAction;
388 c = getopt (argc, argv, "htC:"
407 global_option_set ("PIDFile", optarg);
412 #endif /* COLLECT_DAEMON */
420 * Read options from the config file, the environment and the command
421 * line (in that order, with later options overwriting previous ones in
423 * Also, this will automatically load modules.
425 if (cf_read (configfile))
427 fprintf (stderr, "Error: Reading the config file failed!\n"
428 "Read the syslog for details.\n");
433 * Change directory. We do this _after_ reading the config and loading
434 * modules to relative paths work as expected.
436 if ((basedir = global_option_get ("BaseDir")) == NULL)
438 fprintf (stderr, "Don't have a basedir to use. This should not happen. Ever.");
441 else if (change_basedir (basedir))
443 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", basedir);
448 * Set global variables or, if that failes, exit. We cannot run with
449 * them being uninitialized. If nothing is configured, then defaults
450 * are being used. So this means that the user has actually done
453 if (init_global_variables () != 0)
463 memset (&sigChldAction, '\0', sizeof (sigChldAction));
464 sigChldAction.sa_handler = SIG_IGN;
465 sigaction (SIGCHLD, &sigChldAction, NULL);
469 if ((pid = fork ()) == -1)
473 fprintf (stderr, "fork: %s",
474 sstrerror (errno, errbuf,
481 /* printf ("Running (PID %i)\n", pid); */
485 /* Detach from session */
489 if (pidfile_create ())
492 /* close standard descriptors */
497 if (open ("/dev/null", O_RDWR) != 0)
499 ERROR ("Error: Could not connect `STDIN' to `/dev/null'");
504 ERROR ("Error: Could not connect `STDOUT' to `/dev/null'");
509 ERROR ("Error: Could not connect `STDERR' to `/dev/null'");
512 } /* if (daemonize) */
513 #endif /* COLLECT_DAEMON */
516 * install signal handlers
518 memset (&sigIntAction, '\0', sizeof (sigIntAction));
519 sigIntAction.sa_handler = sigIntHandler;
520 sigaction (SIGINT, &sigIntAction, NULL);
522 memset (&sigTermAction, '\0', sizeof (sigTermAction));
523 sigTermAction.sa_handler = sigTermHandler;
524 sigaction (SIGTERM, &sigTermAction, NULL);
527 * run the actual loops
533 INFO ("Exiting normally");
540 #endif /* COLLECT_DAEMON */