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"
28 #include "multicast.h"
30 #include "configfile.h"
38 #endif /* HAVE_LIBKSTAT */
41 char *pinghosts[MAX_PINGHOSTS];
42 int num_pinghosts = 0;
54 static void sigIntHandler (int signal)
59 static void sigTermHandler (int signal)
64 static int change_basedir (char *dir)
66 int dirlen = strlen (dir);
68 while ((dirlen > 0) && (dir[dirlen - 1] == '/'))
74 if (chdir (dir) == -1)
78 if (mkdir (dir, 0755) == -1)
80 syslog (LOG_ERR, "mkdir (%s): %s", dir, strerror (errno));
83 else if (chdir (dir) == -1)
85 syslog (LOG_ERR, "chdir (%s): %s", dir, strerror (errno));
91 syslog (LOG_ERR, "chdir: %s", strerror (errno));
97 } /* static int change_basedir (char *dir) */
100 static void update_kstat (void)
104 if ((kc = kstat_open ()) == NULL)
105 syslog (LOG_ERR, "Unable to open kstat control structure");
110 kid = kstat_chain_update (kc);
113 syslog (LOG_INFO, "kstat chain has been updated");
117 syslog (LOG_ERR, "kstat chain update failed");
118 /* else: everything works as expected */
122 } /* static void update_kstat (void) */
123 #endif /* HAVE_LIBKSTAT */
126 * Remove all settings but `-f' and `-C'
128 static void exit_usage (char *name)
130 printf ("Usage: "PACKAGE" [OPTIONS]\n\n"
132 "Available options:\n"
134 " -C <file> Configuration file.\n"
135 " Default: "CONFIGFILE"\n"
137 " -f Don't fork to the background.\n"
139 "\nBuiltin defaults:\n"
140 " Config-File "CONFIGFILE"\n"
141 " PID-File "PIDFILE"\n"
142 " Data-Directory "PKGLOCALSTATEDIR"\n"
144 " Log-File "LOGFILE"\n"
146 "\n"PACKAGE" "VERSION", http://verplant.org/collectd/\n"
147 "by Florian octo Forster <octo@verplant.org>\n"
148 "for contributions see `AUTHORS'\n");
150 } /* static void exit_usage (char *name) */
152 static int start_client (void)
164 syslog (LOG_ERR, "sg_init: %s", sg_str_error (sg_get_error ()));
168 if (sg_drop_privileges ())
170 syslog (LOG_ERR, "sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
179 curtime = time (NULL);
186 while (sleepingtime != 0)
190 sleepingtime = sleep (sleepingtime);
195 } /* static int start_client (void) */
198 static int start_server (void)
207 if (multicast_receive (&host, &type, &instance, &values) == 0)
208 plugin_write (host, type, instance, values);
210 if (host != NULL) free (host); host = NULL;
211 if (type != NULL) free (type); type = NULL;
212 if (instance != NULL) free (instance); instance = NULL;
213 if (values != NULL) free (values); values = NULL;
217 } /* static int start_server (void) */
218 #endif /* HAVE_LIBRRD */
221 static int pidfile_create (const char *file)
228 if ((fh = fopen (file, "w")) == NULL)
230 syslog (LOG_ERR, "fopen (%s): %s", file, strerror (errno));
234 fprintf (fh, "%d\n", getpid());
238 } /* static int pidfile_create (const char *file) */
239 #endif /* COLLECT_DAEMON */
242 static int pidfile_remove (const char *file)
247 return (unlink (file));
248 } /* static int pidfile_remove (const char *file) */
249 #endif /* COLLECT_DAEMON */
251 int main (int argc, char **argv)
253 struct sigaction sigIntAction;
254 struct sigaction sigTermAction;
255 char *datadir = PKGLOCALSTATEDIR;
256 char *configfile = CONFIGFILE;
258 struct sigaction sigChldAction;
259 char *pidfile = PIDFILE;
264 char *logfile = LOGFILE;
268 operating_mode = MODE_LOCAL;
272 openlog (PACKAGE, LOG_CONS | LOG_PID, LOG_DAEMON);
279 c = getopt (argc, argv, "hC:"
297 #endif /* COLLECT_DAEMON */
300 exit_usage (argv[0]);
305 if ((logfile = cf_get_option ("LogFile", LOGFILE)) != NULL)
306 DBG_STARTFILE (logfile, "Debug file opened.");
310 * Read options from the config file, the environment and the command
311 * line (in that order, with later options overwriting previous ones in
313 * Also, this will automatically load modules.
315 if (cf_read (configfile))
317 fprintf (stderr, "Error: Reading the config file failed!\n"
318 "Read the syslog for details.\n");
323 * Change directory. We do this _after_ reading the config and loading
324 * modules to relative paths work as expected.
326 if ((datadir = cf_get_option ("DataDir", PKGLOCALSTATEDIR)) == NULL)
328 fprintf (stderr, "Don't have a datadir to use. This should not happen. Ever.");
331 if (change_basedir (datadir))
333 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", datadir);
341 sigChldAction.sa_handler = SIG_IGN;
342 sigaction (SIGCHLD, &sigChldAction, NULL);
344 if ((pidfile = cf_get_option ("PIDFile", PIDFILE)) == NULL)
346 fprintf (stderr, "Cannot obtain pidfile. This shoud not happen. Ever.");
352 if ((pid = fork ()) == -1)
355 fprintf (stderr, "fork: %s", strerror (errno));
361 /* printf ("Running (PID %i)\n", pid); */
365 /* Detach from session */
369 if (pidfile_create (pidfile))
372 /* close standard descriptors */
377 if (open ("/dev/null", O_RDWR) != 0)
379 syslog (LOG_ERR, "Error: Could not connect `STDIN' to `/dev/null'");
384 syslog (LOG_ERR, "Error: Could not connect `STDOUT' to `/dev/null'");
389 syslog (LOG_ERR, "Error: Could not connect `STDERR' to `/dev/null'");
392 } /* if (daemonize) */
393 #endif /* COLLECT_DAEMON */
396 * install signal handlers
398 sigIntAction.sa_handler = sigIntHandler;
399 sigaction (SIGINT, &sigIntAction, NULL);
401 sigTermAction.sa_handler = sigTermHandler;
402 sigaction (SIGTERM, &sigTermAction, NULL);
405 * run the actual loops
408 if (operating_mode == MODE_SERVER)
410 else /* if (operating_mode == MODE_CLIENT || operating_mode == MODE_LOCAL) */
416 DBG_STOPFILE("debug file closed.");
420 syslog (LOG_INFO, "Exiting normally");
425 pidfile_remove (pidfile);
426 #endif /* COLLECT_DAEMON */
429 } /* int main (int argc, char **argv) */