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>
22 #if !defined(__GNUC__) || !__GNUC__
23 # define __attribute__(x) /**/
43 #include <sys/resource.h>
45 #include <sys/types.h>
53 #ifndef COLLECTDMON_PIDFILE
54 # define COLLECTDMON_PIDFILE LOCALSTATEDIR"/run/collectdmon.pid"
55 #endif /* ! COLLECTDMON_PIDFILE */
58 # define WCOREDUMP(s) 0
59 #endif /* ! WCOREDUMP */
62 static int restart = 0;
64 static char *pidfile = NULL;
65 static pid_t collectd_pid = 0;
67 static void exit_usage (char *name)
69 printf ("Usage: %s <options> [-- <collectd options>]\n"
71 "\nAvailable options:\n"
72 " -h Display this help and exit.\n"
73 " -c <path> Path to the collectd binary.\n"
74 " -P <file> PID-file.\n"
76 "\nFor <collectd options> see collectd.conf(5).\n"
78 "\n"PACKAGE" "VERSION", http://collectd.org/\n"
79 "by Florian octo Forster <octo@verplant.org>\n"
80 "for contributions see `AUTHORS'\n", name);
84 static int pidfile_create (void)
89 pidfile = COLLECTDMON_PIDFILE;
91 if (NULL == (file = fopen (pidfile, "w"))) {
92 syslog (LOG_ERR, "Error: couldn't open PID-file (%s) for writing: %s",
93 pidfile, strerror (errno));
97 fprintf (file, "%i\n", (int)getpid ());
100 } /* pidfile_create */
102 static int pidfile_delete (void)
104 assert (NULL != pidfile);
106 if (0 != unlink (pidfile)) {
107 syslog (LOG_ERR, "Error: couldn't delete PID-file (%s): %s",
108 pidfile, strerror (errno));
112 } /* pidfile_remove */
114 static int daemonize (void)
121 if (0 != chdir ("/")) {
122 fprintf (stderr, "Error: chdir() failed: %s\n", strerror (errno));
126 if (0 != getrlimit (RLIMIT_NOFILE, &rl)) {
127 fprintf (stderr, "Error: getrlimit() failed: %s\n", strerror (errno));
131 if (0 > (pid = fork ())) {
132 fprintf (stderr, "Error: fork() failed: %s\n", strerror (errno));
139 if (0 != pidfile_create ())
144 if (RLIM_INFINITY == rl.rlim_max)
147 for (i = 0; i < (int)rl.rlim_max; ++i)
151 if (open ("/dev/null", O_RDWR) != 0) {
152 syslog (LOG_ERR, "Error: couldn't connect STDIN to /dev/null: %s",
159 syslog (LOG_ERR, "Error: couldn't connect STDOUT to /dev/null: %s",
166 syslog (LOG_ERR, "Error: couldn't connect STDERR to /dev/null: %s",
173 static int collectd_start (char **argv)
177 if (0 > (pid = fork ())) {
178 syslog (LOG_ERR, "Error: fork() failed: %s", strerror (errno));
186 execvp (argv[0], argv);
187 syslog (LOG_ERR, "Error: execvp(%s) failed: %s",
188 argv[0], strerror (errno));
190 } /* collectd_start */
192 static int collectd_stop (void)
194 if (0 == collectd_pid)
197 if (0 != kill (collectd_pid, SIGTERM)) {
198 syslog (LOG_ERR, "Error: kill() failed: %s", strerror (errno));
202 } /* collectd_stop */
204 static void sig_int_term_handler (int __attribute__((unused)) signo)
208 } /* sig_int_term_handler */
210 static void sig_hup_handler (int __attribute__((unused)) signo)
214 } /* sig_hup_handler */
216 static void log_status (int status)
218 if (WIFEXITED (status)) {
219 if (0 == WEXITSTATUS (status))
220 syslog (LOG_INFO, "Info: collectd terminated with exit status %i",
221 WEXITSTATUS (status));
224 "Warning: collectd terminated with exit status %i",
225 WEXITSTATUS (status));
227 else if (WIFSIGNALED (status)) {
228 syslog (LOG_WARNING, "Warning: collectd was terminated by signal %i%s",
229 WTERMSIG (status), WCOREDUMP (status) ? " (core dumped)" : "");
234 static void check_respawn (void)
236 time_t t = time (NULL);
238 static time_t timestamp = 0;
239 static int counter = 0;
241 if ((t - 120) < timestamp)
249 unsigned int time_left = 300;
251 syslog (LOG_ERR, "Error: collectd is respawning too fast - "
252 "disabled for %i seconds", time_left);
254 while ((0 < (time_left = sleep (time_left))) && (0 == loop));
257 } /* check_respawn */
259 int main (int argc, char **argv)
261 int collectd_argc = 0;
262 char *collectd = NULL;
263 char **collectd_argv = NULL;
269 /* parse command line options */
271 int c = getopt (argc, argv, "hc:P:");
285 exit_usage (argv[0]);
289 for (i = optind; i < argc; ++i)
290 if (0 == strcmp (argv[i], "-f"))
293 /* i < argc => -f already present */
294 collectd_argc = 1 + argc - optind + ((i < argc) ? 0 : 1);
295 collectd_argv = (char **)calloc (collectd_argc + 1, sizeof (char *));
297 if (NULL == collectd_argv) {
298 fprintf (stderr, "Out of memory.");
302 collectd_argv[0] = (NULL == collectd) ? "collectd" : collectd;
305 collectd_argv[collectd_argc - 1] = "-f";
307 for (i = optind; i < argc; ++i)
308 collectd_argv[i - optind + 1] = argv[i];
310 collectd_argv[collectd_argc] = NULL;
312 openlog ("collectdmon", LOG_CONS | LOG_PID, LOG_DAEMON);
314 if (-1 == daemonize ())
317 sa.sa_handler = sig_int_term_handler;
319 sigemptyset (&sa.sa_mask);
321 if (0 != sigaction (SIGINT, &sa, NULL)) {
322 syslog (LOG_ERR, "Error: sigaction() failed: %s", strerror (errno));
326 if (0 != sigaction (SIGTERM, &sa, NULL)) {
327 syslog (LOG_ERR, "Error: sigaction() failed: %s", strerror (errno));
331 sa.sa_handler = sig_hup_handler;
333 if (0 != sigaction (SIGHUP, &sa, NULL)) {
334 syslog (LOG_ERR, "Error: sigaction() failed: %s", strerror (errno));
341 if (0 != collectd_start (collectd_argv)) {
342 syslog (LOG_ERR, "Error: failed to start collectd.");
346 assert (0 < collectd_pid);
347 while ((collectd_pid != waitpid (collectd_pid, &status, 0))
349 if ((0 != loop) || (0 != restart))
358 syslog (LOG_INFO, "Info: restarting collectd");
362 syslog (LOG_WARNING, "Warning: restarting collectd");
365 syslog (LOG_INFO, "Info: shutting down collectdmon");
370 free (collectd_argv);
374 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */