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 void sigIntHandler (int signal)
56 int change_basedir (char *dir)
58 int dirlen = strlen (dir);
60 while ((dirlen > 0) && (dir[dirlen - 1] == '/'))
66 if (chdir (dir) == -1)
70 if (mkdir (dir, 0755) == -1)
72 syslog (LOG_ERR, "mkdir: %s", strerror (errno));
75 else if (chdir (dir) == -1)
77 syslog (LOG_ERR, "chdir: %s", strerror (errno));
83 syslog (LOG_ERR, "chdir: %s", strerror (errno));
92 void update_kstat (void)
96 if ((kc = kstat_open ()) == NULL)
97 syslog (LOG_ERR, "Unable to open kstat control structure");
102 kid = kstat_chain_update (kc);
105 syslog (LOG_INFO, "kstat chain has been updated");
109 syslog (LOG_ERR, "kstat chain update failed");
110 /* else: everything works as expected */
115 #endif /* HAVE_LIBKSTAT */
117 void exit_usage (char *name)
119 printf ("Usage: %s [OPTIONS]\n\n"
121 "Available options:\n"
123 " -C <dir> Configuration directory.\n"
125 " -P <file> PID File.\n"
127 " -M <dir> Module/Plugin directory.\n"
129 " -D <dir> Data storage directory.\n"
131 " -f Don't fork to the background.\n"
133 " -l Start in local mode (no network).\n"
134 " -c Start in client (sender) mode.\n"
135 " -s Start in server (listener) mode.\n"
136 #endif /* HAVE_LIBRRD */
139 " -p <host> Host to ping periodically, may be repeated to ping\n"
140 " more than one host.\n"
141 #endif /* COLLECT_PING */
142 "\n%s %s, http://verplant.org/collectd/\n"
143 "by Florian octo Forster <octo@verplant.org>\n"
144 "for contributions see `AUTHORS'\n",
145 PACKAGE, SYSCONFDIR, PIDFILE, PLUGINDIR, PKGLOCALSTATEDIR, PACKAGE, VERSION);
149 int start_client (void)
158 #ifdef HAVE_LIBSTATGRAB
161 syslog (LOG_ERR, "sg_init: %s", sg_str_error (sg_get_error ()));
165 if (sg_drop_privileges ())
167 syslog (LOG_ERR, "sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
176 curtime = time (NULL);
183 while (sleepingtime != 0)
187 sleepingtime = sleep (sleepingtime);
195 int start_server (void)
204 if (multicast_receive (&host, &type, &instance, &values) == 0)
205 plugin_write (host, type, instance, values);
207 if (host != NULL) free (host); host = NULL;
208 if (type != NULL) free (type); type = NULL;
209 if (instance != NULL) free (instance); instance = NULL;
210 if (values != NULL) free (values); values = NULL;
215 #endif /* HAVE_LIBRRD */
217 int pidfile_create (void)
221 if ((fh = fopen (PIDFILE, "w")) == NULL)
223 syslog (LOG_ERR, "fopen (pidfile): %s", strerror (errno));
227 fprintf (fh, "%d\n", getpid());
233 int pidfile_remove (void)
235 return (unlink (PIDFILE));
238 int main (int argc, char **argv)
240 struct sigaction sigIntAction, sigChldAction;
245 char *confdir = SYSCONFDIR;
246 char *pidfile = PIDFILE;
247 char *plugindir = PLUGINDIR;
248 char *datadir = PKGLOCALSTATEDIR;
253 operating_mode = MODE_LOCAL;
257 openlog (PACKAGE, LOG_CONS | LOG_PID, LOG_DAEMON);
264 c = getopt (argc, argv, "C:P:M:D:fh"
267 #endif /* HAVE_LIBRRD */
270 #endif /* COLLECT_PING */
280 operating_mode = MODE_CLIENT;
284 operating_mode = MODE_SERVER;
288 operating_mode = MODE_LOCAL;
290 #endif /* HAVE_LIBRRD */
308 if (num_pinghosts < MAX_PINGHOSTS)
309 pinghosts[num_pinghosts++] = optarg;
311 fprintf (stderr, "Maximum of %i ping hosts reached.\n", MAX_PINGHOSTS);
313 #endif /* COLLECT_PING */
316 exit_usage (argv[0]);
322 * Load plugins and change to output directory
323 * Loading plugins is done first so relative paths work as expected..
325 if (plugin_load_all (plugindir) < 1)
327 fprintf (stderr, "Error: No plugins found.\n");
331 if (change_basedir (datadir))
333 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", datadir);
338 * install signal handlers
340 sigIntAction.sa_handler = sigIntHandler;
341 sigaction (SIGINT, &sigIntAction, NULL);
343 sigChldAction.sa_handler = SIG_IGN;
344 sigaction (SIGCHLD, &sigChldAction, NULL);
352 if ((pid = fork ()) == -1)
355 fprintf (stderr, "fork: %s", strerror (errno));
361 /* printf ("Running (PID %i)\n", pid); */
365 /* Detach from session */
369 if (pidfile_create ())
372 /* close standard descriptors */
377 if (open ("/dev/null", O_RDWR) != 0)
379 syslog (LOG_ERR, "Error: Could not connect `STDIN' to `/dev/null'");
384 syslog (LOG_ERR, "Error: Could not connect `STDOUT' to `/dev/null'");
389 syslog (LOG_ERR, "Error: Could not connect `STDERR' to `/dev/null'");
392 } /* if (daemonize) */
393 #endif /* COLLECT_DAEMON */
396 * run the actual loops
399 if (operating_mode == MODE_SERVER)
401 else /* if (operating_mode == MODE_CLIENT || operating_mode == MODE_LOCAL) */
406 syslog (LOG_INFO, "Exiting normally");