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; either version 2 of the License, or (at your
8 * option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at verplant.org>
21 * Alvaro Barcellos <alvaro.barcellos at gmail.com>
26 #include "utils_debug.h"
30 #include "configfile.h"
36 #endif /* HAVE_LIBKSTAT */
44 static void sigIntHandler (int signal)
49 static void sigTermHandler (int signal)
54 static int change_basedir (char *dir)
56 int dirlen = strlen (dir);
58 while ((dirlen > 0) && (dir[dirlen - 1] == '/'))
64 if (chdir (dir) == -1)
68 if (mkdir (dir, 0755) == -1)
70 syslog (LOG_ERR, "mkdir (%s): %s", dir, strerror (errno));
73 else if (chdir (dir) == -1)
75 syslog (LOG_ERR, "chdir (%s): %s", dir, strerror (errno));
81 syslog (LOG_ERR, "chdir: %s", strerror (errno));
87 } /* static int change_basedir (char *dir) */
90 static void update_kstat (void)
94 if ((kc = kstat_open ()) == NULL)
95 syslog (LOG_ERR, "Unable to open kstat control structure");
100 kid = kstat_chain_update (kc);
103 syslog (LOG_INFO, "kstat chain has been updated");
107 syslog (LOG_ERR, "kstat chain update failed");
108 /* else: everything works as expected */
112 } /* static void update_kstat (void) */
113 #endif /* HAVE_LIBKSTAT */
116 * Remove all settings but `-f' and `-C'
118 static void exit_usage (char *name)
120 printf ("Usage: "PACKAGE" [OPTIONS]\n\n"
122 "Available options:\n"
124 " -C <file> Configuration file.\n"
125 " Default: "CONFIGFILE"\n"
126 " -P <file> PID-file.\n"
127 " Default: "PIDFILE"\n"
129 " -f Don't fork to the background.\n"
131 "\nBuiltin defaults:\n"
132 " Config-File "CONFIGFILE"\n"
133 " PID-File "PIDFILE"\n"
134 " Data-Directory "PKGLOCALSTATEDIR"\n"
136 " Log-File "LOGFILE"\n"
138 " Step "COLLECTD_STEP" seconds\n"
139 " Heartbeat "COLLECTD_HEARTBEAT" seconds\n"
140 "\n"PACKAGE" "VERSION", http://collectd.org/\n"
141 "by Florian octo Forster <octo@verplant.org>\n"
142 "for contributions see `AUTHORS'\n");
144 } /* static void exit_usage (char *name) */
146 static int start_client (void)
150 struct timeval tv_now;
151 struct timeval tv_next;
152 struct timespec ts_wait;
154 step = atoi (COLLECTD_STEP);
166 syslog (LOG_ERR, "sg_init: %s", sg_str_error (sg_get_error ()));
170 if (sg_drop_privileges ())
172 syslog (LOG_ERR, "sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
181 if (gettimeofday (&tv_next, NULL) < 0)
183 syslog (LOG_ERR, "gettimeofday failed: %s", strerror (errno));
186 tv_next.tv_sec += step;
191 /* `curtime' is used by many (all?) plugins as the
192 * data-sample-time passed to RRDTool */
193 curtime = time (NULL);
195 /* Issue all plugins */
196 plugin_read_all (&loop);
198 if (gettimeofday (&tv_now, NULL) < 0)
200 syslog (LOG_ERR, "gettimeofday failed: %s", strerror (errno));
204 if (timeval_sub_timespec (&tv_next, &tv_now, &ts_wait) != 0)
206 syslog (LOG_WARNING, "Not sleeping because `timeval_sub_timespec' returned non-zero!");
210 while ((loop == 0) && (nanosleep (&ts_wait, &ts_wait) == -1))
214 syslog (LOG_ERR, "nanosleep failed: %s", strerror (errno));
221 } /* static int start_client (void) */
224 static int start_server (void)
231 int error_counter = 0;
234 while ((loop == 0) && (error_counter < 3))
236 status = network_receive (&host, &type, &instance, &values);
246 plugin_write (host, type, instance, values);
248 if (host != NULL) free (host); host = NULL;
249 if (type != NULL) free (type); type = NULL;
250 if (instance != NULL) free (instance); instance = NULL;
251 if (values != NULL) free (values); values = NULL;
255 } /* static int start_server (void) */
256 #endif /* HAVE_LIBRRD */
259 static int pidfile_create (const char *file)
266 if ((fh = fopen (file, "w")) == NULL)
268 syslog (LOG_ERR, "fopen (%s): %s", file, strerror (errno));
272 fprintf (fh, "%i\n", (int) getpid ());
276 } /* static int pidfile_create (const char *file) */
277 #endif /* COLLECT_DAEMON */
280 static int pidfile_remove (const char *file)
285 return (unlink (file));
286 } /* static int pidfile_remove (const char *file) */
287 #endif /* COLLECT_DAEMON */
289 int main (int argc, char **argv)
291 struct sigaction sigIntAction;
292 struct sigaction sigTermAction;
293 char *datadir = PKGLOCALSTATEDIR;
294 char *configfile = CONFIGFILE;
296 struct sigaction sigChldAction;
297 char *pidfile = NULL;
302 char *logfile = LOGFILE;
306 operating_mode = MODE_LOCAL;
308 operating_mode = MODE_CLIENT;
312 openlog (PACKAGE, LOG_CONS | LOG_PID, LOG_DAEMON);
319 c = getopt (argc, argv, "hC:"
340 #endif /* COLLECT_DAEMON */
343 exit_usage (argv[0]);
348 if ((logfile = cf_get_option ("LogFile", LOGFILE)) != NULL)
349 DBG_STARTFILE (logfile, "Debug file opened.");
353 * Read options from the config file, the environment and the command
354 * line (in that order, with later options overwriting previous ones in
356 * Also, this will automatically load modules.
358 if (cf_read (configfile))
360 fprintf (stderr, "Error: Reading the config file failed!\n"
361 "Read the syslog for details.\n");
366 * Change directory. We do this _after_ reading the config and loading
367 * modules to relative paths work as expected.
369 if ((datadir = cf_get_option ("DataDir", PKGLOCALSTATEDIR)) == NULL)
371 fprintf (stderr, "Don't have a datadir to use. This should not happen. Ever.");
374 if (change_basedir (datadir))
376 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", datadir);
384 sigChldAction.sa_handler = SIG_IGN;
385 sigaction (SIGCHLD, &sigChldAction, NULL);
387 if ((pidfile == NULL)
388 && ((pidfile = cf_get_option ("PIDFile", PIDFILE)) == NULL))
390 fprintf (stderr, "Cannot obtain pidfile. This shoud not happen. Ever.");
396 if ((pid = fork ()) == -1)
399 fprintf (stderr, "fork: %s", strerror (errno));
405 /* printf ("Running (PID %i)\n", pid); */
409 /* Detach from session */
413 if (pidfile_create (pidfile))
416 /* close standard descriptors */
421 if (open ("/dev/null", O_RDWR) != 0)
423 syslog (LOG_ERR, "Error: Could not connect `STDIN' to `/dev/null'");
428 syslog (LOG_ERR, "Error: Could not connect `STDOUT' to `/dev/null'");
433 syslog (LOG_ERR, "Error: Could not connect `STDERR' to `/dev/null'");
436 } /* if (daemonize) */
437 #endif /* COLLECT_DAEMON */
440 * install signal handlers
442 sigIntAction.sa_handler = sigIntHandler;
443 sigaction (SIGINT, &sigIntAction, NULL);
445 sigTermAction.sa_handler = sigTermHandler;
446 sigaction (SIGTERM, &sigTermAction, NULL);
449 * run the actual loops
452 if (operating_mode == MODE_SERVER)
454 else /* if (operating_mode == MODE_CLIENT || operating_mode == MODE_LOCAL || operating_mode == MODE_LOG) */
458 plugin_shutdown_all ();
462 DBG_STOPFILE("debug file closed.");
466 syslog (LOG_INFO, "Exiting normally");
471 pidfile_remove (pidfile);
472 #endif /* COLLECT_DAEMON */
475 } /* int main (int argc, char **argv) */