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)
232 int error_counter = 0;
235 while ((loop == 0) && (error_counter < 3))
237 status = network_receive (&host, &type, &instance, &values);
247 plugin_write (host, type, instance, values);
249 if (host != NULL) free (host); host = NULL;
250 if (type != NULL) free (type); type = NULL;
251 if (instance != NULL) free (instance); instance = NULL;
252 if (values != NULL) free (values); values = NULL;
257 } /* static int start_server (void) */
258 #endif /* HAVE_LIBRRD */
261 static int pidfile_create (const char *file)
268 if ((fh = fopen (file, "w")) == NULL)
270 syslog (LOG_ERR, "fopen (%s): %s", file, strerror (errno));
274 fprintf (fh, "%i\n", (int) getpid ());
278 } /* static int pidfile_create (const char *file) */
279 #endif /* COLLECT_DAEMON */
282 static int pidfile_remove (const char *file)
287 return (unlink (file));
288 } /* static int pidfile_remove (const char *file) */
289 #endif /* COLLECT_DAEMON */
291 int main (int argc, char **argv)
293 struct sigaction sigIntAction;
294 struct sigaction sigTermAction;
295 char *datadir = PKGLOCALSTATEDIR;
296 char *configfile = CONFIGFILE;
298 struct sigaction sigChldAction;
299 char *pidfile = NULL;
304 char *logfile = LOGFILE;
308 operating_mode = MODE_LOCAL;
310 operating_mode = MODE_CLIENT;
314 openlog (PACKAGE, LOG_CONS | LOG_PID, LOG_DAEMON);
321 c = getopt (argc, argv, "hC:"
342 #endif /* COLLECT_DAEMON */
345 exit_usage (argv[0]);
350 if ((logfile = cf_get_option ("LogFile", LOGFILE)) != NULL)
351 DBG_STARTFILE (logfile, "Debug file opened.");
355 * Read options from the config file, the environment and the command
356 * line (in that order, with later options overwriting previous ones in
358 * Also, this will automatically load modules.
360 if (cf_read (configfile))
362 fprintf (stderr, "Error: Reading the config file failed!\n"
363 "Read the syslog for details.\n");
368 * Change directory. We do this _after_ reading the config and loading
369 * modules to relative paths work as expected.
371 if ((datadir = cf_get_option ("DataDir", PKGLOCALSTATEDIR)) == NULL)
373 fprintf (stderr, "Don't have a datadir to use. This should not happen. Ever.");
376 if (change_basedir (datadir))
378 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", datadir);
386 sigChldAction.sa_handler = SIG_IGN;
387 sigaction (SIGCHLD, &sigChldAction, NULL);
389 if ((pidfile == NULL)
390 && ((pidfile = cf_get_option ("PIDFile", PIDFILE)) == NULL))
392 fprintf (stderr, "Cannot obtain pidfile. This shoud not happen. Ever.");
398 if ((pid = fork ()) == -1)
401 fprintf (stderr, "fork: %s", strerror (errno));
407 /* printf ("Running (PID %i)\n", pid); */
411 /* Detach from session */
415 if (pidfile_create (pidfile))
418 /* close standard descriptors */
423 if (open ("/dev/null", O_RDWR) != 0)
425 syslog (LOG_ERR, "Error: Could not connect `STDIN' to `/dev/null'");
430 syslog (LOG_ERR, "Error: Could not connect `STDOUT' to `/dev/null'");
435 syslog (LOG_ERR, "Error: Could not connect `STDERR' to `/dev/null'");
438 } /* if (daemonize) */
439 #endif /* COLLECT_DAEMON */
442 * install signal handlers
444 sigIntAction.sa_handler = sigIntHandler;
445 sigaction (SIGINT, &sigIntAction, NULL);
447 sigTermAction.sa_handler = sigTermHandler;
448 sigaction (SIGTERM, &sigTermAction, NULL);
451 * run the actual loops
454 if (operating_mode == MODE_SERVER)
456 else /* if (operating_mode == MODE_CLIENT || operating_mode == MODE_LOCAL || operating_mode == MODE_LOG) */
460 plugin_shutdown_all ();
464 DBG_STOPFILE("debug file closed.");
468 syslog (LOG_INFO, "Exiting normally");
473 pidfile_remove (pidfile);
474 #endif /* COLLECT_DAEMON */
477 } /* int main (int argc, char **argv) */