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"
36 #endif /* HAVE_LIBKSTAT */
39 char *pinghosts[MAX_PINGHOSTS];
40 int num_pinghosts = 0;
52 static void sigIntHandler (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", strerror (errno));
76 else if (chdir (dir) == -1)
78 syslog (LOG_ERR, "chdir: %s", 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 */
118 static void exit_usage (char *name)
120 printf ("Usage: "PACKAGE" [OPTIONS]\n\n"
122 "Available options:\n"
124 " -C <file> Configuration file.\n"
125 " Default: "CONFIGFILE"\n"
127 " -P <file> PID file.\n"
128 " Default: "PIDFILE"\n"
130 " -M <dir> Module/Plugin directory.\n"
131 " Default: "PLUGINDIR"\n"
132 " -D <dir> Data storage directory.\n"
133 " Default: "PKGLOCALSTATEDIR"\n"
135 " -L <file> Log file.\n"
136 " Default: "LOGFILE"\n"
139 " -f Don't fork to the background.\n"
142 " -l Start in local mode (no network).\n"
143 " -c Start in client (sender) mode.\n"
144 " -s Start in server (listener) mode.\n"
145 #endif /* HAVE_LIBRRD */
148 " -p <host> Host to ping periodically, may be repeated to ping\n"
149 " more than one host.\n"
150 #endif /* COLLECT_PING */
151 "\n"PACKAGE" "VERSION", http://verplant.org/collectd/\n"
152 "by Florian octo Forster <octo@verplant.org>\n"
153 "for contributions see `AUTHORS'\n");
155 } /* static void exit_usage (char *name) */
157 static int start_client (void)
169 syslog (LOG_ERR, "sg_init: %s", sg_str_error (sg_get_error ()));
173 if (sg_drop_privileges ())
175 syslog (LOG_ERR, "sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
184 curtime = time (NULL);
191 while (sleepingtime != 0)
195 sleepingtime = sleep (sleepingtime);
200 } /* static int start_client (void) */
203 static int start_server (void)
212 if (multicast_receive (&host, &type, &instance, &values) == 0)
213 plugin_write (host, type, instance, values);
215 if (host != NULL) free (host); host = NULL;
216 if (type != NULL) free (type); type = NULL;
217 if (instance != NULL) free (instance); instance = NULL;
218 if (values != NULL) free (values); values = NULL;
222 } /* static int start_server (void) */
223 #endif /* HAVE_LIBRRD */
226 static int pidfile_create (const char *file)
233 if ((fh = fopen (file, "w")) == NULL)
235 syslog (LOG_ERR, "fopen (%s): %s", file, strerror (errno));
239 fprintf (fh, "%d\n", getpid());
243 } /* static int pidfile_create (const char *file) */
244 #endif /* COLLECT_DAEMON */
247 static int pidfile_remove (const char *file)
252 return (unlink (file));
253 } /* static int pidfile_remove (const char *file) */
254 #endif /* COLLECT_DAEMON */
256 int main (int argc, char **argv)
258 struct sigaction sigIntAction, sigChldAction;
259 char *configfile = CONFIGFILE;
260 char *plugindir = PLUGINDIR;
261 char *datadir = PKGLOCALSTATEDIR;
263 char *pidfile = PIDFILE;
268 char *logfile = LOGFILE;
272 operating_mode = MODE_LOCAL;
276 openlog (PACKAGE, LOG_CONS | LOG_PID, LOG_DAEMON);
283 c = getopt (argc, argv, "C:M:D:h"
292 #endif /* HAVE_LIBRRD */
295 #endif /* COLLECT_PING */
305 operating_mode = MODE_CLIENT;
309 operating_mode = MODE_SERVER;
313 operating_mode = MODE_LOCAL;
315 #endif /* HAVE_LIBRRD */
326 #endif /* COLLECT_DAEMON */
340 if (num_pinghosts < MAX_PINGHOSTS)
341 pinghosts[num_pinghosts++] = optarg;
343 fprintf (stderr, "Maximum of %i ping hosts reached.\n", MAX_PINGHOSTS);
345 #endif /* COLLECT_PING */
348 exit_usage (argv[0]);
352 DBG_STARTFILE(logfile, "debug file opened.");
355 * Load plugins and change to output directory
356 * Loading plugins is done first so relative paths work as expected..
358 if (plugin_load_all (plugindir) < 1)
360 fprintf (stderr, "Error: No plugins found.\n");
364 if (change_basedir (datadir))
366 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", datadir);
371 * install signal handlers
373 sigIntAction.sa_handler = sigIntHandler;
374 sigaction (SIGINT, &sigIntAction, NULL);
376 sigChldAction.sa_handler = SIG_IGN;
377 sigaction (SIGCHLD, &sigChldAction, NULL);
385 if ((pid = fork ()) == -1)
388 fprintf (stderr, "fork: %s", strerror (errno));
394 /* printf ("Running (PID %i)\n", pid); */
398 /* Detach from session */
402 if (pidfile_create (pidfile))
405 /* close standard descriptors */
410 if (open ("/dev/null", O_RDWR) != 0)
412 syslog (LOG_ERR, "Error: Could not connect `STDIN' to `/dev/null'");
417 syslog (LOG_ERR, "Error: Could not connect `STDOUT' to `/dev/null'");
422 syslog (LOG_ERR, "Error: Could not connect `STDERR' to `/dev/null'");
425 } /* if (daemonize) */
426 #endif /* COLLECT_DAEMON */
429 * run the actual loops
432 if (operating_mode == MODE_SERVER)
434 else /* if (operating_mode == MODE_CLIENT || operating_mode == MODE_LOCAL) */
438 DBG_STOPFILE("debug file closed.");
441 syslog (LOG_INFO, "Exiting normally");
446 pidfile_remove(pidfile);
447 #endif /* COLLECT_DAEMON */
450 } /* int main (int argc, char **argv) */