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"
35 #endif /* HAVE_LIBKSTAT */
42 static void sigIntHandler (int signal)
47 static void sigTermHandler (int signal)
52 static int change_basedir (const char *orig_dir)
54 char *dir = strdup (orig_dir);
60 syslog (LOG_ERR, "strdup failed: %s", strerror (errno));
64 dirlen = strlen (dir);
65 while ((dirlen > 0) && (dir[dirlen - 1] == '/'))
78 if (mkdir (orig_dir, 0755) == -1)
80 syslog (LOG_ERR, "mkdir (%s): %s", orig_dir,
84 else if (chdir (orig_dir) == -1)
86 syslog (LOG_ERR, "chdir (%s): %s", orig_dir,
93 syslog (LOG_ERR, "chdir (%s): %s", orig_dir,
100 } /* static int change_basedir (char *dir) */
103 static void update_kstat (void)
107 if ((kc = kstat_open ()) == NULL)
108 syslog (LOG_ERR, "Unable to open kstat control structure");
113 kid = kstat_chain_update (kc);
116 syslog (LOG_INFO, "kstat chain has been updated");
120 syslog (LOG_ERR, "kstat chain update failed");
121 /* else: everything works as expected */
125 } /* static void update_kstat (void) */
126 #endif /* HAVE_LIBKSTAT */
129 * Remove all settings but `-f' and `-C'
131 static void exit_usage (char *name)
133 printf ("Usage: "PACKAGE" [OPTIONS]\n\n"
135 "Available options:\n"
137 " -C <file> Configuration file.\n"
138 " Default: "CONFIGFILE"\n"
139 " -P <file> PID-file.\n"
140 " Default: "PIDFILE"\n"
142 " -f Don't fork to the background.\n"
144 "\nBuiltin defaults:\n"
145 " Config-File "CONFIGFILE"\n"
146 " PID-File "PIDFILE"\n"
147 " Data-Directory "PKGLOCALSTATEDIR"\n"
149 " Log-File "LOGFILE"\n"
151 " Step "COLLECTD_STEP" seconds\n"
152 " Heartbeat "COLLECTD_HEARTBEAT" seconds\n"
153 "\n"PACKAGE" "VERSION", http://collectd.org/\n"
154 "by Florian octo Forster <octo@verplant.org>\n"
155 "for contributions see `AUTHORS'\n");
157 } /* static void exit_usage (char *name) */
159 static int do_init (void)
169 syslog (LOG_ERR, "sg_init: %s", sg_str_error (sg_get_error ()));
173 if (sg_drop_privileges ())
175 syslog (LOG_ERR, "sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
183 } /* int do_init () */
186 static int do_loop (void)
190 struct timeval tv_now;
191 struct timeval tv_next;
192 struct timespec ts_wait;
194 step = atoi (COLLECTD_STEP);
200 if (gettimeofday (&tv_next, NULL) < 0)
202 syslog (LOG_ERR, "gettimeofday failed: %s", strerror (errno));
205 tv_next.tv_sec += step;
210 /* `curtime' is used by many (all?) plugins as the
211 * data-sample-time passed to RRDTool */
212 curtime = time (NULL);
214 /* Issue all plugins */
215 plugin_read_all (&loop);
217 if (gettimeofday (&tv_now, NULL) < 0)
219 syslog (LOG_ERR, "gettimeofday failed: %s",
224 if (timeval_sub_timespec (&tv_next, &tv_now, &ts_wait) != 0)
226 syslog (LOG_WARNING, "Not sleeping because "
227 "`timeval_sub_timespec' returned "
232 while ((loop == 0) && (nanosleep (&ts_wait, &ts_wait) == -1))
236 syslog (LOG_ERR, "nanosleep failed: %s", strerror (errno));
240 } /* while (loop == 0) */
246 static int do_shutdown (void)
248 plugin_shutdown_all ();
250 } /* int do_shutdown */
253 static int pidfile_create (void)
256 const char *file = global_option_get ("PIDFile");
258 if ((fh = fopen (file, "w")) == NULL)
260 syslog (LOG_ERR, "fopen (%s): %s", file, strerror (errno));
264 fprintf (fh, "%i\n", (int) getpid ());
268 } /* static int pidfile_create (const char *file) */
270 static int pidfile_remove (void)
272 const char *file = global_option_get ("PIDFile");
274 DBG ("unlink (%s)", (file != NULL) ? file : "<null>");
275 return (unlink (file));
276 } /* static int pidfile_remove (const char *file) */
277 #endif /* COLLECT_DAEMON */
279 int main (int argc, char **argv)
281 struct sigaction sigIntAction;
282 struct sigaction sigTermAction;
283 char *configfile = CONFIGFILE;
286 struct sigaction sigChldAction;
295 openlog (PACKAGE, LOG_CONS | LOG_PID, LOG_DAEMON);
302 c = getopt (argc, argv, "hC:"
318 global_option_set ("PIDFile", optarg);
323 #endif /* COLLECT_DAEMON */
326 exit_usage (argv[0]);
331 if ((logfile = global_option_get ("LogFile")) != NULL)
332 DBG_STARTFILE (logfile, "Debug file opened.");
336 * Read options from the config file, the environment and the command
337 * line (in that order, with later options overwriting previous ones in
339 * Also, this will automatically load modules.
341 if (cf_read (configfile))
343 fprintf (stderr, "Error: Reading the config file failed!\n"
344 "Read the syslog for details.\n");
349 * Change directory. We do this _after_ reading the config and loading
350 * modules to relative paths work as expected.
352 if ((datadir = global_option_get ("BaseDir")) == NULL)
354 fprintf (stderr, "Don't have a datadir to use. This should not happen. Ever.");
357 else if (change_basedir (datadir))
359 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", datadir);
367 memset (&sigChldAction, '\0', sizeof (sigChldAction));
368 sigChldAction.sa_handler = SIG_IGN;
369 sigaction (SIGCHLD, &sigChldAction, NULL);
373 if ((pid = fork ()) == -1)
376 fprintf (stderr, "fork: %s", strerror (errno));
382 /* printf ("Running (PID %i)\n", pid); */
386 /* Detach from session */
390 if (pidfile_create ())
393 /* close standard descriptors */
398 if (open ("/dev/null", O_RDWR) != 0)
400 syslog (LOG_ERR, "Error: Could not connect `STDIN' to `/dev/null'");
405 syslog (LOG_ERR, "Error: Could not connect `STDOUT' to `/dev/null'");
410 syslog (LOG_ERR, "Error: Could not connect `STDERR' to `/dev/null'");
413 } /* if (daemonize) */
414 #endif /* COLLECT_DAEMON */
417 * install signal handlers
419 memset (&sigIntAction, '\0', sizeof (sigIntAction));
420 sigIntAction.sa_handler = sigIntHandler;
421 sigaction (SIGINT, &sigIntAction, NULL);
423 memset (&sigTermAction, '\0', sizeof (sigTermAction));
424 sigTermAction.sa_handler = sigTermHandler;
425 sigaction (SIGTERM, &sigTermAction, NULL);
428 * run the actual loops
436 DBG_STOPFILE("debug file closed.");
440 syslog (LOG_INFO, "Exiting normally");
446 #endif /* COLLECT_DAEMON */