2 * collectd - src/collectdmon.c
3 * Copyright (C) 2007 Sebastian Harl
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; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Sebastian Harl <sh at tokkee.org>
39 #include <sys/resource.h>
41 #include <sys/types.h>
49 #ifndef COLLECTDMON_PIDFILE
50 # define COLLECTDMON_PIDFILE LOCALSTATEDIR"/run/collectdmon.pid"
51 #endif /* ! COLLECTDMON_PIDFILE */
54 # define WCOREDUMP(s) 0
55 #endif /* ! WCOREDUMP */
58 static int restart = 0;
60 static char *pidfile = NULL;
61 static pid_t collectd_pid = 0;
63 static void exit_usage (char *name)
65 printf ("Usage: %s <options> [-- <collectd options>]\n"
67 "\nAvailable options:\n"
68 " -h Display this help and exit.\n"
69 " -c <path> Path to the collectd binary.\n"
70 " -P <file> PID-file.\n"
72 "\nFor <collectd options> see collectd.conf(5).\n"
74 "\n"PACKAGE" "VERSION", http://collectd.org/\n"
75 "by Florian octo Forster <octo@verplant.org>\n"
76 "for contributions see `AUTHORS'\n", name);
80 static int pidfile_create (void)
85 pidfile = COLLECTDMON_PIDFILE;
87 if (NULL == (file = fopen (pidfile, "w"))) {
88 syslog (LOG_ERR, "Error: couldn't open PID-file (%s) for writing: %s",
89 pidfile, strerror (errno));
93 fprintf (file, "%i\n", (int)getpid ());
96 } /* pidfile_create */
98 static int pidfile_delete (void)
100 assert (NULL != pidfile);
102 if (0 != unlink (pidfile)) {
103 syslog (LOG_ERR, "Error: couldn't delete PID-file (%s): %s",
104 pidfile, strerror (errno));
108 } /* pidfile_remove */
110 static int daemonize (void)
117 if (0 != chdir ("/")) {
118 fprintf (stderr, "Error: chdir() failed: %s\n", strerror (errno));
122 if (0 != getrlimit (RLIMIT_NOFILE, &rl)) {
123 fprintf (stderr, "Error: getrlimit() failed: %s\n", strerror (errno));
127 if (0 > (pid = fork ())) {
128 fprintf (stderr, "Error: fork() failed: %s\n", strerror (errno));
135 if (0 != pidfile_create ())
140 if (RLIM_INFINITY == rl.rlim_max)
143 for (i = 0; i < (int)rl.rlim_max; ++i)
147 if (open ("/dev/null", O_RDWR) != 0) {
148 syslog (LOG_ERR, "Error: couldn't connect STDIN to /dev/null: %s",
155 syslog (LOG_ERR, "Error: couldn't connect STDOUT to /dev/null: %s",
162 syslog (LOG_ERR, "Error: couldn't connect STDERR to /dev/null: %s",
169 static int collectd_start (char **argv)
173 if (0 > (pid = fork ())) {
174 syslog (LOG_ERR, "Error: fork() failed: %s", strerror (errno));
182 execvp (argv[0], argv);
183 syslog (LOG_ERR, "Error: execvp(%s) failed: %s",
184 argv[0], strerror (errno));
186 } /* collectd_start */
188 static int collectd_stop (void)
190 if (0 == collectd_pid)
193 if (0 != kill (collectd_pid, SIGTERM)) {
194 syslog (LOG_ERR, "Error: kill() failed: %s", strerror (errno));
198 } /* collectd_stop */
200 static void sig_int_term_handler (int signo)
204 } /* sig_int_term_handler */
206 static void sig_hup_handler (int signo)
210 } /* sig_hup_handler */
212 static void log_status (int status)
214 if (WIFEXITED (status)) {
215 if (0 == WEXITSTATUS (status))
216 syslog (LOG_INFO, "Info: collectd terminated with exit status %i",
217 WEXITSTATUS (status));
220 "Warning: collectd terminated with exit status %i",
221 WEXITSTATUS (status));
223 else if (WIFSIGNALED (status)) {
224 syslog (LOG_WARNING, "Warning: collectd was terminated by signal %i%s",
225 WTERMSIG (status), WCOREDUMP (status) ? " (core dumped)" : "");
230 static void check_respawn (void)
232 time_t t = time (NULL);
234 static time_t timestamp = 0;
235 static int counter = 0;
237 if ((t - 120) < timestamp)
245 unsigned int time_left = 300;
247 syslog (LOG_ERR, "Error: collectd is respawning too fast - "
248 "disabled for %i seconds", time_left);
250 while ((0 < (time_left = sleep (time_left))) && (0 == loop));
253 } /* check_respawn */
255 int main (int argc, char **argv)
257 int collectd_argc = 0;
258 char *collectd = NULL;
259 char **collectd_argv = NULL;
265 /* parse command line options */
267 int c = getopt (argc, argv, "hc:P:");
281 exit_usage (argv[0]);
285 for (i = optind; i < argc; ++i)
286 if (0 == strcmp (argv[i], "-f"))
289 /* i < argc => -f already present */
290 collectd_argc = 1 + argc - optind + ((i < argc) ? 0 : 1);
291 collectd_argv = (char **)calloc (collectd_argc + 1, sizeof (char *));
293 if (NULL == collectd_argv) {
294 fprintf (stderr, "Out of memory.");
298 collectd_argv[0] = (NULL == collectd) ? "collectd" : collectd;
301 collectd_argv[collectd_argc - 1] = "-f";
303 for (i = optind; i < argc; ++i)
304 collectd_argv[i - optind + 1] = argv[i];
306 collectd_argv[collectd_argc] = NULL;
308 openlog ("collectdmon", LOG_CONS | LOG_PID, LOG_DAEMON);
310 if (-1 == daemonize ())
313 sa.sa_handler = sig_int_term_handler;
315 sigemptyset (&sa.sa_mask);
317 if (0 != sigaction (SIGINT, &sa, NULL)) {
318 syslog (LOG_ERR, "Error: sigaction() failed: %s", strerror (errno));
322 if (0 != sigaction (SIGTERM, &sa, NULL)) {
323 syslog (LOG_ERR, "Error: sigaction() failed: %s", strerror (errno));
327 sa.sa_handler = sig_hup_handler;
329 if (0 != sigaction (SIGHUP, &sa, NULL)) {
330 syslog (LOG_ERR, "Error: sigaction() failed: %s", strerror (errno));
337 if (0 != collectd_start (collectd_argv)) {
338 syslog (LOG_ERR, "Error: failed to start collectd.");
342 assert (0 < collectd_pid);
343 while ((collectd_pid != waitpid (collectd_pid, &status, 0))
345 if ((0 != loop) || (0 != restart))
354 syslog (LOG_INFO, "Info: restarting collectd");
358 syslog (LOG_WARNING, "Warning: restarting collectd");
361 syslog (LOG_INFO, "Info: shutting down collectdmon");
366 free (collectd_argv);
370 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */