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 int change_basedir (char *dir)
60 int dirlen = strlen (dir);
62 while ((dirlen > 0) && (dir[dirlen - 1] == '/'))
68 if (chdir (dir) == -1)
72 if (mkdir (dir, 0755) == -1)
74 syslog (LOG_ERR, "mkdir (%s): %s", dir, strerror (errno));
77 else if (chdir (dir) == -1)
79 syslog (LOG_ERR, "chdir (%s): %s", dir, strerror (errno));
85 syslog (LOG_ERR, "chdir: %s", strerror (errno));
91 } /* static int change_basedir (char *dir) */
94 static void update_kstat (void)
98 if ((kc = kstat_open ()) == NULL)
99 syslog (LOG_ERR, "Unable to open kstat control structure");
104 kid = kstat_chain_update (kc);
107 syslog (LOG_INFO, "kstat chain has been updated");
111 syslog (LOG_ERR, "kstat chain update failed");
112 /* else: everything works as expected */
116 } /* static void update_kstat (void) */
117 #endif /* HAVE_LIBKSTAT */
119 static void exit_usage (char *name)
121 printf ("Usage: "PACKAGE" [OPTIONS]\n\n"
123 "Available options:\n"
125 " -C <file> Configuration file.\n"
126 " Default: "CONFIGFILE"\n"
128 " -P <file> PID file.\n"
129 " Default: "PIDFILE"\n"
131 " -M <dir> Module/Plugin directory.\n"
132 " Default: "PLUGINDIR"\n"
133 " -D <dir> Data storage directory.\n"
134 " Default: "PKGLOCALSTATEDIR"\n"
136 " -L <file> Log file.\n"
137 " Default: "LOGFILE"\n"
140 " -f Don't fork to the background.\n"
143 " -l Start in local mode (no network).\n"
144 " -c Start in client (sender) mode.\n"
145 " -s Start in server (listener) mode.\n"
146 #endif /* HAVE_LIBRRD */
149 " -p <host> Host to ping periodically, may be repeated to ping\n"
150 " more than one host.\n"
151 #endif /* COLLECT_PING */
152 "\n"PACKAGE" "VERSION", http://verplant.org/collectd/\n"
153 "by Florian octo Forster <octo@verplant.org>\n"
154 "for contributions see `AUTHORS'\n");
156 } /* static void exit_usage (char *name) */
158 static int start_client (void)
170 syslog (LOG_ERR, "sg_init: %s", sg_str_error (sg_get_error ()));
174 if (sg_drop_privileges ())
176 syslog (LOG_ERR, "sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
185 curtime = time (NULL);
192 while (sleepingtime != 0)
196 sleepingtime = sleep (sleepingtime);
201 } /* static int start_client (void) */
204 static int start_server (void)
213 if (multicast_receive (&host, &type, &instance, &values) == 0)
214 plugin_write (host, type, instance, values);
216 if (host != NULL) free (host); host = NULL;
217 if (type != NULL) free (type); type = NULL;
218 if (instance != NULL) free (instance); instance = NULL;
219 if (values != NULL) free (values); values = NULL;
223 } /* static int start_server (void) */
224 #endif /* HAVE_LIBRRD */
227 static int pidfile_create (const char *file)
234 if ((fh = fopen (file, "w")) == NULL)
236 syslog (LOG_ERR, "fopen (%s): %s", file, strerror (errno));
240 fprintf (fh, "%d\n", getpid());
244 } /* static int pidfile_create (const char *file) */
245 #endif /* COLLECT_DAEMON */
248 static int pidfile_remove (const char *file)
253 return (unlink (file));
254 } /* static int pidfile_remove (const char *file) */
255 #endif /* COLLECT_DAEMON */
257 int main (int argc, char **argv)
259 struct sigaction sigIntAction, sigChldAction;
260 char *configfile = CONFIGFILE;
261 char *plugindir = PLUGINDIR;
262 char *datadir = PKGLOCALSTATEDIR;
264 char *pidfile = PIDFILE;
269 char *logfile = LOGFILE;
273 operating_mode = MODE_LOCAL;
277 openlog (PACKAGE, LOG_CONS | LOG_PID, LOG_DAEMON);
284 c = getopt (argc, argv, "C:M:D:h"
293 #endif /* HAVE_LIBRRD */
296 #endif /* COLLECT_PING */
306 operating_mode = MODE_CLIENT;
310 operating_mode = MODE_SERVER;
314 operating_mode = MODE_LOCAL;
316 #endif /* HAVE_LIBRRD */
327 #endif /* COLLECT_DAEMON */
341 if (num_pinghosts < MAX_PINGHOSTS)
342 pinghosts[num_pinghosts++] = optarg;
344 fprintf (stderr, "Maximum of %i ping hosts reached.\n", MAX_PINGHOSTS);
346 #endif /* COLLECT_PING */
349 exit_usage (argv[0]);
353 DBG_STARTFILE(logfile, "debug file opened.");
356 * Read the config file. This will load any modules automagically.
358 plugin_set_dir (plugindir);
360 if (cf_read (configfile))
362 fprintf (stderr, "Error: Reading the config file failed!\n"
363 "Read the syslog for details.\n");
368 * Change directory. We do this _after_ reading the config and loading
369 * modules to relative paths work as expected.
371 if (change_basedir (datadir))
373 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", datadir);
378 * install signal handlers
380 sigIntAction.sa_handler = sigIntHandler;
381 sigaction (SIGINT, &sigIntAction, NULL);
383 sigChldAction.sa_handler = SIG_IGN;
384 sigaction (SIGCHLD, &sigChldAction, NULL);
392 if ((pid = fork ()) == -1)
395 fprintf (stderr, "fork: %s", strerror (errno));
401 /* printf ("Running (PID %i)\n", pid); */
405 /* Detach from session */
409 if (pidfile_create (pidfile))
412 /* close standard descriptors */
417 if (open ("/dev/null", O_RDWR) != 0)
419 syslog (LOG_ERR, "Error: Could not connect `STDIN' to `/dev/null'");
424 syslog (LOG_ERR, "Error: Could not connect `STDOUT' to `/dev/null'");
429 syslog (LOG_ERR, "Error: Could not connect `STDERR' to `/dev/null'");
432 } /* if (daemonize) */
433 #endif /* COLLECT_DAEMON */
436 * run the actual loops
439 if (operating_mode == MODE_SERVER)
441 else /* if (operating_mode == MODE_CLIENT || operating_mode == MODE_LOCAL) */
445 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) */