2 * collectd - src/collectd.c
3 * Copyright (C) 2005 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 */
47 static void sigIntHandler (int signal)
52 static void sigTermHandler (int signal)
57 static int change_basedir (char *dir)
59 int dirlen = strlen (dir);
61 while ((dirlen > 0) && (dir[dirlen - 1] == '/'))
67 if (chdir (dir) == -1)
71 if (mkdir (dir, 0755) == -1)
73 syslog (LOG_ERR, "mkdir (%s): %s", dir, strerror (errno));
76 else if (chdir (dir) == -1)
78 syslog (LOG_ERR, "chdir (%s): %s", dir, strerror (errno));
84 syslog (LOG_ERR, "chdir: %s", strerror (errno));
90 } /* static int change_basedir (char *dir) */
93 static void update_kstat (void)
97 if ((kc = kstat_open ()) == NULL)
98 syslog (LOG_ERR, "Unable to open kstat control structure");
103 kid = kstat_chain_update (kc);
106 syslog (LOG_INFO, "kstat chain has been updated");
110 syslog (LOG_ERR, "kstat chain update failed");
111 /* else: everything works as expected */
115 } /* static void update_kstat (void) */
116 #endif /* HAVE_LIBKSTAT */
119 * Remove all settings but `-f' and `-C'
121 static void exit_usage (char *name)
123 printf ("Usage: "PACKAGE" [OPTIONS]\n\n"
125 "Available options:\n"
127 " -C <file> Configuration file.\n"
128 " Default: "CONFIGFILE"\n"
130 " -f Don't fork to the background.\n"
132 "\nBuiltin defaults:\n"
133 " Config-File "CONFIGFILE"\n"
134 " PID-File "PIDFILE"\n"
135 " Data-Directory "PKGLOCALSTATEDIR"\n"
137 " Log-File "LOGFILE"\n"
139 " Step "COLLECTD_STEP" seconds\n"
140 " Heartbeat "COLLECTD_HEARTBEAT" seconds\n"
141 "\n"PACKAGE" "VERSION", http://verplant.org/collectd/\n"
142 "by Florian octo Forster <octo@verplant.org>\n"
143 "for contributions see `AUTHORS'\n");
145 } /* static void exit_usage (char *name) */
147 static int start_client (void)
151 struct timeval tv_now;
152 struct timeval tv_next;
153 struct timespec ts_wait;
155 step = atoi (COLLECTD_STEP);
167 syslog (LOG_ERR, "sg_init: %s", sg_str_error (sg_get_error ()));
171 if (sg_drop_privileges ())
173 syslog (LOG_ERR, "sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
182 if (gettimeofday (&tv_next, NULL) < 0)
184 syslog (LOG_ERR, "gettimeofday failed: %s", strerror (errno));
187 tv_next.tv_sec += step;
192 /* `curtime' is used by many (all?) plugins as the
193 * data-sample-time passed to RRDTool */
194 curtime = time (NULL);
196 /* Issue all plugins */
199 if (gettimeofday (&tv_now, NULL) < 0)
201 syslog (LOG_ERR, "gettimeofday failed: %s", strerror (errno));
205 if (timeval_sub_timespec (&tv_next, &tv_now, &ts_wait) != 0)
207 syslog (LOG_WARNING, "No sleeping because `timeval_sub_timespec' returned non-zero!");
211 while (nanosleep (&ts_wait, &ts_wait) == -1)
215 syslog (LOG_ERR, "nanosleep failed: %s", strerror (errno));
222 } /* static int start_client (void) */
225 static int start_server (void)
227 /* 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;
245 } /* static int start_server (void) */
246 #endif /* HAVE_LIBRRD */
249 static int pidfile_create (const char *file)
256 if ((fh = fopen (file, "w")) == NULL)
258 syslog (LOG_ERR, "fopen (%s): %s", file, strerror (errno));
262 fprintf (fh, "%d\n", getpid());
266 } /* static int pidfile_create (const char *file) */
267 #endif /* COLLECT_DAEMON */
270 static int pidfile_remove (const char *file)
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 *datadir = PKGLOCALSTATEDIR;
284 char *configfile = CONFIGFILE;
286 struct sigaction sigChldAction;
287 char *pidfile = PIDFILE;
292 char *logfile = LOGFILE;
296 operating_mode = MODE_LOCAL;
300 openlog (PACKAGE, LOG_CONS | LOG_PID, LOG_DAEMON);
307 c = getopt (argc, argv, "hC:"
325 #endif /* COLLECT_DAEMON */
328 exit_usage (argv[0]);
333 if ((logfile = cf_get_option ("LogFile", LOGFILE)) != NULL)
334 DBG_STARTFILE (logfile, "Debug file opened.");
338 * Read options from the config file, the environment and the command
339 * line (in that order, with later options overwriting previous ones in
341 * Also, this will automatically load modules.
343 if (cf_read (configfile))
345 fprintf (stderr, "Error: Reading the config file failed!\n"
346 "Read the syslog for details.\n");
351 * Change directory. We do this _after_ reading the config and loading
352 * modules to relative paths work as expected.
354 if ((datadir = cf_get_option ("DataDir", PKGLOCALSTATEDIR)) == NULL)
356 fprintf (stderr, "Don't have a datadir to use. This should not happen. Ever.");
359 if (change_basedir (datadir))
361 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", datadir);
369 sigChldAction.sa_handler = SIG_IGN;
370 sigaction (SIGCHLD, &sigChldAction, NULL);
372 if ((pidfile = cf_get_option ("PIDFile", PIDFILE)) == NULL)
374 fprintf (stderr, "Cannot obtain pidfile. This shoud not happen. Ever.");
380 if ((pid = fork ()) == -1)
383 fprintf (stderr, "fork: %s", strerror (errno));
389 /* printf ("Running (PID %i)\n", pid); */
393 /* Detach from session */
397 if (pidfile_create (pidfile))
400 /* close standard descriptors */
405 if (open ("/dev/null", O_RDWR) != 0)
407 syslog (LOG_ERR, "Error: Could not connect `STDIN' to `/dev/null'");
412 syslog (LOG_ERR, "Error: Could not connect `STDOUT' to `/dev/null'");
417 syslog (LOG_ERR, "Error: Could not connect `STDERR' to `/dev/null'");
420 } /* if (daemonize) */
421 #endif /* COLLECT_DAEMON */
424 * install signal handlers
426 sigIntAction.sa_handler = sigIntHandler;
427 sigaction (SIGINT, &sigIntAction, NULL);
429 sigTermAction.sa_handler = sigTermHandler;
430 sigaction (SIGTERM, &sigTermAction, NULL);
433 * run the actual loops
436 if (operating_mode == MODE_SERVER)
438 else /* if (operating_mode == MODE_CLIENT || operating_mode == MODE_LOCAL) */
444 DBG_STOPFILE("debug file closed.");
448 syslog (LOG_INFO, "Exiting normally");
453 pidfile_remove (pidfile);
454 #endif /* COLLECT_DAEMON */
457 } /* int main (int argc, char **argv) */