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>
25 #include "utils_debug.h"
29 #include "configfile.h"
34 char hostname_g[DATA_MAX_NAME_LEN];
38 #endif /* HAVE_LIBKSTAT */
42 static void sigIntHandler (int signal)
47 static void sigTermHandler (int signal)
52 static int init_global_variables (void)
56 str = global_option_get ("Hostname");
59 strncpy (hostname_g, str, sizeof (hostname_g));
63 if (gethostname (hostname_g, sizeof (hostname_g)) != 0)
65 fprintf (stderr, "`gethostname' failed and no "
66 "hostname was configured.\n");
70 DBG ("hostname_g = %s;", hostname_g);
72 str = global_option_get ("Interval");
75 interval_g = atoi (str);
78 fprintf (stderr, "Cannot set the interval to a correct value.\n"
79 "Please check your settings.\n");
82 DBG ("interval_g = %i;", interval_g);
85 } /* int init_global_variables */
87 static int change_basedir (const char *orig_dir)
89 char *dir = strdup (orig_dir);
95 syslog (LOG_ERR, "strdup failed: %s", strerror (errno));
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)
115 syslog (LOG_ERR, "mkdir (%s): %s", orig_dir,
119 else if (chdir (orig_dir) == -1)
121 syslog (LOG_ERR, "chdir (%s): %s", orig_dir,
128 syslog (LOG_ERR, "chdir (%s): %s", orig_dir,
135 } /* static int change_basedir (char *dir) */
138 static void update_kstat (void)
142 if ((kc = kstat_open ()) == NULL)
143 syslog (LOG_ERR, "Unable to open kstat control structure");
148 kid = kstat_chain_update (kc);
151 syslog (LOG_INFO, "kstat chain has been updated");
155 syslog (LOG_ERR, "kstat chain update failed");
156 /* else: everything works as expected */
160 } /* static void update_kstat (void) */
161 #endif /* HAVE_LIBKSTAT */
164 * Remove all settings but `-f' and `-C'
166 static void exit_usage (char *name)
168 printf ("Usage: "PACKAGE" [OPTIONS]\n\n"
170 "Available options:\n"
172 " -C <file> Configuration file.\n"
173 " Default: "CONFIGFILE"\n"
174 " -P <file> PID-file.\n"
175 " Default: "PIDFILE"\n"
177 " -f Don't fork to the background.\n"
179 "\nBuiltin defaults:\n"
180 " Config-File "CONFIGFILE"\n"
181 " PID-File "PIDFILE"\n"
182 " Data-Directory "PKGLOCALSTATEDIR"\n"
184 " Log-File "LOGFILE"\n"
186 " Step "COLLECTD_STEP" seconds\n"
187 " Heartbeat "COLLECTD_HEARTBEAT" seconds\n"
188 "\n"PACKAGE" "VERSION", http://collectd.org/\n"
189 "by Florian octo Forster <octo@verplant.org>\n"
190 "for contributions see `AUTHORS'\n");
192 } /* static void exit_usage (char *name) */
194 static int do_init (void)
204 syslog (LOG_ERR, "sg_init: %s", sg_str_error (sg_get_error ()));
208 if (sg_drop_privileges ())
210 syslog (LOG_ERR, "sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
218 } /* int do_init () */
221 static int do_loop (void)
223 struct timeval tv_now;
224 struct timeval tv_next;
225 struct timespec ts_wait;
229 if (gettimeofday (&tv_next, NULL) < 0)
231 syslog (LOG_ERR, "gettimeofday failed: %s", strerror (errno));
234 tv_next.tv_sec += interval_g;
240 /* Issue all plugins */
241 plugin_read_all (&loop);
243 if (gettimeofday (&tv_now, NULL) < 0)
245 syslog (LOG_ERR, "gettimeofday failed: %s",
250 if (timeval_sub_timespec (&tv_next, &tv_now, &ts_wait) != 0)
252 syslog (LOG_WARNING, "Not sleeping because "
253 "`timeval_sub_timespec' returned "
258 while ((loop == 0) && (nanosleep (&ts_wait, &ts_wait) == -1))
262 syslog (LOG_ERR, "nanosleep failed: %s", strerror (errno));
266 } /* while (loop == 0) */
272 static int do_shutdown (void)
274 plugin_shutdown_all ();
276 } /* int do_shutdown */
279 static int pidfile_create (void)
282 const char *file = global_option_get ("PIDFile");
284 if ((fh = fopen (file, "w")) == NULL)
286 syslog (LOG_ERR, "fopen (%s): %s", file, strerror (errno));
290 fprintf (fh, "%i\n", (int) getpid ());
294 } /* static int pidfile_create (const char *file) */
296 static int pidfile_remove (void)
298 const char *file = global_option_get ("PIDFile");
300 DBG ("unlink (%s)", (file != NULL) ? file : "<null>");
301 return (unlink (file));
302 } /* static int pidfile_remove (const char *file) */
303 #endif /* COLLECT_DAEMON */
305 int main (int argc, char **argv)
307 struct sigaction sigIntAction;
308 struct sigaction sigTermAction;
309 char *configfile = CONFIGFILE;
312 struct sigaction sigChldAction;
321 openlog (PACKAGE, LOG_CONS | LOG_PID, LOG_DAEMON);
328 c = getopt (argc, argv, "hC:"
344 global_option_set ("PIDFile", optarg);
349 #endif /* COLLECT_DAEMON */
352 exit_usage (argv[0]);
357 if ((logfile = global_option_get ("LogFile")) != NULL)
358 DBG_STARTFILE (logfile, "Debug file opened.");
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)
411 fprintf (stderr, "fork: %s", strerror (errno));
417 /* printf ("Running (PID %i)\n", pid); */
421 /* Detach from session */
425 if (pidfile_create ())
428 /* close standard descriptors */
433 if (open ("/dev/null", O_RDWR) != 0)
435 syslog (LOG_ERR, "Error: Could not connect `STDIN' to `/dev/null'");
440 syslog (LOG_ERR, "Error: Could not connect `STDOUT' to `/dev/null'");
445 syslog (LOG_ERR, "Error: Could not connect `STDERR' to `/dev/null'");
448 } /* if (daemonize) */
449 #endif /* COLLECT_DAEMON */
452 * install signal handlers
454 memset (&sigIntAction, '\0', sizeof (sigIntAction));
455 sigIntAction.sa_handler = sigIntHandler;
456 sigaction (SIGINT, &sigIntAction, NULL);
458 memset (&sigTermAction, '\0', sizeof (sigTermAction));
459 sigTermAction.sa_handler = sigTermHandler;
460 sigaction (SIGTERM, &sigTermAction, NULL);
463 * run the actual loops
471 DBG_STOPFILE("debug file closed.");
475 syslog (LOG_INFO, "Exiting normally");
481 #endif /* COLLECT_DAEMON */