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>
27 #include "configfile.h"
32 char hostname_g[DATA_MAX_NAME_LEN];
36 #endif /* HAVE_LIBKSTAT */
40 static void sigIntHandler (int signal)
45 static void sigTermHandler (int signal)
50 static int init_global_variables (void)
54 str = global_option_get ("Hostname");
57 strncpy (hostname_g, str, sizeof (hostname_g));
61 if (gethostname (hostname_g, sizeof (hostname_g)) != 0)
63 fprintf (stderr, "`gethostname' failed and no "
64 "hostname was configured.\n");
68 DEBUG ("hostname_g = %s;", hostname_g);
70 str = global_option_get ("Interval");
73 interval_g = atoi (str);
76 fprintf (stderr, "Cannot set the interval to a correct value.\n"
77 "Please check your settings.\n");
80 DEBUG ("interval_g = %i;", interval_g);
83 } /* int init_global_variables */
85 static int change_basedir (const char *orig_dir)
87 char *dir = strdup (orig_dir);
94 ERROR ("strdup failed: %s",
95 sstrerror (errno, errbuf, sizeof (errbuf)));
99 dirlen = strlen (dir);
100 while ((dirlen > 0) && (dir[dirlen - 1] == '/'))
101 dir[--dirlen] = '\0';
106 status = chdir (dir);
113 if (mkdir (orig_dir, 0755) == -1)
116 ERROR ("mkdir (%s): %s", orig_dir,
117 sstrerror (errno, errbuf,
121 else if (chdir (orig_dir) == -1)
124 ERROR ("chdir (%s): %s", orig_dir,
125 sstrerror (errno, errbuf,
133 ERROR ("chdir (%s): %s", orig_dir,
134 sstrerror (errno, errbuf,
141 } /* static int change_basedir (char *dir) */
144 static void update_kstat (void)
148 if ((kc = kstat_open ()) == NULL)
149 ERROR ("Unable to open kstat control structure");
154 kid = kstat_chain_update (kc);
157 INFO ("kstat chain has been updated");
161 ERROR ("kstat chain update failed");
162 /* else: everything works as expected */
166 } /* static void update_kstat (void) */
167 #endif /* HAVE_LIBKSTAT */
170 * Remove all settings but `-f' and `-C'
172 static void exit_usage (char *name)
174 printf ("Usage: "PACKAGE" [OPTIONS]\n\n"
176 "Available options:\n"
178 " -C <file> Configuration file.\n"
179 " Default: "CONFIGFILE"\n"
180 " -P <file> PID-file.\n"
181 " Default: "PIDFILE"\n"
183 " -f Don't fork to the background.\n"
185 "\nBuiltin defaults:\n"
186 " Config-File "CONFIGFILE"\n"
187 " PID-File "PIDFILE"\n"
188 " Data-Directory "PKGLOCALSTATEDIR"\n"
189 "\n"PACKAGE" "VERSION", http://collectd.org/\n"
190 "by Florian octo Forster <octo@verplant.org>\n"
191 "for contributions see `AUTHORS'\n");
193 } /* static void exit_usage (char *name) */
195 static int do_init (void)
205 ERROR ("sg_init: %s", sg_str_error (sg_get_error ()));
209 if (sg_drop_privileges ())
211 ERROR ("sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
219 } /* int do_init () */
222 static int do_loop (void)
224 struct timeval tv_now;
225 struct timeval tv_next;
226 struct timespec ts_wait;
230 if (gettimeofday (&tv_next, NULL) < 0)
233 ERROR ("gettimeofday failed: %s",
234 sstrerror (errno, errbuf,
238 tv_next.tv_sec += interval_g;
244 /* Issue all plugins */
245 plugin_read_all (&loop);
247 if (gettimeofday (&tv_now, NULL) < 0)
250 ERROR ("gettimeofday failed: %s",
251 sstrerror (errno, errbuf,
256 if (timeval_sub_timespec (&tv_next, &tv_now, &ts_wait) != 0)
258 WARNING ("Not sleeping because "
259 "`timeval_sub_timespec' returned "
264 while ((loop == 0) && (nanosleep (&ts_wait, &ts_wait) == -1))
269 ERROR ("nanosleep failed: %s",
270 sstrerror (errno, errbuf,
275 } /* while (loop == 0) */
277 DEBUG ("return (0);");
281 static int do_shutdown (void)
283 plugin_shutdown_all ();
285 } /* int do_shutdown */
288 static int pidfile_create (void)
291 const char *file = global_option_get ("PIDFile");
293 if ((fh = fopen (file, "w")) == NULL)
296 ERROR ("fopen (%s): %s", file,
297 sstrerror (errno, errbuf, sizeof (errbuf)));
301 fprintf (fh, "%i\n", (int) getpid ());
305 } /* static int pidfile_create (const char *file) */
307 static int pidfile_remove (void)
309 const char *file = global_option_get ("PIDFile");
311 DEBUG ("unlink (%s)", (file != NULL) ? file : "<null>");
312 return (unlink (file));
313 } /* static int pidfile_remove (const char *file) */
314 #endif /* COLLECT_DAEMON */
316 int main (int argc, char **argv)
318 struct sigaction sigIntAction;
319 struct sigaction sigTermAction;
320 char *configfile = CONFIGFILE;
323 struct sigaction sigChldAction;
333 c = getopt (argc, argv, "hC:"
349 global_option_set ("PIDFile", optarg);
354 #endif /* COLLECT_DAEMON */
357 exit_usage (argv[0]);
362 * Read options from the config file, the environment and the command
363 * line (in that order, with later options overwriting previous ones in
365 * Also, this will automatically load modules.
367 if (cf_read (configfile))
369 fprintf (stderr, "Error: Reading the config file failed!\n"
370 "Read the syslog for details.\n");
375 * Change directory. We do this _after_ reading the config and loading
376 * modules to relative paths work as expected.
378 if ((basedir = global_option_get ("BaseDir")) == NULL)
380 fprintf (stderr, "Don't have a basedir to use. This should not happen. Ever.");
383 else if (change_basedir (basedir))
385 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", basedir);
390 * Set global variables or, if that failes, exit. We cannot run with
391 * them being uninitialized. If nothing is configured, then defaults
392 * are being used. So this means that the user has actually done
395 if (init_global_variables () != 0)
402 memset (&sigChldAction, '\0', sizeof (sigChldAction));
403 sigChldAction.sa_handler = SIG_IGN;
404 sigaction (SIGCHLD, &sigChldAction, NULL);
408 if ((pid = fork ()) == -1)
412 fprintf (stderr, "fork: %s",
413 sstrerror (errno, errbuf,
420 /* printf ("Running (PID %i)\n", pid); */
424 /* Detach from session */
428 if (pidfile_create ())
431 /* close standard descriptors */
436 if (open ("/dev/null", O_RDWR) != 0)
438 ERROR ("Error: Could not connect `STDIN' to `/dev/null'");
443 ERROR ("Error: Could not connect `STDOUT' to `/dev/null'");
448 ERROR ("Error: Could not connect `STDERR' to `/dev/null'");
451 } /* if (daemonize) */
452 #endif /* COLLECT_DAEMON */
455 * install signal handlers
457 memset (&sigIntAction, '\0', sizeof (sigIntAction));
458 sigIntAction.sa_handler = sigIntHandler;
459 sigaction (SIGINT, &sigIntAction, NULL);
461 memset (&sigTermAction, '\0', sizeof (sigTermAction));
462 sigTermAction.sa_handler = sigTermHandler;
463 sigaction (SIGTERM, &sigTermAction, NULL);
466 * run the actual loops
472 INFO ("Exiting normally");
479 #endif /* COLLECT_DAEMON */