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