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