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"
36 char hostname_g[DATA_MAX_NAME_LEN];
40 #endif /* HAVE_LIBKSTAT */
44 static void sigIntHandler (int signal)
49 static void sigTermHandler (int signal)
54 static int init_hostname (void)
58 struct addrinfo ai_hints;
59 struct addrinfo *ai_list;
60 struct addrinfo *ai_ptr;
63 str = global_option_get ("Hostname");
66 strncpy (hostname_g, str, sizeof (hostname_g));
67 hostname_g[sizeof (hostname_g) - 1] = '\0';
71 if (gethostname (hostname_g, sizeof (hostname_g)) != 0)
73 fprintf (stderr, "`gethostname' failed and no "
74 "hostname was configured.\n");
78 str = global_option_get ("FQDNLookup");
79 if ((strcasecmp ("false", str) == 0)
80 || (strcasecmp ("no", str) == 0)
81 || (strcasecmp ("off", str) == 0))
84 memset (&ai_hints, '\0', sizeof (ai_hints));
85 ai_hints.ai_flags = AI_CANONNAME;
87 status = getaddrinfo (hostname_g, NULL, &ai_hints, &ai_list);
90 ERROR ("Looking up \"%s\" failed. You have set the "
91 "\"FQDNLookup\" option, but I cannot resolve "
92 "my hostname to a fully qualified domain "
93 "name. Please fix you network "
94 "configuration.", hostname_g);
98 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
100 if (ai_ptr->ai_canonname == NULL)
103 strncpy (hostname_g, ai_ptr->ai_canonname, sizeof (hostname_g));
104 hostname_g[sizeof (hostname_g) - 1] = '\0';
108 freeaddrinfo (ai_list);
110 } /* int init_hostname */
112 static int init_global_variables (void)
116 str = global_option_get ("Interval");
119 interval_g = atoi (str);
122 fprintf (stderr, "Cannot set the interval to a correct value.\n"
123 "Please check your settings.\n");
126 DEBUG ("interval_g = %i;", interval_g);
128 if (init_hostname () != 0)
130 DEBUG ("hostname_g = %s;", hostname_g);
133 } /* int init_global_variables */
135 static int change_basedir (const char *orig_dir)
137 char *dir = strdup (orig_dir);
144 ERROR ("strdup failed: %s",
145 sstrerror (errno, errbuf, sizeof (errbuf)));
149 dirlen = strlen (dir);
150 while ((dirlen > 0) && (dir[dirlen - 1] == '/'))
151 dir[--dirlen] = '\0';
156 status = chdir (dir);
163 if (mkdir (orig_dir, 0755) == -1)
166 ERROR ("change_basedir: mkdir (%s): %s", orig_dir,
167 sstrerror (errno, errbuf,
171 else if (chdir (orig_dir) == -1)
174 ERROR ("chdir (%s): %s", orig_dir,
175 sstrerror (errno, errbuf,
183 ERROR ("chdir (%s): %s", orig_dir,
184 sstrerror (errno, errbuf,
191 } /* static int change_basedir (char *dir) */
194 static void update_kstat (void)
198 if ((kc = kstat_open ()) == NULL)
199 ERROR ("Unable to open kstat control structure");
204 kid = kstat_chain_update (kc);
207 INFO ("kstat chain has been updated");
211 ERROR ("kstat chain update failed");
212 /* else: everything works as expected */
216 } /* static void update_kstat (void) */
217 #endif /* HAVE_LIBKSTAT */
220 * Remove all settings but `-f' and `-C'
222 static void exit_usage (void)
224 printf ("Usage: "PACKAGE" [OPTIONS]\n\n"
226 "Available options:\n"
228 " -C <file> Configuration file.\n"
229 " Default: "CONFIGFILE"\n"
230 " -P <file> PID-file.\n"
231 " Default: "PIDFILE"\n"
233 " -f Don't fork to the background.\n"
235 "\nBuiltin defaults:\n"
236 " Config-File "CONFIGFILE"\n"
237 " PID-File "PIDFILE"\n"
238 " Data-Directory "PKGLOCALSTATEDIR"\n"
239 "\n"PACKAGE" "VERSION", http://collectd.org/\n"
240 "by Florian octo Forster <octo@verplant.org>\n"
241 "for contributions see `AUTHORS'\n");
243 } /* static void exit_usage (char *name) */
245 static int do_init (void)
255 ERROR ("sg_init: %s", sg_str_error (sg_get_error ()));
259 if (sg_drop_privileges ())
261 ERROR ("sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
269 } /* int do_init () */
272 static int do_loop (void)
274 struct timeval tv_now;
275 struct timeval tv_next;
276 struct timespec ts_wait;
280 if (gettimeofday (&tv_next, NULL) < 0)
283 ERROR ("gettimeofday failed: %s",
284 sstrerror (errno, errbuf,
288 tv_next.tv_sec += interval_g;
294 /* Issue all plugins */
297 if (gettimeofday (&tv_now, NULL) < 0)
300 ERROR ("gettimeofday failed: %s",
301 sstrerror (errno, errbuf,
306 if (timeval_sub_timespec (&tv_next, &tv_now, &ts_wait) != 0)
308 WARNING ("Not sleeping because "
309 "`timeval_sub_timespec' returned "
314 while ((loop == 0) && (nanosleep (&ts_wait, &ts_wait) == -1))
319 ERROR ("nanosleep failed: %s",
320 sstrerror (errno, errbuf,
325 } /* while (loop == 0) */
327 DEBUG ("return (0);");
331 static int do_shutdown (void)
333 plugin_shutdown_all ();
335 } /* int do_shutdown */
338 static int pidfile_create (void)
341 const char *file = global_option_get ("PIDFile");
343 if ((fh = fopen (file, "w")) == NULL)
346 ERROR ("fopen (%s): %s", file,
347 sstrerror (errno, errbuf, sizeof (errbuf)));
351 fprintf (fh, "%i\n", (int) getpid ());
355 } /* static int pidfile_create (const char *file) */
357 static int pidfile_remove (void)
359 const char *file = global_option_get ("PIDFile");
361 DEBUG ("unlink (%s)", (file != NULL) ? file : "<null>");
362 return (unlink (file));
363 } /* static int pidfile_remove (const char *file) */
364 #endif /* COLLECT_DAEMON */
366 int main (int argc, char **argv)
368 struct sigaction sigIntAction;
369 struct sigaction sigTermAction;
370 char *configfile = CONFIGFILE;
374 struct sigaction sigChldAction;
384 c = getopt (argc, argv, "htC:"
403 global_option_set ("PIDFile", optarg);
408 #endif /* COLLECT_DAEMON */
416 * Read options from the config file, the environment and the command
417 * line (in that order, with later options overwriting previous ones in
419 * Also, this will automatically load modules.
421 if (cf_read (configfile))
423 fprintf (stderr, "Error: Reading the config file failed!\n"
424 "Read the syslog for details.\n");
429 * Change directory. We do this _after_ reading the config and loading
430 * modules to relative paths work as expected.
432 if ((basedir = global_option_get ("BaseDir")) == NULL)
434 fprintf (stderr, "Don't have a basedir to use. This should not happen. Ever.");
437 else if (change_basedir (basedir))
439 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", basedir);
444 * Set global variables or, if that failes, exit. We cannot run with
445 * them being uninitialized. If nothing is configured, then defaults
446 * are being used. So this means that the user has actually done
449 if (init_global_variables () != 0)
459 memset (&sigChldAction, '\0', sizeof (sigChldAction));
460 sigChldAction.sa_handler = SIG_IGN;
461 sigaction (SIGCHLD, &sigChldAction, NULL);
465 if ((pid = fork ()) == -1)
469 fprintf (stderr, "fork: %s",
470 sstrerror (errno, errbuf,
477 /* printf ("Running (PID %i)\n", pid); */
481 /* Detach from session */
485 if (pidfile_create ())
488 /* close standard descriptors */
493 if (open ("/dev/null", O_RDWR) != 0)
495 ERROR ("Error: Could not connect `STDIN' to `/dev/null'");
500 ERROR ("Error: Could not connect `STDOUT' to `/dev/null'");
505 ERROR ("Error: Could not connect `STDERR' to `/dev/null'");
508 } /* if (daemonize) */
509 #endif /* COLLECT_DAEMON */
512 * install signal handlers
514 memset (&sigIntAction, '\0', sizeof (sigIntAction));
515 sigIntAction.sa_handler = sigIntHandler;
516 sigaction (SIGINT, &sigIntAction, NULL);
518 memset (&sigTermAction, '\0', sizeof (sigTermAction));
519 sigTermAction.sa_handler = sigTermHandler;
520 sigaction (SIGTERM, &sigTermAction, NULL);
523 * run the actual loops
529 INFO ("Exiting normally");
536 #endif /* COLLECT_DAEMON */