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;
53 sigIntHandler (int signal)
59 change_basedir (char *dir)
61 int dirlen = strlen (dir);
63 while ((dirlen > 0) && (dir[dirlen - 1] == '/'))
69 if (chdir (dir) == -1)
73 if (mkdir (dir, 0755) == -1)
75 syslog (LOG_ERR, "mkdir: %s", strerror (errno));
78 else if (chdir (dir) == -1)
80 syslog (LOG_ERR, "chdir: %s", strerror (errno));
86 syslog (LOG_ERR, "chdir: %s", strerror (errno));
92 } /* static int change_basedir (char *dir) */
100 if ((kc = kstat_open ()) == NULL)
101 syslog (LOG_ERR, "Unable to open kstat control structure");
106 kid = kstat_chain_update (kc);
109 syslog (LOG_INFO, "kstat chain has been updated");
113 syslog (LOG_ERR, "kstat chain update failed");
114 /* else: everything works as expected */
118 } /* static void update_kstat (void) */
119 #endif /* HAVE_LIBKSTAT */
122 exit_usage (char *name)
124 printf ("Usage: "PACKAGE" [OPTIONS]\n\n"
126 "Available options:\n"
128 " -C <file> Configuration file.\n"
129 " Default: "CONFIGFILE"\n"
130 /* sure you want a configFILE?
131 what about a configDIR? - niki */
133 " -C <dir> Configuration directory.\n"
134 " Default: "CONFIGDIR"\n"
137 " -P <file> PID file.\n"
138 " Default: "PIDFILE"\n"
140 " -M <dir> Module/Plugin directory.\n"
141 " Default: "PLUGINDIR"\n"
142 " -D <dir> Data storage directory.\n"
143 " Default: "PKGLOCALSTATEDIR"\n"
145 " -L <file> Log file.\n"
146 " Default: "LOGFILE"\n"
149 " -f Don't fork to the background.\n"
152 " -l Start in local mode (no network).\n"
153 " -c Start in client (sender) mode.\n"
154 " -s Start in server (listener) mode.\n"
155 #endif /* HAVE_LIBRRD */
158 " -p <host> Host to ping periodically, may be repeated to ping\n"
159 " more than one host.\n"
160 #endif /* COLLECT_PING */
161 "\n"PACKAGE" "VERSION", http://verplant.org/collectd/\n"
162 "by Florian octo Forster <octo@verplant.org>\n"
163 "for contributions see `AUTHORS'\n");
165 } /* static void exit_usage (char *name) */
177 #ifdef HAVE_LIBSTATGRAB
180 syslog (LOG_ERR, "sg_init: %s", sg_str_error (sg_get_error ()));
184 if (sg_drop_privileges ())
186 syslog (LOG_ERR, "sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
195 curtime = time (NULL);
202 while (sleepingtime != 0)
206 sleepingtime = sleep (sleepingtime);
211 } /* static int start_client (void) */
224 if (multicast_receive (&host, &type, &instance, &values) == 0)
225 plugin_write (host, type, instance, values);
227 if (host != NULL) free (host); host = NULL;
228 if (type != NULL) free (type); type = NULL;
229 if (instance != NULL) free (instance); instance = NULL;
230 if (values != NULL) free (values); values = NULL;
234 } /* static int start_server (void) */
235 #endif /* HAVE_LIBRRD */
239 pidfile_create (const char *file)
246 if ((fh = fopen (file, "w")) == NULL)
248 syslog (LOG_ERR, "fopen (%s): %s", file, strerror (errno));
252 fprintf (fh, "%d\n", getpid());
256 } /* static int pidfile_create (const char *file) */
257 #endif /* COLLECT_DAEMON */
261 pidfile_remove (const char *file)
266 return (unlink (file));
267 } /* static int pidfile_remove (const char *file) */
268 #endif /* COLLECT_DAEMON */
271 main (int argc, char **argv)
273 struct sigaction sigIntAction, sigChldAction;
274 char *configfile = CONFIGFILE;
275 /* or char *configdir = CONFIGDIR; */
276 char *plugindir = PLUGINDIR;
277 char *datadir = PKGLOCALSTATEDIR;
279 char *pidfile = PIDFILE;
284 char *logfile = LOGFILE;
288 operating_mode = MODE_LOCAL;
292 openlog (PACKAGE, LOG_CONS | LOG_PID, LOG_DAEMON);
299 c = getopt (argc, argv, "C:M:D:h"
308 #endif /* HAVE_LIBRRD */
311 #endif /* COLLECT_PING */
321 operating_mode = MODE_CLIENT;
325 operating_mode = MODE_SERVER;
329 operating_mode = MODE_LOCAL;
331 #endif /* HAVE_LIBRRD */
334 /* configdir = optarg; */
343 #endif /* COLLECT_DAEMON */
357 if (num_pinghosts < MAX_PINGHOSTS)
358 pinghosts[num_pinghosts++] = optarg;
360 fprintf (stderr, "Maximum of %i ping hosts reached.\n", MAX_PINGHOSTS);
362 #endif /* COLLECT_PING */
365 exit_usage (argv[0]);
369 DBG_STARTFILE(logfile, "debug file opened.");
372 * Load plugins and change to output directory
373 * Loading plugins is done first so relative paths work as expected..
375 if (plugin_load_all (plugindir) < 1)
377 fprintf (stderr, "Error: No plugins found.\n");
381 if (change_basedir (datadir))
383 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", datadir);
388 * install signal handlers
390 sigIntAction.sa_handler = sigIntHandler;
391 sigaction (SIGINT, &sigIntAction, NULL);
393 sigChldAction.sa_handler = SIG_IGN;
394 sigaction (SIGCHLD, &sigChldAction, NULL);
402 if ((pid = fork ()) == -1)
405 fprintf (stderr, "fork: %s", strerror (errno));
411 /* printf ("Running (PID %i)\n", pid); */
415 /* Detach from session */
419 if (pidfile_create (pidfile))
422 /* close standard descriptors */
427 if (open ("/dev/null", O_RDWR) != 0)
429 syslog (LOG_ERR, "Error: Could not connect `STDIN' to `/dev/null'");
434 syslog (LOG_ERR, "Error: Could not connect `STDOUT' to `/dev/null'");
439 syslog (LOG_ERR, "Error: Could not connect `STDERR' to `/dev/null'");
442 } /* if (daemonize) */
443 #endif /* COLLECT_DAEMON */
446 * run the actual loops
449 if (operating_mode == MODE_SERVER)
451 else /* if (operating_mode == MODE_CLIENT || operating_mode == MODE_LOCAL) */
455 DBG_STOPFILE("debug file closed.");
458 syslog (LOG_INFO, "Exiting normally");
463 pidfile_remove(pidfile);
464 #endif /* COLLECT_DAEMON */
467 } /* int main (int argc, char **argv) */