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"
28 #include "types_list.h"
31 # include <statgrab.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_global_variables (void)
59 str = global_option_get ("Hostname");
62 strncpy (hostname_g, str, sizeof (hostname_g));
66 if (gethostname (hostname_g, sizeof (hostname_g)) != 0)
68 fprintf (stderr, "`gethostname' failed and no "
69 "hostname was configured.\n");
73 DEBUG ("hostname_g = %s;", hostname_g);
75 str = global_option_get ("Interval");
78 interval_g = atoi (str);
81 fprintf (stderr, "Cannot set the interval to a correct value.\n"
82 "Please check your settings.\n");
85 DEBUG ("interval_g = %i;", interval_g);
88 } /* int init_global_variables */
90 static int change_basedir (const char *orig_dir)
92 char *dir = strdup (orig_dir);
99 ERROR ("strdup failed: %s",
100 sstrerror (errno, errbuf, sizeof (errbuf)));
104 dirlen = strlen (dir);
105 while ((dirlen > 0) && (dir[dirlen - 1] == '/'))
106 dir[--dirlen] = '\0';
111 status = chdir (dir);
118 if (mkdir (orig_dir, 0755) == -1)
121 ERROR ("change_basedir: mkdir (%s): %s", orig_dir,
122 sstrerror (errno, errbuf,
126 else if (chdir (orig_dir) == -1)
129 ERROR ("chdir (%s): %s", orig_dir,
130 sstrerror (errno, errbuf,
138 ERROR ("chdir (%s): %s", orig_dir,
139 sstrerror (errno, errbuf,
146 } /* static int change_basedir (char *dir) */
149 static void update_kstat (void)
153 if ((kc = kstat_open ()) == NULL)
154 ERROR ("Unable to open kstat control structure");
159 kid = kstat_chain_update (kc);
162 INFO ("kstat chain has been updated");
166 ERROR ("kstat chain update failed");
167 /* else: everything works as expected */
171 } /* static void update_kstat (void) */
172 #endif /* HAVE_LIBKSTAT */
175 * Remove all settings but `-f' and `-C'
177 static void exit_usage (char *name)
179 printf ("Usage: "PACKAGE" [OPTIONS]\n\n"
181 "Available options:\n"
183 " -C <file> Configuration file.\n"
184 " Default: "CONFIGFILE"\n"
185 " -P <file> PID-file.\n"
186 " Default: "PIDFILE"\n"
188 " -f Don't fork to the background.\n"
190 "\nBuiltin defaults:\n"
191 " Config-File "CONFIGFILE"\n"
192 " PID-File "PIDFILE"\n"
193 " Data-Directory "PKGLOCALSTATEDIR"\n"
194 "\n"PACKAGE" "VERSION", http://collectd.org/\n"
195 "by Florian octo Forster <octo@verplant.org>\n"
196 "for contributions see `AUTHORS'\n");
198 } /* static void exit_usage (char *name) */
200 static int do_init (void)
210 ERROR ("sg_init: %s", sg_str_error (sg_get_error ()));
214 if (sg_drop_privileges ())
216 ERROR ("sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
225 } /* int do_init () */
228 static int do_loop (void)
230 struct timeval tv_now;
231 struct timeval tv_next;
232 struct timespec ts_wait;
236 if (gettimeofday (&tv_next, NULL) < 0)
239 ERROR ("gettimeofday failed: %s",
240 sstrerror (errno, errbuf,
244 tv_next.tv_sec += interval_g;
250 /* Issue all plugins */
251 plugin_read_all (&loop);
253 if (gettimeofday (&tv_now, NULL) < 0)
256 ERROR ("gettimeofday failed: %s",
257 sstrerror (errno, errbuf,
262 if (timeval_sub_timespec (&tv_next, &tv_now, &ts_wait) != 0)
264 WARNING ("Not sleeping because "
265 "`timeval_sub_timespec' returned "
270 while ((loop == 0) && (nanosleep (&ts_wait, &ts_wait) == -1))
275 ERROR ("nanosleep failed: %s",
276 sstrerror (errno, errbuf,
281 } /* while (loop == 0) */
283 DEBUG ("return (0);");
287 static int do_shutdown (void)
289 plugin_shutdown_all ();
291 } /* int do_shutdown */
294 static int pidfile_create (void)
297 const char *file = global_option_get ("PIDFile");
299 if ((fh = fopen (file, "w")) == NULL)
302 ERROR ("fopen (%s): %s", file,
303 sstrerror (errno, errbuf, sizeof (errbuf)));
307 fprintf (fh, "%i\n", (int) getpid ());
311 } /* static int pidfile_create (const char *file) */
313 static int pidfile_remove (void)
315 const char *file = global_option_get ("PIDFile");
317 DEBUG ("unlink (%s)", (file != NULL) ? file : "<null>");
318 return (unlink (file));
319 } /* static int pidfile_remove (const char *file) */
320 #endif /* COLLECT_DAEMON */
322 int main (int argc, char **argv)
324 struct sigaction sigIntAction;
325 struct sigaction sigTermAction;
326 char *configfile = CONFIGFILE;
330 struct sigaction sigChldAction;
340 c = getopt (argc, argv, "htC:"
359 global_option_set ("PIDFile", optarg);
364 #endif /* COLLECT_DAEMON */
367 exit_usage (argv[0]);
372 * Read options from the config file, the environment and the command
373 * line (in that order, with later options overwriting previous ones in
375 * Also, this will automatically load modules.
377 if (cf_read (configfile))
379 fprintf (stderr, "Error: Reading the config file failed!\n"
380 "Read the syslog for details.\n");
385 * Change directory. We do this _after_ reading the config and loading
386 * modules to relative paths work as expected.
388 if ((basedir = global_option_get ("BaseDir")) == NULL)
390 fprintf (stderr, "Don't have a basedir to use. This should not happen. Ever.");
393 else if (change_basedir (basedir))
395 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", basedir);
400 * Set global variables or, if that failes, exit. We cannot run with
401 * them being uninitialized. If nothing is configured, then defaults
402 * are being used. So this means that the user has actually done
405 if (init_global_variables () != 0)
415 memset (&sigChldAction, '\0', sizeof (sigChldAction));
416 sigChldAction.sa_handler = SIG_IGN;
417 sigaction (SIGCHLD, &sigChldAction, NULL);
421 if ((pid = fork ()) == -1)
425 fprintf (stderr, "fork: %s",
426 sstrerror (errno, errbuf,
433 /* printf ("Running (PID %i)\n", pid); */
437 /* Detach from session */
441 if (pidfile_create ())
444 /* close standard descriptors */
449 if (open ("/dev/null", O_RDWR) != 0)
451 ERROR ("Error: Could not connect `STDIN' to `/dev/null'");
456 ERROR ("Error: Could not connect `STDOUT' to `/dev/null'");
461 ERROR ("Error: Could not connect `STDERR' to `/dev/null'");
464 } /* if (daemonize) */
465 #endif /* COLLECT_DAEMON */
468 * install signal handlers
470 memset (&sigIntAction, '\0', sizeof (sigIntAction));
471 sigIntAction.sa_handler = sigIntHandler;
472 sigaction (SIGINT, &sigIntAction, NULL);
474 memset (&sigTermAction, '\0', sizeof (sigTermAction));
475 sigTermAction.sa_handler = sigTermHandler;
476 sigaction (SIGTERM, &sigTermAction, NULL);
479 * run the actual loops
485 INFO ("Exiting normally");
492 #endif /* COLLECT_DAEMON */