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 */
43 static void sigIntHandler (int signal)
48 static void sigTermHandler (int signal)
53 static int change_basedir (const char *orig_dir)
55 char *dir = strdup (orig_dir);
61 syslog (LOG_ERR, "strdup failed: %s", strerror (errno));
65 dirlen = strlen (dir);
66 while ((dirlen > 0) && (dir[dirlen - 1] == '/'))
79 if (mkdir (orig_dir, 0755) == -1)
81 syslog (LOG_ERR, "mkdir (%s): %s", orig_dir,
85 else if (chdir (orig_dir) == -1)
87 syslog (LOG_ERR, "chdir (%s): %s", orig_dir,
94 syslog (LOG_ERR, "chdir (%s): %s", orig_dir,
101 } /* static int change_basedir (char *dir) */
104 static void update_kstat (void)
108 if ((kc = kstat_open ()) == NULL)
109 syslog (LOG_ERR, "Unable to open kstat control structure");
114 kid = kstat_chain_update (kc);
117 syslog (LOG_INFO, "kstat chain has been updated");
121 syslog (LOG_ERR, "kstat chain update failed");
122 /* else: everything works as expected */
126 } /* static void update_kstat (void) */
127 #endif /* HAVE_LIBKSTAT */
130 * Remove all settings but `-f' and `-C'
132 static void exit_usage (char *name)
134 printf ("Usage: "PACKAGE" [OPTIONS]\n\n"
136 "Available options:\n"
138 " -C <file> Configuration file.\n"
139 " Default: "CONFIGFILE"\n"
140 " -P <file> PID-file.\n"
141 " Default: "PIDFILE"\n"
143 " -f Don't fork to the background.\n"
145 "\nBuiltin defaults:\n"
146 " Config-File "CONFIGFILE"\n"
147 " PID-File "PIDFILE"\n"
148 " Data-Directory "PKGLOCALSTATEDIR"\n"
150 " Log-File "LOGFILE"\n"
152 " Step "COLLECTD_STEP" seconds\n"
153 " Heartbeat "COLLECTD_HEARTBEAT" seconds\n"
154 "\n"PACKAGE" "VERSION", http://collectd.org/\n"
155 "by Florian octo Forster <octo@verplant.org>\n"
156 "for contributions see `AUTHORS'\n");
158 } /* static void exit_usage (char *name) */
160 static int start_client (void)
164 struct timeval tv_now;
165 struct timeval tv_next;
166 struct timespec ts_wait;
168 step = atoi (COLLECTD_STEP);
180 syslog (LOG_ERR, "sg_init: %s", sg_str_error (sg_get_error ()));
184 if (sg_drop_privileges ())
186 syslog (LOG_ERR, "sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
195 if (gettimeofday (&tv_next, NULL) < 0)
197 syslog (LOG_ERR, "gettimeofday failed: %s", strerror (errno));
200 tv_next.tv_sec += step;
205 /* `curtime' is used by many (all?) plugins as the
206 * data-sample-time passed to RRDTool */
207 curtime = time (NULL);
209 /* Issue all plugins */
210 plugin_read_all (&loop);
212 if (gettimeofday (&tv_now, NULL) < 0)
214 syslog (LOG_ERR, "gettimeofday failed: %s", strerror (errno));
218 if (timeval_sub_timespec (&tv_next, &tv_now, &ts_wait) != 0)
220 syslog (LOG_WARNING, "Not sleeping because `timeval_sub_timespec' returned non-zero!");
224 while ((loop == 0) && (nanosleep (&ts_wait, &ts_wait) == -1))
228 syslog (LOG_ERR, "nanosleep failed: %s", strerror (errno));
235 } /* static int start_client (void) */
238 static int start_server (void)
247 int error_counter = 0;
250 while ((loop == 0) && (error_counter < 3))
252 status = network_receive (&host, &type, &instance, &values);
262 plugin_write (host, type, instance, values);
264 if (host != NULL) free (host); host = NULL;
265 if (type != NULL) free (type); type = NULL;
266 if (instance != NULL) free (instance); instance = NULL;
267 if (values != NULL) free (values); values = NULL;
272 } /* static int start_server (void) */
273 #endif /* HAVE_LIBRRD */
276 static int pidfile_create (void)
279 const char *file = global_option_get ("PIDFile");
281 if ((fh = fopen (file, "w")) == NULL)
283 syslog (LOG_ERR, "fopen (%s): %s", file, strerror (errno));
287 fprintf (fh, "%i\n", (int) getpid ());
291 } /* static int pidfile_create (const char *file) */
293 static int pidfile_remove (void)
295 const char *file = global_option_get ("PIDFile");
297 return (unlink (file));
298 } /* static int pidfile_remove (const char *file) */
299 #endif /* COLLECT_DAEMON */
301 int main (int argc, char **argv)
303 struct sigaction sigIntAction;
304 struct sigaction sigTermAction;
305 char *configfile = CONFIGFILE;
308 struct sigaction sigChldAction;
317 operating_mode = MODE_LOCAL;
319 operating_mode = MODE_CLIENT;
323 openlog (PACKAGE, LOG_CONS | LOG_PID, LOG_DAEMON);
330 c = getopt (argc, argv, "hC:"
346 global_option_set ("PIDFile", optarg);
351 #endif /* COLLECT_DAEMON */
354 exit_usage (argv[0]);
359 if ((logfile = global_option_get ("LogFile")) != NULL)
360 DBG_STARTFILE (logfile, "Debug file opened.");
364 * Read options from the config file, the environment and the command
365 * line (in that order, with later options overwriting previous ones in
367 * Also, this will automatically load modules.
369 if (cf_read (configfile))
371 fprintf (stderr, "Error: Reading the config file failed!\n"
372 "Read the syslog for details.\n");
377 * Change directory. We do this _after_ reading the config and loading
378 * modules to relative paths work as expected.
380 if ((datadir = global_option_get ("BaseDir")) != NULL)
382 fprintf (stderr, "Don't have a datadir to use. This should not happen. Ever.");
385 if (change_basedir (datadir))
387 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", datadir);
395 sigChldAction.sa_handler = SIG_IGN;
396 sigaction (SIGCHLD, &sigChldAction, NULL);
400 if ((pid = fork ()) == -1)
403 fprintf (stderr, "fork: %s", strerror (errno));
409 /* printf ("Running (PID %i)\n", pid); */
413 /* Detach from session */
417 if (pidfile_create ())
420 /* close standard descriptors */
425 if (open ("/dev/null", O_RDWR) != 0)
427 syslog (LOG_ERR, "Error: Could not connect `STDIN' to `/dev/null'");
432 syslog (LOG_ERR, "Error: Could not connect `STDOUT' to `/dev/null'");
437 syslog (LOG_ERR, "Error: Could not connect `STDERR' to `/dev/null'");
440 } /* if (daemonize) */
441 #endif /* COLLECT_DAEMON */
444 * install signal handlers
446 sigIntAction.sa_handler = sigIntHandler;
447 sigaction (SIGINT, &sigIntAction, NULL);
449 sigTermAction.sa_handler = sigTermHandler;
450 sigaction (SIGTERM, &sigTermAction, NULL);
453 * run the actual loops
456 if (operating_mode == MODE_SERVER)
458 else /* if (operating_mode == MODE_CLIENT || operating_mode == MODE_LOCAL || operating_mode == MODE_LOG) */
462 plugin_shutdown_all ();
466 DBG_STOPFILE("debug file closed.");
470 syslog (LOG_INFO, "Exiting normally");
476 #endif /* COLLECT_DAEMON */
479 } /* int main (int argc, char **argv) */