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 ("getaddrinfo failed.");
94 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
96 if (ai_ptr->ai_canonname == NULL)
99 strncpy (hostname_g, ai_ptr->ai_canonname, sizeof (hostname_g));
100 hostname_g[sizeof (hostname_g) - 1] = '\0';
104 freeaddrinfo (ai_list);
106 } /* int init_hostname */
108 static int init_global_variables (void)
112 str = global_option_get ("Interval");
115 interval_g = atoi (str);
118 fprintf (stderr, "Cannot set the interval to a correct value.\n"
119 "Please check your settings.\n");
122 DEBUG ("interval_g = %i;", interval_g);
124 if (init_hostname () != 0)
126 DEBUG ("hostname_g = %s;", hostname_g);
129 } /* int init_global_variables */
131 static int change_basedir (const char *orig_dir)
133 char *dir = strdup (orig_dir);
140 ERROR ("strdup failed: %s",
141 sstrerror (errno, errbuf, sizeof (errbuf)));
145 dirlen = strlen (dir);
146 while ((dirlen > 0) && (dir[dirlen - 1] == '/'))
147 dir[--dirlen] = '\0';
152 status = chdir (dir);
159 if (mkdir (orig_dir, 0755) == -1)
162 ERROR ("change_basedir: mkdir (%s): %s", orig_dir,
163 sstrerror (errno, errbuf,
167 else if (chdir (orig_dir) == -1)
170 ERROR ("chdir (%s): %s", orig_dir,
171 sstrerror (errno, errbuf,
179 ERROR ("chdir (%s): %s", orig_dir,
180 sstrerror (errno, errbuf,
187 } /* static int change_basedir (char *dir) */
190 static void update_kstat (void)
194 if ((kc = kstat_open ()) == NULL)
195 ERROR ("Unable to open kstat control structure");
200 kid = kstat_chain_update (kc);
203 INFO ("kstat chain has been updated");
207 ERROR ("kstat chain update failed");
208 /* else: everything works as expected */
212 } /* static void update_kstat (void) */
213 #endif /* HAVE_LIBKSTAT */
216 * Remove all settings but `-f' and `-C'
218 static void exit_usage (char *name)
220 printf ("Usage: "PACKAGE" [OPTIONS]\n\n"
222 "Available options:\n"
224 " -C <file> Configuration file.\n"
225 " Default: "CONFIGFILE"\n"
226 " -P <file> PID-file.\n"
227 " Default: "PIDFILE"\n"
229 " -f Don't fork to the background.\n"
231 "\nBuiltin defaults:\n"
232 " Config-File "CONFIGFILE"\n"
233 " PID-File "PIDFILE"\n"
234 " Data-Directory "PKGLOCALSTATEDIR"\n"
235 "\n"PACKAGE" "VERSION", http://collectd.org/\n"
236 "by Florian octo Forster <octo@verplant.org>\n"
237 "for contributions see `AUTHORS'\n");
239 } /* static void exit_usage (char *name) */
241 static int do_init (void)
251 ERROR ("sg_init: %s", sg_str_error (sg_get_error ()));
255 if (sg_drop_privileges ())
257 ERROR ("sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
265 } /* int do_init () */
268 static int do_loop (void)
270 struct timeval tv_now;
271 struct timeval tv_next;
272 struct timespec ts_wait;
276 if (gettimeofday (&tv_next, NULL) < 0)
279 ERROR ("gettimeofday failed: %s",
280 sstrerror (errno, errbuf,
284 tv_next.tv_sec += interval_g;
290 /* Issue all plugins */
291 plugin_read_all (&loop);
293 if (gettimeofday (&tv_now, NULL) < 0)
296 ERROR ("gettimeofday failed: %s",
297 sstrerror (errno, errbuf,
302 if (timeval_sub_timespec (&tv_next, &tv_now, &ts_wait) != 0)
304 WARNING ("Not sleeping because "
305 "`timeval_sub_timespec' returned "
310 while ((loop == 0) && (nanosleep (&ts_wait, &ts_wait) == -1))
315 ERROR ("nanosleep failed: %s",
316 sstrerror (errno, errbuf,
321 } /* while (loop == 0) */
323 DEBUG ("return (0);");
327 static int do_shutdown (void)
329 plugin_shutdown_all ();
331 } /* int do_shutdown */
334 static int pidfile_create (void)
337 const char *file = global_option_get ("PIDFile");
339 if ((fh = fopen (file, "w")) == NULL)
342 ERROR ("fopen (%s): %s", file,
343 sstrerror (errno, errbuf, sizeof (errbuf)));
347 fprintf (fh, "%i\n", (int) getpid ());
351 } /* static int pidfile_create (const char *file) */
353 static int pidfile_remove (void)
355 const char *file = global_option_get ("PIDFile");
357 DEBUG ("unlink (%s)", (file != NULL) ? file : "<null>");
358 return (unlink (file));
359 } /* static int pidfile_remove (const char *file) */
360 #endif /* COLLECT_DAEMON */
362 int main (int argc, char **argv)
364 struct sigaction sigIntAction;
365 struct sigaction sigTermAction;
366 char *configfile = CONFIGFILE;
370 struct sigaction sigChldAction;
380 c = getopt (argc, argv, "htC:"
399 global_option_set ("PIDFile", optarg);
404 #endif /* COLLECT_DAEMON */
407 exit_usage (argv[0]);
412 * Read options from the config file, the environment and the command
413 * line (in that order, with later options overwriting previous ones in
415 * Also, this will automatically load modules.
417 if (cf_read (configfile))
419 fprintf (stderr, "Error: Reading the config file failed!\n"
420 "Read the syslog for details.\n");
425 * Change directory. We do this _after_ reading the config and loading
426 * modules to relative paths work as expected.
428 if ((basedir = global_option_get ("BaseDir")) == NULL)
430 fprintf (stderr, "Don't have a basedir to use. This should not happen. Ever.");
433 else if (change_basedir (basedir))
435 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", basedir);
440 * Set global variables or, if that failes, exit. We cannot run with
441 * them being uninitialized. If nothing is configured, then defaults
442 * are being used. So this means that the user has actually done
445 if (init_global_variables () != 0)
455 memset (&sigChldAction, '\0', sizeof (sigChldAction));
456 sigChldAction.sa_handler = SIG_IGN;
457 sigaction (SIGCHLD, &sigChldAction, NULL);
461 if ((pid = fork ()) == -1)
465 fprintf (stderr, "fork: %s",
466 sstrerror (errno, errbuf,
473 /* printf ("Running (PID %i)\n", pid); */
477 /* Detach from session */
481 if (pidfile_create ())
484 /* close standard descriptors */
489 if (open ("/dev/null", O_RDWR) != 0)
491 ERROR ("Error: Could not connect `STDIN' to `/dev/null'");
496 ERROR ("Error: Could not connect `STDOUT' to `/dev/null'");
501 ERROR ("Error: Could not connect `STDERR' to `/dev/null'");
504 } /* if (daemonize) */
505 #endif /* COLLECT_DAEMON */
508 * install signal handlers
510 memset (&sigIntAction, '\0', sizeof (sigIntAction));
511 sigIntAction.sa_handler = sigIntHandler;
512 sigaction (SIGINT, &sigIntAction, NULL);
514 memset (&sigTermAction, '\0', sizeof (sigTermAction));
515 sigTermAction.sa_handler = sigTermHandler;
516 sigaction (SIGTERM, &sigTermAction, NULL);
519 * run the actual loops
525 INFO ("Exiting normally");
532 #endif /* COLLECT_DAEMON */