2 * collectd - src/collectd.c
3 * Copyright (C) 2005,2006 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 (char *dir)
55 int dirlen = strlen (dir);
57 while ((dirlen > 0) && (dir[dirlen - 1] == '/'))
63 if (chdir (dir) == -1)
67 if (mkdir (dir, 0755) == -1)
69 syslog (LOG_ERR, "mkdir (%s): %s", dir, strerror (errno));
72 else if (chdir (dir) == -1)
74 syslog (LOG_ERR, "chdir (%s): %s", dir, strerror (errno));
80 syslog (LOG_ERR, "chdir: %s", strerror (errno));
86 } /* static int change_basedir (char *dir) */
89 static void update_kstat (void)
93 if ((kc = kstat_open ()) == NULL)
94 syslog (LOG_ERR, "Unable to open kstat control structure");
99 kid = kstat_chain_update (kc);
102 syslog (LOG_INFO, "kstat chain has been updated");
106 syslog (LOG_ERR, "kstat chain update failed");
107 /* else: everything works as expected */
111 } /* static void update_kstat (void) */
112 #endif /* HAVE_LIBKSTAT */
115 * Remove all settings but `-f' and `-C'
117 static void exit_usage (char *name)
119 printf ("Usage: "PACKAGE" [OPTIONS]\n\n"
121 "Available options:\n"
123 " -C <file> Configuration file.\n"
124 " Default: "CONFIGFILE"\n"
125 " -P <file> PID-file.\n"
126 " Default: "PIDFILE"\n"
128 " -f Don't fork to the background.\n"
130 "\nBuiltin defaults:\n"
131 " Config-File "CONFIGFILE"\n"
132 " PID-File "PIDFILE"\n"
133 " Data-Directory "PKGLOCALSTATEDIR"\n"
135 " Log-File "LOGFILE"\n"
137 " Step "COLLECTD_STEP" seconds\n"
138 " Heartbeat "COLLECTD_HEARTBEAT" seconds\n"
139 "\n"PACKAGE" "VERSION", http://collectd.org/\n"
140 "by Florian octo Forster <octo@verplant.org>\n"
141 "for contributions see `AUTHORS'\n");
143 } /* static void exit_usage (char *name) */
145 static int start_client (void)
149 struct timeval tv_now;
150 struct timeval tv_next;
151 struct timespec ts_wait;
153 step = atoi (COLLECTD_STEP);
165 syslog (LOG_ERR, "sg_init: %s", sg_str_error (sg_get_error ()));
169 if (sg_drop_privileges ())
171 syslog (LOG_ERR, "sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
180 if (gettimeofday (&tv_next, NULL) < 0)
182 syslog (LOG_ERR, "gettimeofday failed: %s", strerror (errno));
185 tv_next.tv_sec += step;
190 /* `curtime' is used by many (all?) plugins as the
191 * data-sample-time passed to RRDTool */
192 curtime = time (NULL);
194 /* Issue all plugins */
195 plugin_read_all (&loop);
197 if (gettimeofday (&tv_now, NULL) < 0)
199 syslog (LOG_ERR, "gettimeofday failed: %s", strerror (errno));
203 if (timeval_sub_timespec (&tv_next, &tv_now, &ts_wait) != 0)
205 syslog (LOG_WARNING, "Not sleeping because `timeval_sub_timespec' returned non-zero!");
209 while ((loop == 0) && (nanosleep (&ts_wait, &ts_wait) == -1))
213 syslog (LOG_ERR, "nanosleep failed: %s", strerror (errno));
220 } /* static int start_client (void) */
223 static int start_server (void)
225 /* FIXME use stack here! */
235 if (network_receive (&host, &type, &instance, &values) == 0)
236 plugin_write (host, type, instance, values);
238 if (host != NULL) free (host); host = NULL;
239 if (type != NULL) free (type); type = NULL;
240 if (instance != NULL) free (instance); instance = NULL;
241 if (values != NULL) free (values); values = NULL;
246 } /* static int start_server (void) */
247 #endif /* HAVE_LIBRRD */
250 static int pidfile_create (const char *file)
257 if ((fh = fopen (file, "w")) == NULL)
259 syslog (LOG_ERR, "fopen (%s): %s", file, strerror (errno));
263 fprintf (fh, "%i\n", (int) getpid ());
267 } /* static int pidfile_create (const char *file) */
268 #endif /* COLLECT_DAEMON */
271 static int pidfile_remove (const char *file)
276 return (unlink (file));
277 } /* static int pidfile_remove (const char *file) */
278 #endif /* COLLECT_DAEMON */
280 int main (int argc, char **argv)
282 struct sigaction sigIntAction;
283 struct sigaction sigTermAction;
284 char *datadir = PKGLOCALSTATEDIR;
285 char *configfile = CONFIGFILE;
287 struct sigaction sigChldAction;
288 char *pidfile = NULL;
293 char *logfile = LOGFILE;
297 operating_mode = MODE_LOCAL;
299 operating_mode = MODE_CLIENT;
303 openlog (PACKAGE, LOG_CONS | LOG_PID, LOG_DAEMON);
310 c = getopt (argc, argv, "hC:"
331 #endif /* COLLECT_DAEMON */
334 exit_usage (argv[0]);
339 if ((logfile = cf_get_option ("LogFile", LOGFILE)) != NULL)
340 DBG_STARTFILE (logfile, "Debug file opened.");
344 * Read options from the config file, the environment and the command
345 * line (in that order, with later options overwriting previous ones in
347 * Also, this will automatically load modules.
349 if (cf_read (configfile))
351 fprintf (stderr, "Error: Reading the config file failed!\n"
352 "Read the syslog for details.\n");
357 * Change directory. We do this _after_ reading the config and loading
358 * modules to relative paths work as expected.
360 if ((datadir = cf_get_option ("DataDir", PKGLOCALSTATEDIR)) == NULL)
362 fprintf (stderr, "Don't have a datadir to use. This should not happen. Ever.");
365 if (change_basedir (datadir))
367 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", datadir);
375 sigChldAction.sa_handler = SIG_IGN;
376 sigaction (SIGCHLD, &sigChldAction, NULL);
378 if ((pidfile == NULL)
379 && ((pidfile = cf_get_option ("PIDFile", PIDFILE)) == NULL))
381 fprintf (stderr, "Cannot obtain pidfile. This shoud not happen. Ever.");
387 if ((pid = fork ()) == -1)
390 fprintf (stderr, "fork: %s", strerror (errno));
396 /* printf ("Running (PID %i)\n", pid); */
400 /* Detach from session */
404 if (pidfile_create (pidfile))
407 /* close standard descriptors */
412 if (open ("/dev/null", O_RDWR) != 0)
414 syslog (LOG_ERR, "Error: Could not connect `STDIN' to `/dev/null'");
419 syslog (LOG_ERR, "Error: Could not connect `STDOUT' to `/dev/null'");
424 syslog (LOG_ERR, "Error: Could not connect `STDERR' to `/dev/null'");
427 } /* if (daemonize) */
428 #endif /* COLLECT_DAEMON */
431 * install signal handlers
433 sigIntAction.sa_handler = sigIntHandler;
434 sigaction (SIGINT, &sigIntAction, NULL);
436 sigTermAction.sa_handler = sigTermHandler;
437 sigaction (SIGTERM, &sigTermAction, NULL);
440 * run the actual loops
443 if (operating_mode == MODE_SERVER)
445 else /* if (operating_mode == MODE_CLIENT || operating_mode == MODE_LOCAL || operating_mode == MODE_LOG) */
449 plugin_shutdown_all ();
453 DBG_STOPFILE("debug file closed.");
457 syslog (LOG_INFO, "Exiting normally");
462 pidfile_remove (pidfile);
463 #endif /* COLLECT_DAEMON */
466 } /* int main (int argc, char **argv) */