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 "multicast.h"
35 #endif /* HAVE_LIBKSTAT */
38 char *pinghosts[MAX_PINGHOSTS];
39 int num_pinghosts = 0;
51 static void sigIntHandler (int signal)
56 static void sigTermHandler (int signal)
61 static int change_basedir (char *dir)
63 int dirlen = strlen (dir);
65 while ((dirlen > 0) && (dir[dirlen - 1] == '/'))
71 if (chdir (dir) == -1)
75 if (mkdir (dir, 0755) == -1)
77 syslog (LOG_ERR, "mkdir: %s", strerror (errno));
80 else if (chdir (dir) == -1)
82 syslog (LOG_ERR, "chdir: %s", strerror (errno));
88 syslog (LOG_ERR, "chdir: %s", strerror (errno));
97 void update_kstat (void)
101 if ((kc = kstat_open ()) == NULL)
102 syslog (LOG_ERR, "Unable to open kstat control structure");
107 kid = kstat_chain_update (kc);
110 syslog (LOG_INFO, "kstat chain has been updated");
114 syslog (LOG_ERR, "kstat chain update failed");
115 /* else: everything works as expected */
120 #endif /* HAVE_LIBKSTAT */
122 void exit_usage (char *name)
124 printf ("Usage: %s [OPTIONS]\n\n"
126 "Available options:\n"
129 " -C <dir> Configuration file.\n"
132 " -P <file> PID File.\n"
134 " -M <dir> Module/Plugin directory.\n"
136 " -D <dir> Data storage directory.\n"
138 " -f Don't fork to the background.\n"
140 " -l Start in local mode (no network).\n"
141 " -c Start in client (sender) mode.\n"
142 " -s Start in server (listener) mode.\n"
143 #endif /* HAVE_LIBRRD */
146 " -p <host> Host to ping periodically, may be repeated to ping\n"
147 " more than one host.\n"
148 #endif /* COLLECT_PING */
149 "\n%s %s, http://verplant.org/collectd/\n"
150 "by Florian octo Forster <octo@verplant.org>\n"
151 "for contributions see `AUTHORS'\n",
152 PACKAGE, /* CONFIGFILE, */ PIDFILE, PLUGINDIR, PKGLOCALSTATEDIR, PACKAGE, VERSION);
156 int start_client (void)
165 #ifdef HAVE_LIBSTATGRAB
168 syslog (LOG_ERR, "sg_init: %s", sg_str_error (sg_get_error ()));
172 if (sg_drop_privileges ())
174 syslog (LOG_ERR, "sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
183 curtime = time (NULL);
190 while (sleepingtime != 0)
194 sleepingtime = sleep (sleepingtime);
202 int start_server (void)
211 if (multicast_receive (&host, &type, &instance, &values) == 0)
212 plugin_write (host, type, instance, values);
214 if (host != NULL) free (host); host = NULL;
215 if (type != NULL) free (type); type = NULL;
216 if (instance != NULL) free (instance); instance = NULL;
217 if (values != NULL) free (values); values = NULL;
221 } /* static int start_server (void) */
222 #endif /* HAVE_LIBRRD */
225 static int pidfile_create (const char *file)
232 if ((fh = fopen (file, "w")) == NULL)
234 syslog (LOG_ERR, "fopen (%s): %s", file, strerror (errno));
238 fprintf (fh, "%d\n", getpid());
242 } /* static int pidfile_create (const char *file) */
243 #endif /* COLLECT_DAEMON */
246 static int pidfile_remove (const char *file)
251 return (unlink (file));
252 } /* static int pidfile_remove (const char *file) */
253 #endif /* COLLECT_DAEMON */
255 int main (int argc, char **argv)
257 struct sigaction sigIntAction;
258 struct sigaction sigTermAction;
260 struct sigaction sigChldAction;
264 char *configfile = CONFIGFILE;
265 char *pidfile = PIDFILE;
266 char *plugindir = PLUGINDIR;
267 char *datadir = PKGLOCALSTATEDIR;
272 operating_mode = MODE_LOCAL;
276 openlog (PACKAGE, LOG_CONS | LOG_PID, LOG_DAEMON);
283 c = getopt (argc, argv, "C:P:M:D:fh"
286 #endif /* HAVE_LIBRRD */
289 #endif /* COLLECT_PING */
299 operating_mode = MODE_CLIENT;
303 operating_mode = MODE_SERVER;
307 operating_mode = MODE_LOCAL;
309 #endif /* HAVE_LIBRRD */
327 if (num_pinghosts < MAX_PINGHOSTS)
328 pinghosts[num_pinghosts++] = optarg;
330 fprintf (stderr, "Maximum of %i ping hosts reached.\n", MAX_PINGHOSTS);
332 #endif /* COLLECT_PING */
335 exit_usage (argv[0]);
341 * Load plugins and change to output directory
342 * Loading plugins is done first so relative paths work as expected..
344 if (plugin_load_all (plugindir) < 1)
346 fprintf (stderr, "Error: No plugins found.\n");
350 if (change_basedir (datadir))
352 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", datadir);
360 sigChldAction.sa_handler = SIG_IGN;
361 sigaction (SIGCHLD, &sigChldAction, NULL);
365 if ((pid = fork ()) == -1)
368 fprintf (stderr, "fork: %s", strerror (errno));
374 /* printf ("Running (PID %i)\n", pid); */
378 /* Detach from session */
382 if (pidfile_create (pidfile))
385 /* close standard descriptors */
390 if (open ("/dev/null", O_RDWR) != 0)
392 syslog (LOG_ERR, "Error: Could not connect `STDIN' to `/dev/null'");
397 syslog (LOG_ERR, "Error: Could not connect `STDOUT' to `/dev/null'");
402 syslog (LOG_ERR, "Error: Could not connect `STDERR' to `/dev/null'");
405 } /* if (daemonize) */
406 #endif /* COLLECT_DAEMON */
409 * install signal handlers
411 sigIntAction.sa_handler = sigIntHandler;
412 sigaction (SIGINT, &sigIntAction, NULL);
414 sigIntAction.sa_handler = sigTermHandler;
415 sigaction (SIGTERM, &sigTermAction, NULL);
418 * run the actual loops
421 if (operating_mode == MODE_SERVER)
423 else /* if (operating_mode == MODE_CLIENT || operating_mode == MODE_LOCAL) */
428 syslog (LOG_INFO, "Exiting normally");
433 pidfile_remove(pidfile);
434 #endif /* COLLECT_DAEMON */
437 } /* int main (int argc, char **argv) */