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"
28 #include "configfile.h"
36 #endif /* HAVE_LIBKSTAT */
39 char *pinghosts[MAX_PINGHOSTS];
40 int num_pinghosts = 0;
52 void sigIntHandler (int signal)
57 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));
93 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 */
116 #endif /* HAVE_LIBKSTAT */
118 void exit_usage (char *name)
120 printf ("Usage: %s [OPTIONS]\n\n"
122 "Available options:\n"
124 " -d <dir> Base directory to use.\n"
126 " -P <dir> Set the plugin-directory\n"
128 " -f Don't fork to the background\n"
130 " -l Start in local mode (no network)\n"
131 " -c Start in client (sender) mode\n"
132 " -s Start in server (listener) mode\n"
133 #endif /* HAVE_LIBRRD */
136 " -p <host> Host to ping periodically, may be repeated to ping\n"
137 " more than one host.\n"
138 #endif /* COLLECT_PING */
139 "\n%s %s, http://verplant.org/collectd/\n"
140 "by Florian octo Forster <octo@verplant.org>\n"
141 "for contributions see `AUTHORS'\n",
142 PACKAGE, DATADIR, PLUGINDIR, PACKAGE, VERSION);
146 int start_client (void)
155 #ifdef HAVE_LIBSTATGRAB
158 syslog (LOG_ERR, "sg_init: %s", sg_str_error (sg_get_error ()));
162 if (sg_drop_privileges ())
164 syslog (LOG_ERR, "sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
173 curtime = time (NULL);
180 while (sleepingtime != 0)
184 sleepingtime = sleep (sleepingtime);
192 int start_server (void)
201 if (multicast_receive (&host, &type, &instance, &values) == 0)
202 plugin_write (host, type, instance, values);
204 if (host != NULL) free (host); host = NULL;
205 if (type != NULL) free (type); type = NULL;
206 if (instance != NULL) free (instance); instance = NULL;
207 if (values != NULL) free (values); values = NULL;
212 #endif /* HAVE_LIBRRD */
214 int pidfile_create (void)
218 if ((fh = fopen (PIDFILE, "w")) == NULL)
220 syslog (LOG_ERR, "fopen (pidfile): %s", strerror (errno));
224 fprintf (fh, "%d\n", getpid());
230 int pidfile_remove (void)
232 return (unlink (PIDFILE));
235 int main (int argc, char **argv)
237 struct sigaction sigIntAction, sigChldAction;
242 char *plugindir = NULL;
243 char *basedir = DATADIR;
244 char *configfile = NULL;
249 operating_mode = MODE_LOCAL;
255 openlog (PACKAGE, LOG_CONS | LOG_PID, LOG_DAEMON);
264 c = getopt (argc, argv, "d:fP:h"
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 */
299 if (num_pinghosts < MAX_PINGHOSTS)
300 pinghosts[num_pinghosts++] = optarg;
302 fprintf (stderr, "Maximum of %i ping hosts reached.\n", MAX_PINGHOSTS);
304 #endif /* COLLECT_PING */
311 exit_usage (argv[0]);
317 * Read the config file. This will load any modules automagically.
319 plugin_set_dir (plugindir);
321 if (cf_read (configfile))
323 fprintf (stderr, "Error: Reading the config file failed!\n"
324 "Read the syslog for details.\n");
329 * Change directory. We do this _after_ reading the config and loading
330 * modules to relative paths work as expected.
332 if (change_basedir (basedir))
334 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", basedir);
339 * install signal handlers
341 sigIntAction.sa_handler = sigIntHandler;
342 sigaction (SIGINT, &sigIntAction, NULL);
344 sigChldAction.sa_handler = SIG_IGN;
345 sigaction (SIGCHLD, &sigChldAction, NULL);
353 if ((pid = fork ()) == -1)
356 fprintf (stderr, "fork: %s", strerror (errno));
362 /* printf ("Running (PID %i)\n", pid); */
366 /* Detach from session */
370 if (pidfile_create ())
373 /* close standard descriptors */
378 if (open ("/dev/null", O_RDWR) != 0)
380 syslog (LOG_ERR, "Error: Could not connect `STDIN' to `/dev/null'");
385 syslog (LOG_ERR, "Error: Could not connect `STDOUT' to `/dev/null'");
390 syslog (LOG_ERR, "Error: Could not connect `STDERR' to `/dev/null'");
393 } /* if (daemonize) */
394 #endif /* COLLECT_DAEMON */
397 * run the actual loops
400 if (operating_mode == MODE_SERVER)
402 else /* if (operating_mode == MODE_CLIENT || operating_mode == MODE_LOCAL) */
409 syslog (LOG_INFO, "Exiting normally");