daemon: Let all command line options override global options from the config.
[collectd.git] / src / daemon / collectd.c
1 /**
2  * collectd - src/collectd.c
3  * Copyright (C) 2005-2007  Florian octo Forster
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *   Florian octo Forster <octo at collectd.org>
25  *   Alvaro Barcellos <alvaro.barcellos at gmail.com>
26  **/
27
28 #include "collectd.h"
29 #include "common.h"
30
31 #include "plugin.h"
32 #include "configfile.h"
33
34 #include <sys/types.h>
35 #include <sys/un.h>
36 #include <netdb.h>
37
38 #include <pthread.h>
39
40 #if HAVE_LOCALE_H
41 # include <locale.h>
42 #endif
43
44 #if HAVE_STATGRAB_H
45 # include <statgrab.h>
46 #endif
47
48 #ifndef COLLECTD_LOCALE
49 # define COLLECTD_LOCALE "C"
50 #endif
51
52 /*
53  * Global variables
54  */
55 char hostname_g[DATA_MAX_NAME_LEN];
56 cdtime_t interval_g;
57 int  timeout_g;
58 #if HAVE_LIBKSTAT
59 kstat_ctl_t *kc;
60 #endif /* HAVE_LIBKSTAT */
61
62 static int loop = 0;
63
64 static void *do_flush (void __attribute__((unused)) *arg)
65 {
66         INFO ("Flushing all data.");
67         plugin_flush (/* plugin = */ NULL,
68                         /* timeout = */ 0,
69                         /* ident = */ NULL);
70         INFO ("Finished flushing all data.");
71         pthread_exit (NULL);
72         return NULL;
73 }
74
75 static void sig_int_handler (int __attribute__((unused)) signal)
76 {
77         loop++;
78 }
79
80 static void sig_term_handler (int __attribute__((unused)) signal)
81 {
82         loop++;
83 }
84
85 static void sig_usr1_handler (int __attribute__((unused)) signal)
86 {
87         pthread_t      thread;
88         pthread_attr_t attr;
89
90         /* flushing the data might take a while,
91          * so it should be done asynchronously */
92         pthread_attr_init (&attr);
93         pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
94         pthread_create (&thread, &attr, do_flush, NULL);
95         pthread_attr_destroy (&attr);
96 }
97
98 static int init_hostname (void)
99 {
100         const char *str;
101
102         struct addrinfo  ai_hints;
103         struct addrinfo *ai_list;
104         struct addrinfo *ai_ptr;
105         int status;
106
107         str = global_option_get ("Hostname");
108         if (str != NULL)
109         {
110                 sstrncpy (hostname_g, str, sizeof (hostname_g));
111                 return (0);
112         }
113
114         if (gethostname (hostname_g, sizeof (hostname_g)) != 0)
115         {
116                 fprintf (stderr, "`gethostname' failed and no "
117                                 "hostname was configured.\n");
118                 return (-1);
119         }
120
121         str = global_option_get ("FQDNLookup");
122         if (IS_FALSE (str))
123                 return (0);
124
125         memset (&ai_hints, '\0', sizeof (ai_hints));
126         ai_hints.ai_flags = AI_CANONNAME;
127
128         status = getaddrinfo (hostname_g, NULL, &ai_hints, &ai_list);
129         if (status != 0)
130         {
131                 ERROR ("Looking up \"%s\" failed. You have set the "
132                                 "\"FQDNLookup\" option, but I cannot resolve "
133                                 "my hostname to a fully qualified domain "
134                                 "name. Please fix the network "
135                                 "configuration.", hostname_g);
136                 return (-1);
137         }
138
139         for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
140         {
141                 if (ai_ptr->ai_canonname == NULL)
142                         continue;
143
144                 sstrncpy (hostname_g, ai_ptr->ai_canonname, sizeof (hostname_g));
145                 break;
146         }
147
148         freeaddrinfo (ai_list);
149         return (0);
150 } /* int init_hostname */
151
152 static int init_global_variables (void)
153 {
154         char const *str;
155
156         interval_g = cf_get_default_interval ();
157         assert (interval_g > 0);
158         DEBUG ("interval_g = %.3f;", CDTIME_T_TO_DOUBLE (interval_g));
159
160         str = global_option_get ("Timeout");
161         if (str == NULL)
162                 str = "2";
163         timeout_g = atoi (str);
164         if (timeout_g <= 1)
165         {
166                 fprintf (stderr, "Cannot set the timeout to a correct value.\n"
167                                 "Please check your settings.\n");
168                 return (-1);
169         }
170         DEBUG ("timeout_g = %i;", timeout_g);
171
172         if (init_hostname () != 0)
173                 return (-1);
174         DEBUG ("hostname_g = %s;", hostname_g);
175
176         return (0);
177 } /* int init_global_variables */
178
179 static int change_basedir (const char *orig_dir)
180 {
181         char *dir;
182         size_t dirlen;
183         int status;
184
185         dir = strdup (orig_dir);
186         if (dir == NULL)
187         {
188                 char errbuf[1024];
189                 ERROR ("strdup failed: %s",
190                                 sstrerror (errno, errbuf, sizeof (errbuf)));
191                 return (-1);
192         }
193
194         dirlen = strlen (dir);
195         while ((dirlen > 0) && (dir[dirlen - 1] == '/'))
196                 dir[--dirlen] = '\0';
197
198         if (dirlen == 0) {
199                 free (dir);
200                 return (-1);
201         }
202
203         status = chdir (dir);
204         if (status == 0)
205         {
206                 free (dir);
207                 return (0);
208         }
209         else if (errno != ENOENT)
210         {
211                 char errbuf[1024];
212                 ERROR ("change_basedir: chdir (%s): %s", dir,
213                                 sstrerror (errno, errbuf, sizeof (errbuf)));
214                 free (dir);
215                 return (-1);
216         }
217
218         status = mkdir (dir, S_IRWXU | S_IRWXG | S_IRWXO);
219         if (status != 0)
220         {
221                 char errbuf[1024];
222                 ERROR ("change_basedir: mkdir (%s): %s", dir,
223                                 sstrerror (errno, errbuf, sizeof (errbuf)));
224                 free (dir);
225                 return (-1);
226         }
227
228         status = chdir (dir);
229         if (status != 0)
230         {
231                 char errbuf[1024];
232                 ERROR ("change_basedir: chdir (%s): %s", dir,
233                                 sstrerror (errno, errbuf, sizeof (errbuf)));
234                 free (dir);
235                 return (-1);
236         }
237
238         free (dir);
239         return (0);
240 } /* static int change_basedir (char *dir) */
241
242 #if HAVE_LIBKSTAT
243 static void update_kstat (void)
244 {
245         if (kc == NULL)
246         {
247                 if ((kc = kstat_open ()) == NULL)
248                         ERROR ("Unable to open kstat control structure");
249         }
250         else
251         {
252                 kid_t kid;
253                 kid = kstat_chain_update (kc);
254                 if (kid > 0)
255                 {
256                         INFO ("kstat chain has been updated");
257                         plugin_init_all ();
258                 }
259                 else if (kid < 0)
260                         ERROR ("kstat chain update failed");
261                 /* else: everything works as expected */
262         }
263
264         return;
265 } /* static void update_kstat (void) */
266 #endif /* HAVE_LIBKSTAT */
267
268 /* TODO
269  * Remove all settings but `-f' and `-C'
270  */
271 __attribute__((noreturn))
272 static void exit_usage (int status)
273 {
274         printf ("Usage: "PACKAGE_NAME" [OPTIONS]\n\n"
275
276                         "Available options:\n"
277                         "  General:\n"
278                         "    -C <file>       Configuration file.\n"
279                         "                    Default: "CONFIGFILE"\n"
280                         "    -t              Test config and exit.\n"
281                         "    -T              Test plugin read and exit.\n"
282                         "    -P <file>       PID-file.\n"
283                         "                    Default: "PIDFILE"\n"
284 #if COLLECT_DAEMON
285                         "    -f              Don't fork to the background.\n"
286 #endif
287                         "    -h              Display help (this message)\n"
288                         "\nBuiltin defaults:\n"
289                         "  Config file       "CONFIGFILE"\n"
290                         "  PID file          "PIDFILE"\n"
291                         "  Plugin directory  "PLUGINDIR"\n"
292                         "  Data directory    "PKGLOCALSTATEDIR"\n"
293                         "\n"PACKAGE_NAME" "PACKAGE_VERSION", http://collectd.org/\n"
294                         "by Florian octo Forster <octo@collectd.org>\n"
295                         "for contributions see `AUTHORS'\n");
296         exit (status);
297 } /* static void exit_usage (int status) */
298
299 static int do_init (void)
300 {
301 #if HAVE_SETLOCALE
302         if (setlocale (LC_NUMERIC, COLLECTD_LOCALE) == NULL)
303                 WARNING ("setlocale (\"%s\") failed.", COLLECTD_LOCALE);
304
305         /* Update the environment, so that libraries that are calling
306          * setlocale(LC_NUMERIC, "") don't accidentally revert these changes. */
307         unsetenv ("LC_ALL");
308         setenv ("LC_NUMERIC", COLLECTD_LOCALE, /* overwrite = */ 1);
309 #endif
310
311 #if HAVE_LIBKSTAT
312         kc = NULL;
313         update_kstat ();
314 #endif
315
316 #if HAVE_LIBSTATGRAB
317         if (sg_init (
318 # if HAVE_LIBSTATGRAB_0_90
319                     0
320 # endif
321                     ))
322         {
323                 ERROR ("sg_init: %s", sg_str_error (sg_get_error ()));
324                 return (-1);
325         }
326
327         if (sg_drop_privileges ())
328         {
329                 ERROR ("sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
330                 return (-1);
331         }
332 #endif
333
334         return plugin_init_all ();
335 } /* int do_init () */
336
337
338 static int do_loop (void)
339 {
340         cdtime_t interval = cf_get_default_interval ();
341         cdtime_t wait_until;
342
343         wait_until = cdtime () + interval;
344
345         while (loop == 0)
346         {
347                 struct timespec ts_wait = { 0, 0 };
348                 cdtime_t now;
349
350 #if HAVE_LIBKSTAT
351                 update_kstat ();
352 #endif
353
354                 /* Issue all plugins */
355                 plugin_read_all ();
356
357                 now = cdtime ();
358                 if (now >= wait_until)
359                 {
360                         WARNING ("Not sleeping because the next interval is "
361                                         "%.3f seconds in the past!",
362                                         CDTIME_T_TO_DOUBLE (now - wait_until));
363                         wait_until = now + interval;
364                         continue;
365                 }
366
367                 CDTIME_T_TO_TIMESPEC (wait_until - now, &ts_wait);
368                 wait_until = wait_until + interval;
369
370                 while ((loop == 0) && (nanosleep (&ts_wait, &ts_wait) != 0))
371                 {
372                         if (errno != EINTR)
373                         {
374                                 char errbuf[1024];
375                                 ERROR ("nanosleep failed: %s",
376                                                 sstrerror (errno, errbuf,
377                                                         sizeof (errbuf)));
378                                 return (-1);
379                         }
380                 }
381         } /* while (loop == 0) */
382
383         return (0);
384 } /* int do_loop */
385
386 static int do_shutdown (void)
387 {
388         return plugin_shutdown_all ();
389 } /* int do_shutdown */
390
391 #if COLLECT_DAEMON
392 static int pidfile_create (void)
393 {
394         FILE *fh;
395         const char *file = global_option_get ("PIDFile");
396
397         if ((fh = fopen (file, "w")) == NULL)
398         {
399                 char errbuf[1024];
400                 ERROR ("fopen (%s): %s", file,
401                                 sstrerror (errno, errbuf, sizeof (errbuf)));
402                 return (1);
403         }
404
405         fprintf (fh, "%i\n", (int) getpid ());
406         fclose(fh);
407
408         return (0);
409 } /* static int pidfile_create (const char *file) */
410
411 static int pidfile_remove (void)
412 {
413         const char *file = global_option_get ("PIDFile");
414         if (file == NULL)
415                 return 0;
416
417         return (unlink (file));
418 } /* static int pidfile_remove (const char *file) */
419 #endif /* COLLECT_DAEMON */
420
421 #ifdef KERNEL_LINUX
422 static int notify_upstart (void)
423 {
424     char const *upstart_job = getenv("UPSTART_JOB");
425
426     if (upstart_job == NULL)
427         return 0;
428
429     if (strcmp(upstart_job, "collectd") != 0)
430     {
431         WARNING ("Environment specifies unexpected UPSTART_JOB=\"%s\", expected \"collectd\". Ignoring the variable.", upstart_job);
432         return 0;
433     }
434
435     NOTICE("Upstart detected, stopping now to signal readyness.");
436     raise(SIGSTOP);
437     unsetenv("UPSTART_JOB");
438
439     return 1;
440 }
441
442 static int notify_systemd (void)
443 {
444     int                  fd;
445     const char          *notifysocket;
446     struct sockaddr_un   su;
447     size_t               su_size;
448     char                 buffer[] = "READY=1\n";
449
450     notifysocket = getenv ("NOTIFY_SOCKET");
451     if (notifysocket == NULL)
452         return 0;
453
454     if ((strlen (notifysocket) < 2)
455         || ((notifysocket[0] != '@') && (notifysocket[0] != '/')))
456     {
457         ERROR ("invalid notification socket NOTIFY_SOCKET=\"%s\": path must be absolute", notifysocket);
458         return 0;
459     }
460     NOTICE ("Systemd detected, trying to signal readyness.");
461
462     unsetenv ("NOTIFY_SOCKET");
463
464 #if defined(SOCK_CLOEXEC)
465     fd = socket (AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, /* protocol = */ 0);
466 #else
467     fd = socket (AF_UNIX, SOCK_DGRAM, /* protocol = */ 0);
468 #endif
469     if (fd < 0) {
470         char errbuf[1024];
471         ERROR ("creating UNIX socket failed: %s",
472                  sstrerror (errno, errbuf, sizeof (errbuf)));
473         return 0;
474     }
475
476     memset (&su, 0, sizeof (su));
477     su.sun_family = AF_UNIX;
478     if (notifysocket[0] != '@')
479     {
480         /* regular UNIX socket */
481         sstrncpy (su.sun_path, notifysocket, sizeof (su.sun_path));
482         su_size = sizeof (su);
483     }
484     else
485     {
486         /* Linux abstract namespace socket: specify address as "\0foo", i.e.
487          * start with a null byte. Since null bytes have no special meaning in
488          * that case, we have to set su_size correctly to cover only the bytes
489          * that are part of the address. */
490         sstrncpy (su.sun_path, notifysocket, sizeof (su.sun_path));
491         su.sun_path[0] = 0;
492         su_size = sizeof (sa_family_t) + strlen (notifysocket);
493         if (su_size > sizeof (su))
494             su_size = sizeof (su);
495     }
496
497     if (sendto (fd, buffer, strlen (buffer), MSG_NOSIGNAL, (void *) &su, (socklen_t) su_size) < 0)
498     {
499         char errbuf[1024];
500         ERROR ("sendto(\"%s\") failed: %s", notifysocket,
501                  sstrerror (errno, errbuf, sizeof (errbuf)));
502         close(fd);
503         return 0;
504     }
505
506     unsetenv ("NOTIFY_SOCKET");
507     close(fd);
508     return 1;
509 }
510 #endif /* KERNEL_LINUX */
511
512 int main (int argc, char **argv)
513 {
514         struct sigaction sig_int_action;
515         struct sigaction sig_term_action;
516         struct sigaction sig_usr1_action;
517         struct sigaction sig_pipe_action;
518         const char *configfile = CONFIGFILE;
519         int test_config  = 0;
520         int test_readall = 0;
521         const char *basedir;
522 #if COLLECT_DAEMON
523         struct sigaction sig_chld_action;
524         pid_t pid;
525         int daemonize    = 1;
526 #endif
527         int exit_status = 0;
528
529         /* read options */
530         while (1)
531         {
532                 int c;
533
534                 c = getopt (argc, argv, "htTC:"
535 #if COLLECT_DAEMON
536                                 "fP:"
537 #endif
538                 );
539
540                 if (c == -1)
541                         break;
542
543                 switch (c)
544                 {
545                         case 'C':
546                                 configfile = optarg;
547                                 break;
548                         case 't':
549                                 test_config = 1;
550                                 break;
551                         case 'T':
552                                 test_readall = 1;
553                                 global_option_set ("ReadThreads", "-1", 1);
554 #if COLLECT_DAEMON
555                                 daemonize = 0;
556 #endif /* COLLECT_DAEMON */
557                                 break;
558 #if COLLECT_DAEMON
559                         case 'P':
560                                 global_option_set ("PIDFile", optarg, 1);
561                                 break;
562                         case 'f':
563                                 daemonize = 0;
564                                 break;
565 #endif /* COLLECT_DAEMON */
566                         case 'h':
567                                 exit_usage (0);
568                                 break;
569                         default:
570                                 exit_usage (1);
571                 } /* switch (c) */
572         } /* while (1) */
573
574         if (optind < argc)
575                 exit_usage (1);
576
577         plugin_init_ctx ();
578
579         /*
580          * Read options from the config file, the environment and the command
581          * line (in that order, with later options overwriting previous ones in
582          * general).
583          * Also, this will automatically load modules.
584          */
585         if (cf_read (configfile))
586         {
587                 fprintf (stderr, "Error: Reading the config file failed!\n"
588                                 "Read the syslog for details.\n");
589                 return (1);
590         }
591
592         /*
593          * Change directory. We do this _after_ reading the config and loading
594          * modules to relative paths work as expected.
595          */
596         if ((basedir = global_option_get ("BaseDir")) == NULL)
597         {
598                 fprintf (stderr, "Don't have a basedir to use. This should not happen. Ever.");
599                 return (1);
600         }
601         else if (change_basedir (basedir))
602         {
603                 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", basedir);
604                 return (1);
605         }
606
607         /*
608          * Set global variables or, if that failes, exit. We cannot run with
609          * them being uninitialized. If nothing is configured, then defaults
610          * are being used. So this means that the user has actually done
611          * something wrong.
612          */
613         if (init_global_variables () != 0)
614                 return (1);
615
616         if (test_config)
617                 return (0);
618
619 #if COLLECT_DAEMON
620         /*
621          * fork off child
622          */
623         memset (&sig_chld_action, '\0', sizeof (sig_chld_action));
624         sig_chld_action.sa_handler = SIG_IGN;
625         sigaction (SIGCHLD, &sig_chld_action, NULL);
626
627     /*
628      * Only daemonize if we're not being supervised
629      * by upstart or systemd (when using Linux).
630      */
631         if (daemonize
632 #ifdef KERNEL_LINUX
633             && notify_upstart() == 0 && notify_systemd() == 0
634 #endif
635         )
636         {
637                 int status;
638
639                 if ((pid = fork ()) == -1)
640                 {
641                         /* error */
642                         char errbuf[1024];
643                         fprintf (stderr, "fork: %s",
644                                         sstrerror (errno, errbuf,
645                                                 sizeof (errbuf)));
646                         return (1);
647                 }
648                 else if (pid != 0)
649                 {
650                         /* parent */
651                         /* printf ("Running (PID %i)\n", pid); */
652                         return (0);
653                 }
654
655                 /* Detach from session */
656                 setsid ();
657
658                 /* Write pidfile */
659                 if (pidfile_create ())
660                         exit (2);
661
662                 /* close standard descriptors */
663                 close (2);
664                 close (1);
665                 close (0);
666
667                 status = open ("/dev/null", O_RDWR);
668                 if (status != 0)
669                 {
670                         ERROR ("Error: Could not connect `STDIN' to `/dev/null' (status %d)", status);
671                         return (1);
672                 }
673
674                 status = dup (0);
675                 if (status != 1)
676                 {
677                         ERROR ("Error: Could not connect `STDOUT' to `/dev/null' (status %d)", status);
678                         return (1);
679                 }
680
681                 status = dup (0);
682                 if (status != 2)
683                 {
684                         ERROR ("Error: Could not connect `STDERR' to `/dev/null', (status %d)", status);
685                         return (1);
686                 }
687         } /* if (daemonize) */
688 #endif /* COLLECT_DAEMON */
689
690         memset (&sig_pipe_action, '\0', sizeof (sig_pipe_action));
691         sig_pipe_action.sa_handler = SIG_IGN;
692         sigaction (SIGPIPE, &sig_pipe_action, NULL);
693
694         /*
695          * install signal handlers
696          */
697         memset (&sig_int_action, '\0', sizeof (sig_int_action));
698         sig_int_action.sa_handler = sig_int_handler;
699         if (0 != sigaction (SIGINT, &sig_int_action, NULL)) {
700                 char errbuf[1024];
701                 ERROR ("Error: Failed to install a signal handler for signal INT: %s",
702                                 sstrerror (errno, errbuf, sizeof (errbuf)));
703                 return (1);
704         }
705
706         memset (&sig_term_action, '\0', sizeof (sig_term_action));
707         sig_term_action.sa_handler = sig_term_handler;
708         if (0 != sigaction (SIGTERM, &sig_term_action, NULL)) {
709                 char errbuf[1024];
710                 ERROR ("Error: Failed to install a signal handler for signal TERM: %s",
711                                 sstrerror (errno, errbuf, sizeof (errbuf)));
712                 return (1);
713         }
714
715         memset (&sig_usr1_action, '\0', sizeof (sig_usr1_action));
716         sig_usr1_action.sa_handler = sig_usr1_handler;
717         if (0 != sigaction (SIGUSR1, &sig_usr1_action, NULL)) {
718                 char errbuf[1024];
719                 ERROR ("Error: Failed to install a signal handler for signal USR1: %s",
720                                 sstrerror (errno, errbuf, sizeof (errbuf)));
721                 return (1);
722         }
723
724         /*
725          * run the actual loops
726          */
727         if (do_init () != 0)
728         {
729                 ERROR ("Error: one or more plugin init callbacks failed.");
730                 exit_status = 1;
731         }
732
733         if (test_readall)
734         {
735                 if (plugin_read_all_once () != 0)
736                 {
737                         ERROR ("Error: one or more plugin read callbacks failed.");
738                         exit_status = 1;
739                 }
740         }
741         else
742         {
743                 INFO ("Initialization complete, entering read-loop.");
744                 do_loop ();
745         }
746
747         /* close syslog */
748         INFO ("Exiting normally.");
749
750         if (do_shutdown () != 0)
751         {
752                 ERROR ("Error: one or more plugin shutdown callbacks failed.");
753                 exit_status = 1;
754         }
755
756 #if COLLECT_DAEMON
757         if (daemonize)
758                 pidfile_remove ();
759 #endif /* COLLECT_DAEMON */
760
761         return (exit_status);
762 } /* int main */