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>
25 #include "utils_debug.h"
27 #include "multicast.h"
29 #include "configfile.h"
37 #endif /* HAVE_LIBKSTAT */
40 char *pinghosts[MAX_PINGHOSTS];
41 int num_pinghosts = 0;
53 static void sigIntHandler (int signal)
58 static void sigTermHandler (int signal)
63 static int change_basedir (char *dir)
65 int dirlen = strlen (dir);
67 while ((dirlen > 0) && (dir[dirlen - 1] == '/'))
73 if (chdir (dir) == -1)
77 if (mkdir (dir, 0755) == -1)
79 syslog (LOG_ERR, "mkdir (%s): %s", dir, strerror (errno));
82 else if (chdir (dir) == -1)
84 syslog (LOG_ERR, "chdir (%s): %s", dir, strerror (errno));
90 syslog (LOG_ERR, "chdir: %s", strerror (errno));
96 } /* static int change_basedir (char *dir) */
99 static void update_kstat (void)
103 if ((kc = kstat_open ()) == NULL)
104 syslog (LOG_ERR, "Unable to open kstat control structure");
109 kid = kstat_chain_update (kc);
112 syslog (LOG_INFO, "kstat chain has been updated");
116 syslog (LOG_ERR, "kstat chain update failed");
117 /* else: everything works as expected */
121 } /* static void update_kstat (void) */
122 #endif /* HAVE_LIBKSTAT */
124 static void exit_usage (char *name)
126 printf ("Usage: "PACKAGE" [OPTIONS]\n\n"
128 "Available options:\n"
130 " -C <file> Configuration file.\n"
131 " Default: "CONFIGFILE"\n"
133 " -P <file> PID file.\n"
134 " Default: "PIDFILE"\n"
136 " -M <dir> Module/Plugin directory.\n"
137 " Default: "PLUGINDIR"\n"
138 " -D <dir> Data storage directory.\n"
139 " Default: "PKGLOCALSTATEDIR"\n"
141 " -L <file> Log file.\n"
142 " Default: "LOGFILE"\n"
145 " -f Don't fork to the background.\n"
148 " -l Start in local mode (no network).\n"
149 " -c Start in client (sender) mode.\n"
150 " -s Start in server (listener) mode.\n"
151 #endif /* HAVE_LIBRRD */
154 " -p <host> Host to ping periodically, may be repeated to ping\n"
155 " more than one host.\n"
156 #endif /* COLLECT_PING */
157 "\n"PACKAGE" "VERSION", http://verplant.org/collectd/\n"
158 "by Florian octo Forster <octo@verplant.org>\n"
159 "for contributions see `AUTHORS'\n");
161 } /* static void exit_usage (char *name) */
163 static int start_client (void)
175 syslog (LOG_ERR, "sg_init: %s", sg_str_error (sg_get_error ()));
179 if (sg_drop_privileges ())
181 syslog (LOG_ERR, "sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
190 curtime = time (NULL);
197 while (sleepingtime != 0)
201 sleepingtime = sleep (sleepingtime);
206 } /* static int start_client (void) */
209 static int start_server (void)
218 if (multicast_receive (&host, &type, &instance, &values) == 0)
219 plugin_write (host, type, instance, values);
221 if (host != NULL) free (host); host = NULL;
222 if (type != NULL) free (type); type = NULL;
223 if (instance != NULL) free (instance); instance = NULL;
224 if (values != NULL) free (values); values = NULL;
228 } /* static int start_server (void) */
229 #endif /* HAVE_LIBRRD */
232 static int pidfile_create (const char *file)
239 if ((fh = fopen (file, "w")) == NULL)
241 syslog (LOG_ERR, "fopen (%s): %s", file, strerror (errno));
245 fprintf (fh, "%d\n", getpid());
249 } /* static int pidfile_create (const char *file) */
250 #endif /* COLLECT_DAEMON */
253 static int pidfile_remove (const char *file)
258 return (unlink (file));
259 } /* static int pidfile_remove (const char *file) */
260 #endif /* COLLECT_DAEMON */
262 int main (int argc, char **argv)
264 struct sigaction sigIntAction;
265 struct sigaction sigTermAction;
266 struct sigaction sigChldAction;
267 char *configfile = CONFIGFILE;
268 char *plugindir = PLUGINDIR;
269 char *datadir = PKGLOCALSTATEDIR;
271 char *pidfile = PIDFILE;
276 char *logfile = LOGFILE;
280 operating_mode = MODE_LOCAL;
284 openlog (PACKAGE, LOG_CONS | LOG_PID, LOG_DAEMON);
291 c = getopt (argc, argv, "C:M:D:h"
300 #endif /* HAVE_LIBRRD */
303 #endif /* COLLECT_PING */
313 operating_mode = MODE_CLIENT;
317 operating_mode = MODE_SERVER;
321 operating_mode = MODE_LOCAL;
323 #endif /* HAVE_LIBRRD */
334 #endif /* COLLECT_DAEMON */
348 if (num_pinghosts < MAX_PINGHOSTS)
349 pinghosts[num_pinghosts++] = optarg;
351 fprintf (stderr, "Maximum of %i ping hosts reached.\n", MAX_PINGHOSTS);
353 #endif /* COLLECT_PING */
356 exit_usage (argv[0]);
360 DBG_STARTFILE(logfile, "debug file opened.");
363 * Read the config file. This will load any modules automagically.
365 plugin_set_dir (plugindir);
367 if (cf_read (configfile))
369 fprintf (stderr, "Error: Reading the config file failed!\n"
370 "Read the syslog for details.\n");
375 * Change directory. We do this _after_ reading the config and loading
376 * modules to relative paths work as expected.
378 if (change_basedir (datadir))
380 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", datadir);
385 * install signal handlers
387 sigIntAction.sa_handler = sigIntHandler;
388 sigaction (SIGINT, &sigIntAction, NULL);
390 sigIntAction.sa_handler = sigTermHandler;
391 sigaction (SIGTERM, &sigTermAction, NULL);
393 sigChldAction.sa_handler = SIG_IGN;
394 sigaction (SIGCHLD, &sigChldAction, NULL);
402 if ((pid = fork ()) == -1)
405 fprintf (stderr, "fork: %s", strerror (errno));
411 /* printf ("Running (PID %i)\n", pid); */
415 /* Detach from session */
419 if (pidfile_create (pidfile))
422 /* close standard descriptors */
427 if (open ("/dev/null", O_RDWR) != 0)
429 syslog (LOG_ERR, "Error: Could not connect `STDIN' to `/dev/null'");
434 syslog (LOG_ERR, "Error: Could not connect `STDOUT' to `/dev/null'");
439 syslog (LOG_ERR, "Error: Could not connect `STDERR' to `/dev/null'");
442 } /* if (daemonize) */
443 #endif /* COLLECT_DAEMON */
446 * run the actual loops
449 if (operating_mode == MODE_SERVER)
451 else /* if (operating_mode == MODE_CLIENT || operating_mode == MODE_LOCAL) */
455 DBG_STOPFILE("debug file closed.");
458 syslog (LOG_INFO, "Exiting normally");
463 pidfile_remove(pidfile);
464 #endif /* COLLECT_DAEMON */
467 } /* int main (int argc, char **argv) */