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 */
125 static void exit_usage (char *name)
127 printf ("Usage: "PACKAGE" [OPTIONS]\n\n"
129 "Available options:\n"
131 " -C <file> Configuration file.\n"
132 " Default: "CONFIGFILE"\n"
134 " -P <file> PID file.\n"
135 " Default: "PIDFILE"\n"
137 " -M <dir> Module/Plugin directory.\n"
138 " Default: "PLUGINDIR"\n"
139 " -D <dir> Data storage directory.\n"
140 " Default: "PKGLOCALSTATEDIR"\n"
142 " -L <file> Log file.\n"
143 " Default: "LOGFILE"\n"
146 " -f Don't fork to the background.\n"
149 " -l Start in local mode (no network).\n"
150 " -c Start in client (sender) mode.\n"
151 " -s Start in server (listener) mode.\n"
152 #endif /* HAVE_LIBRRD */
155 " -p <host> Host to ping periodically, may be repeated to ping\n"
156 " more than one host.\n"
157 #endif /* COLLECT_PING */
158 "\n"PACKAGE" "VERSION", http://verplant.org/collectd/\n"
159 "by Florian octo Forster <octo@verplant.org>\n"
160 "for contributions see `AUTHORS'\n");
162 } /* static void exit_usage (char *name) */
164 static int start_client (void)
176 syslog (LOG_ERR, "sg_init: %s", sg_str_error (sg_get_error ()));
180 if (sg_drop_privileges ())
182 syslog (LOG_ERR, "sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
191 curtime = time (NULL);
198 while (sleepingtime != 0)
202 sleepingtime = sleep (sleepingtime);
207 } /* static int start_client (void) */
210 static int start_server (void)
219 if (multicast_receive (&host, &type, &instance, &values) == 0)
220 plugin_write (host, type, instance, values);
222 if (host != NULL) free (host); host = NULL;
223 if (type != NULL) free (type); type = NULL;
224 if (instance != NULL) free (instance); instance = NULL;
225 if (values != NULL) free (values); values = NULL;
229 } /* static int start_server (void) */
230 #endif /* HAVE_LIBRRD */
233 static int pidfile_create (const char *file)
240 if ((fh = fopen (file, "w")) == NULL)
242 syslog (LOG_ERR, "fopen (%s): %s", file, strerror (errno));
246 fprintf (fh, "%d\n", getpid());
250 } /* static int pidfile_create (const char *file) */
251 #endif /* COLLECT_DAEMON */
254 static int pidfile_remove (const char *file)
259 return (unlink (file));
260 } /* static int pidfile_remove (const char *file) */
261 #endif /* COLLECT_DAEMON */
263 int main (int argc, char **argv)
266 struct sigaction sigChldAction;
268 struct sigaction sigIntAction;
269 struct sigaction sigTermAction;
270 char *configfile = CONFIGFILE;
271 char *plugindir = PLUGINDIR;
272 char *datadir = PKGLOCALSTATEDIR;
274 char *pidfile = PIDFILE;
279 char *logfile = LOGFILE;
283 operating_mode = MODE_LOCAL;
287 openlog (PACKAGE, LOG_CONS | LOG_PID, LOG_DAEMON);
294 c = getopt (argc, argv, "C:M:D:h"
303 #endif /* HAVE_LIBRRD */
306 #endif /* COLLECT_PING */
316 operating_mode = MODE_CLIENT;
320 operating_mode = MODE_SERVER;
324 operating_mode = MODE_LOCAL;
326 #endif /* HAVE_LIBRRD */
337 #endif /* COLLECT_DAEMON */
351 if (num_pinghosts < MAX_PINGHOSTS)
352 pinghosts[num_pinghosts++] = optarg;
354 fprintf (stderr, "Maximum of %i ping hosts reached.\n", MAX_PINGHOSTS);
356 #endif /* COLLECT_PING */
359 exit_usage (argv[0]);
363 DBG_STARTFILE(logfile, "debug file opened.");
366 * Read the config file. This will load any modules automagically.
368 plugin_set_dir (plugindir);
370 if (cf_read (configfile))
372 fprintf (stderr, "Error: Reading the config file failed!\n"
373 "Read the syslog for details.\n");
378 * Change directory. We do this _after_ reading the config and loading
379 * modules to relative paths work as expected.
381 if (change_basedir (datadir))
383 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", datadir);
391 sigChldAction.sa_handler = SIG_IGN;
392 sigaction (SIGCHLD, &sigChldAction, NULL);
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 sigIntAction.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) */
458 DBG_STOPFILE("debug file closed.");
461 syslog (LOG_INFO, "Exiting normally");
466 pidfile_remove(pidfile);
467 #endif /* COLLECT_DAEMON */
470 } /* int main (int argc, char **argv) */