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"
32 #include "types_list.h"
37 char hostname_g[DATA_MAX_NAME_LEN];
41 #endif /* HAVE_LIBKSTAT */
45 static void sigIntHandler (int signal)
50 static void sigTermHandler (int signal)
55 static int init_hostname (void)
59 struct addrinfo ai_hints;
60 struct addrinfo *ai_list;
61 struct addrinfo *ai_ptr;
64 str = global_option_get ("Hostname");
67 strncpy (hostname_g, str, sizeof (hostname_g));
68 hostname_g[sizeof (hostname_g) - 1] = '\0';
72 if (gethostname (hostname_g, sizeof (hostname_g)) != 0)
74 fprintf (stderr, "`gethostname' failed and no "
75 "hostname was configured.\n");
79 str = global_option_get ("FQDNLookup");
80 if ((strcasecmp ("false", str) == 0)
81 || (strcasecmp ("no", str) == 0)
82 || (strcasecmp ("off", str) == 0))
85 memset (&ai_hints, '\0', sizeof (ai_hints));
86 ai_hints.ai_flags = AI_CANONNAME;
88 status = getaddrinfo (hostname_g, NULL, &ai_hints, &ai_list);
91 ERROR ("getaddrinfo failed.");
95 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
97 if (ai_ptr->ai_canonname == NULL)
100 strncpy (hostname_g, ai_ptr->ai_canonname, sizeof (hostname_g));
101 hostname_g[sizeof (hostname_g) - 1] = '\0';
105 freeaddrinfo (ai_list);
107 } /* int init_hostname */
109 static int init_global_variables (void)
113 str = global_option_get ("Interval");
116 interval_g = atoi (str);
119 fprintf (stderr, "Cannot set the interval to a correct value.\n"
120 "Please check your settings.\n");
123 DEBUG ("interval_g = %i;", interval_g);
125 if (init_hostname () != 0)
127 DEBUG ("hostname_g = %s;", hostname_g);
130 } /* int init_global_variables */
132 static int change_basedir (const char *orig_dir)
134 char *dir = strdup (orig_dir);
141 ERROR ("strdup failed: %s",
142 sstrerror (errno, errbuf, sizeof (errbuf)));
146 dirlen = strlen (dir);
147 while ((dirlen > 0) && (dir[dirlen - 1] == '/'))
148 dir[--dirlen] = '\0';
153 status = chdir (dir);
160 if (mkdir (orig_dir, 0755) == -1)
163 ERROR ("change_basedir: mkdir (%s): %s", orig_dir,
164 sstrerror (errno, errbuf,
168 else if (chdir (orig_dir) == -1)
171 ERROR ("chdir (%s): %s", orig_dir,
172 sstrerror (errno, errbuf,
180 ERROR ("chdir (%s): %s", orig_dir,
181 sstrerror (errno, errbuf,
188 } /* static int change_basedir (char *dir) */
191 static void update_kstat (void)
195 if ((kc = kstat_open ()) == NULL)
196 ERROR ("Unable to open kstat control structure");
201 kid = kstat_chain_update (kc);
204 INFO ("kstat chain has been updated");
208 ERROR ("kstat chain update failed");
209 /* else: everything works as expected */
213 } /* static void update_kstat (void) */
214 #endif /* HAVE_LIBKSTAT */
217 * Remove all settings but `-f' and `-C'
219 static void exit_usage (char *name)
221 printf ("Usage: "PACKAGE" [OPTIONS]\n\n"
223 "Available options:\n"
225 " -C <file> Configuration file.\n"
226 " Default: "CONFIGFILE"\n"
227 " -P <file> PID-file.\n"
228 " Default: "PIDFILE"\n"
230 " -f Don't fork to the background.\n"
232 "\nBuiltin defaults:\n"
233 " Config-File "CONFIGFILE"\n"
234 " PID-File "PIDFILE"\n"
235 " Data-Directory "PKGLOCALSTATEDIR"\n"
236 "\n"PACKAGE" "VERSION", http://collectd.org/\n"
237 "by Florian octo Forster <octo@verplant.org>\n"
238 "for contributions see `AUTHORS'\n");
240 } /* static void exit_usage (char *name) */
242 static int do_init (void)
252 ERROR ("sg_init: %s", sg_str_error (sg_get_error ()));
256 if (sg_drop_privileges ())
258 ERROR ("sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
267 } /* int do_init () */
270 static int do_loop (void)
272 struct timeval tv_now;
273 struct timeval tv_next;
274 struct timespec ts_wait;
278 if (gettimeofday (&tv_next, NULL) < 0)
281 ERROR ("gettimeofday failed: %s",
282 sstrerror (errno, errbuf,
286 tv_next.tv_sec += interval_g;
292 /* Issue all plugins */
293 plugin_read_all (&loop);
295 if (gettimeofday (&tv_now, NULL) < 0)
298 ERROR ("gettimeofday failed: %s",
299 sstrerror (errno, errbuf,
304 if (timeval_sub_timespec (&tv_next, &tv_now, &ts_wait) != 0)
306 WARNING ("Not sleeping because "
307 "`timeval_sub_timespec' returned "
312 while ((loop == 0) && (nanosleep (&ts_wait, &ts_wait) == -1))
317 ERROR ("nanosleep failed: %s",
318 sstrerror (errno, errbuf,
323 } /* while (loop == 0) */
325 DEBUG ("return (0);");
329 static int do_shutdown (void)
331 plugin_shutdown_all ();
333 } /* int do_shutdown */
336 static int pidfile_create (void)
339 const char *file = global_option_get ("PIDFile");
341 if ((fh = fopen (file, "w")) == NULL)
344 ERROR ("fopen (%s): %s", file,
345 sstrerror (errno, errbuf, sizeof (errbuf)));
349 fprintf (fh, "%i\n", (int) getpid ());
353 } /* static int pidfile_create (const char *file) */
355 static int pidfile_remove (void)
357 const char *file = global_option_get ("PIDFile");
359 DEBUG ("unlink (%s)", (file != NULL) ? file : "<null>");
360 return (unlink (file));
361 } /* static int pidfile_remove (const char *file) */
362 #endif /* COLLECT_DAEMON */
364 int main (int argc, char **argv)
366 struct sigaction sigIntAction;
367 struct sigaction sigTermAction;
368 char *configfile = CONFIGFILE;
372 struct sigaction sigChldAction;
382 c = getopt (argc, argv, "htC:"
401 global_option_set ("PIDFile", optarg);
406 #endif /* COLLECT_DAEMON */
409 exit_usage (argv[0]);
414 * Read options from the config file, the environment and the command
415 * line (in that order, with later options overwriting previous ones in
417 * Also, this will automatically load modules.
419 if (cf_read (configfile))
421 fprintf (stderr, "Error: Reading the config file failed!\n"
422 "Read the syslog for details.\n");
427 * Change directory. We do this _after_ reading the config and loading
428 * modules to relative paths work as expected.
430 if ((basedir = global_option_get ("BaseDir")) == NULL)
432 fprintf (stderr, "Don't have a basedir to use. This should not happen. Ever.");
435 else if (change_basedir (basedir))
437 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", basedir);
442 * Set global variables or, if that failes, exit. We cannot run with
443 * them being uninitialized. If nothing is configured, then defaults
444 * are being used. So this means that the user has actually done
447 if (init_global_variables () != 0)
457 memset (&sigChldAction, '\0', sizeof (sigChldAction));
458 sigChldAction.sa_handler = SIG_IGN;
459 sigaction (SIGCHLD, &sigChldAction, NULL);
463 if ((pid = fork ()) == -1)
467 fprintf (stderr, "fork: %s",
468 sstrerror (errno, errbuf,
475 /* printf ("Running (PID %i)\n", pid); */
479 /* Detach from session */
483 if (pidfile_create ())
486 /* close standard descriptors */
491 if (open ("/dev/null", O_RDWR) != 0)
493 ERROR ("Error: Could not connect `STDIN' to `/dev/null'");
498 ERROR ("Error: Could not connect `STDOUT' to `/dev/null'");
503 ERROR ("Error: Could not connect `STDERR' to `/dev/null'");
506 } /* if (daemonize) */
507 #endif /* COLLECT_DAEMON */
510 * install signal handlers
512 memset (&sigIntAction, '\0', sizeof (sigIntAction));
513 sigIntAction.sa_handler = sigIntHandler;
514 sigaction (SIGINT, &sigIntAction, NULL);
516 memset (&sigTermAction, '\0', sizeof (sigTermAction));
517 sigTermAction.sa_handler = sigTermHandler;
518 sigaction (SIGTERM, &sigTermAction, NULL);
521 * run the actual loops
527 INFO ("Exiting normally");
534 #endif /* COLLECT_DAEMON */