12 #endif /* HAVE_LIBKSTAT */
15 char *pinghosts[MAX_PINGHOSTS];
16 int num_pinghosts = 0;
28 void sigIntHandler (int signal)
33 int change_basedir (char *dir)
35 int dirlen = strlen (dir);
37 while ((dirlen > 0) && (dir[dirlen - 1] == '/'))
43 if (chdir (dir) == -1)
47 if (mkdir (dir, 0755) == -1)
49 syslog (LOG_ERR, "mkdir: %s", strerror (errno));
52 else if (chdir (dir) == -1)
54 syslog (LOG_ERR, "chdir: %s", strerror (errno));
60 syslog (LOG_ERR, "chdir: %s", strerror (errno));
69 void update_kstat (void)
73 if ((kc = kstat_open ()) == NULL)
74 syslog (LOG_ERR, "Unable to open kstat control structure");
79 kid = kstat_chain_update (kc);
82 syslog (LOG_INFO, "kstat chain has been updated");
86 syslog (LOG_ERR, "kstat chain update failed");
87 /* else: everything works as expected */
92 #endif /* HAVE_LIBKSTAT */
94 void exit_usage (char *name)
96 printf ("Usage: %s [OPTIONS]\n\n"
98 "Available options:\n"
100 " -d <dir> Base directory to use.\n"
102 " -P <dir> Set the plugin-directory\n"
104 " -f Don't fork to the background\n"
106 " -l Start in local mode (no network)\n"
107 " -c Start in client (sender) mode\n"
108 " -s Start in server (listener) mode\n"
109 #endif /* HAVE_LIBRRD */
112 " -p <host> Host to ping periodically, may be repeated to ping\n"
113 " more than one host.\n"
114 #endif /* COLLECT_PING */
115 "\n%s %s, http://verplant.org/collectd/\n"
116 "by Florian octo Forster <octo@verplant.org>\n"
117 "for contributions see `AUTHORS'\n",
118 PACKAGE, DATADIR, PLUGINDIR, PACKAGE, VERSION);
122 int start_client (void)
131 #ifdef HAVE_LIBSTATGRAB
134 syslog (LOG_ERR, "sg_init: %s", sg_str_error (sg_get_error ()));
138 if (sg_drop_privileges ())
140 syslog (LOG_ERR, "sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
149 curtime = time (NULL);
156 while (sleepingtime != 0)
160 sleepingtime = sleep (sleepingtime);
168 int start_server (void)
177 if (multicast_receive (&host, &type, &instance, &values) == 0)
178 plugin_write (host, type, instance, values);
180 if (host != NULL) free (host); host = NULL;
181 if (type != NULL) free (type); type = NULL;
182 if (instance != NULL) free (instance); instance = NULL;
183 if (values != NULL) free (values); values = NULL;
188 #endif /* HAVE_LIBRRD */
190 int pidfile_create (void)
194 if ((fh = fopen (PIDFILE, "w")) == NULL)
196 syslog (LOG_ERR, "fopen (pidfile): %s", strerror (errno));
200 fprintf (fh, "%d\n", getpid());
206 int pidfile_remove (void)
208 return (unlink (PIDFILE));
211 int main (int argc, char **argv)
213 struct sigaction sigIntAction, sigChldAction;
216 char *plugindir = NULL;
217 char *basedir = DATADIR;
222 operating_mode = MODE_LOCAL;
228 openlog (PACKAGE, LOG_CONS | LOG_PID, LOG_DAEMON);
237 c = getopt (argc, argv, "d:fP:h"
240 #endif /* HAVE_LIBRRD */
243 #endif /* COLLECT_PING */
253 operating_mode = MODE_CLIENT;
257 operating_mode = MODE_SERVER;
261 operating_mode = MODE_LOCAL;
263 #endif /* HAVE_LIBRRD */
272 if (num_pinghosts < MAX_PINGHOSTS)
273 pinghosts[num_pinghosts++] = optarg;
275 fprintf (stderr, "Maximum of %i ping hosts reached.\n", MAX_PINGHOSTS);
277 #endif /* COLLECT_PING */
284 exit_usage (argv[0]);
290 * Load plugins and change to output directory
291 * Loading plugins is done first so relative paths work as expected..
293 if (plugin_load_all (plugindir) < 1)
295 fprintf (stderr, "Error: No plugins found.\n");
299 if (change_basedir (basedir))
301 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", basedir);
306 * install signal handlers
308 sigIntAction.sa_handler = sigIntHandler;
309 sigaction (SIGINT, &sigIntAction, NULL);
311 sigChldAction.sa_handler = SIG_IGN;
312 sigaction (SIGCHLD, &sigChldAction, NULL);
320 if ((pid = fork ()) == -1)
323 fprintf (stderr, "fork: %s", strerror (errno));
329 /* printf ("Running (PID %i)\n", pid); */
333 /* Detach from session */
337 if (pidfile_create ())
340 /* close standard descriptors */
345 if (open ("/dev/null", O_RDWR) != 0)
347 syslog (LOG_ERR, "Error: Could not connect `STDIN' to `/dev/null'");
352 syslog (LOG_ERR, "Error: Could not connect `STDOUT' to `/dev/null'");
357 syslog (LOG_ERR, "Error: Could not connect `STDERR' to `/dev/null'");
360 } /* if (daemonize) */
364 * run the actual loops
367 if (operating_mode == MODE_SERVER)
369 else /* if (operating_mode == MODE_CLIENT || operating_mode == MODE_LOCAL) */
376 syslog (LOG_INFO, "Exiting normally");