Merge branch 'collectd-5.4' into 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/socket.h>
36 #include <sys/un.h>
37 #include <netdb.h>
38
39 #include <pthread.h>
40
41 #if HAVE_LOCALE_H
42 # include <locale.h>
43 #endif
44
45 #if HAVE_STATGRAB_H
46 # include <statgrab.h>
47 #endif
48
49 #ifndef COLLECTD_LOCALE
50 # define COLLECTD_LOCALE "C"
51 #endif
52
53 /*
54  * Global variables
55  */
56 char hostname_g[DATA_MAX_NAME_LEN];
57 cdtime_t interval_g;
58 int  pidfile_from_cli = 0;
59 int  timeout_g;
60 #if HAVE_LIBKSTAT
61 kstat_ctl_t *kc;
62 #endif /* HAVE_LIBKSTAT */
63
64 static int loop = 0;
65
66 static void *do_flush (void __attribute__((unused)) *arg)
67 {
68         INFO ("Flushing all data.");
69         plugin_flush (/* plugin = */ NULL,
70                         /* timeout = */ 0,
71                         /* ident = */ NULL);
72         INFO ("Finished flushing all data.");
73         pthread_exit (NULL);
74         return NULL;
75 }
76
77 static void sig_int_handler (int __attribute__((unused)) signal)
78 {
79         loop++;
80 }
81
82 static void sig_term_handler (int __attribute__((unused)) signal)
83 {
84         loop++;
85 }
86
87 static void sig_usr1_handler (int __attribute__((unused)) signal)
88 {
89         pthread_t      thread;
90         pthread_attr_t attr;
91
92         /* flushing the data might take a while,
93          * so it should be done asynchronously */
94         pthread_attr_init (&attr);
95         pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
96         pthread_create (&thread, &attr, do_flush, NULL);
97         pthread_attr_destroy (&attr);
98 }
99
100 static int init_hostname (void)
101 {
102         const char *str;
103
104         struct addrinfo  ai_hints;
105         struct addrinfo *ai_list;
106         struct addrinfo *ai_ptr;
107         int status;
108
109         str = global_option_get ("Hostname");
110         if (str != NULL)
111         {
112                 sstrncpy (hostname_g, str, sizeof (hostname_g));
113                 return (0);
114         }
115
116         if (gethostname (hostname_g, sizeof (hostname_g)) != 0)
117         {
118                 fprintf (stderr, "`gethostname' failed and no "
119                                 "hostname was configured.\n");
120                 return (-1);
121         }
122
123         str = global_option_get ("FQDNLookup");
124         if (IS_FALSE (str))
125                 return (0);
126
127         memset (&ai_hints, '\0', sizeof (ai_hints));
128         ai_hints.ai_flags = AI_CANONNAME;
129
130         status = getaddrinfo (hostname_g, NULL, &ai_hints, &ai_list);
131         if (status != 0)
132         {
133                 ERROR ("Looking up \"%s\" failed. You have set the "
134                                 "\"FQDNLookup\" option, but I cannot resolve "
135                                 "my hostname to a fully qualified domain "
136                                 "name. Please fix the network "
137                                 "configuration.", hostname_g);
138                 return (-1);
139         }
140
141         for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
142         {
143                 if (ai_ptr->ai_canonname == NULL)
144                         continue;
145
146                 sstrncpy (hostname_g, ai_ptr->ai_canonname, sizeof (hostname_g));
147                 break;
148         }
149
150         freeaddrinfo (ai_list);
151         return (0);
152 } /* int init_hostname */
153
154 static int init_global_variables (void)
155 {
156         char const *str;
157
158         interval_g = cf_get_default_interval ();
159         assert (interval_g > 0);
160         DEBUG ("interval_g = %.3f;", CDTIME_T_TO_DOUBLE (interval_g));
161
162         str = global_option_get ("Timeout");
163         if (str == NULL)
164                 str = "2";
165         timeout_g = atoi (str);
166         if (timeout_g <= 1)
167         {
168                 fprintf (stderr, "Cannot set the timeout to a correct value.\n"
169                                 "Please check your settings.\n");
170                 return (-1);
171         }
172         DEBUG ("timeout_g = %i;", timeout_g);
173
174         if (init_hostname () != 0)
175                 return (-1);
176         DEBUG ("hostname_g = %s;", hostname_g);
177
178         return (0);
179 } /* int init_global_variables */
180
181 static int change_basedir (const char *orig_dir)
182 {
183         char *dir;
184         size_t dirlen;
185         int status;
186
187         dir = strdup (orig_dir);
188         if (dir == NULL)
189         {
190                 char errbuf[1024];
191                 ERROR ("strdup failed: %s",
192                                 sstrerror (errno, errbuf, sizeof (errbuf)));
193                 return (-1);
194         }
195
196         dirlen = strlen (dir);
197         while ((dirlen > 0) && (dir[dirlen - 1] == '/'))
198                 dir[--dirlen] = '\0';
199
200         if (dirlen <= 0)
201                 return (-1);
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 static void exit_usage (int status)
272 {
273         printf ("Usage: "PACKAGE_NAME" [OPTIONS]\n\n"
274
275                         "Available options:\n"
276                         "  General:\n"
277                         "    -C <file>       Configuration file.\n"
278                         "                    Default: "CONFIGFILE"\n"
279                         "    -t              Test config and exit.\n"
280                         "    -T              Test plugin read and exit.\n"
281                         "    -P <file>       PID-file.\n"
282                         "                    Default: "PIDFILE"\n"
283 #if COLLECT_DAEMON
284                         "    -f              Don't fork to the background.\n"
285 #endif
286                         "    -h              Display help (this message)\n"
287                         "\nBuiltin defaults:\n"
288                         "  Config file       "CONFIGFILE"\n"
289                         "  PID file          "PIDFILE"\n"
290                         "  Plugin directory  "PLUGINDIR"\n"
291                         "  Data directory    "PKGLOCALSTATEDIR"\n"
292                         "\n"PACKAGE_NAME" "PACKAGE_VERSION", http://collectd.org/\n"
293                         "by Florian octo Forster <octo@collectd.org>\n"
294                         "for contributions see `AUTHORS'\n");
295         exit (status);
296 } /* static void exit_usage (int status) */
297
298 static int do_init (void)
299 {
300 #if HAVE_SETLOCALE
301         if (setlocale (LC_NUMERIC, COLLECTD_LOCALE) == NULL)
302                 WARNING ("setlocale (\"%s\") failed.", COLLECTD_LOCALE);
303 #endif
304
305 #if HAVE_LIBKSTAT
306         kc = NULL;
307         update_kstat ();
308 #endif
309
310 #if HAVE_LIBSTATGRAB
311         if (sg_init (
312 # if HAVE_LIBSTATGRAB_0_90
313                     0
314 # endif
315                     ))
316         {
317                 ERROR ("sg_init: %s", sg_str_error (sg_get_error ()));
318                 return (-1);
319         }
320
321         if (sg_drop_privileges ())
322         {
323                 ERROR ("sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
324                 return (-1);
325         }
326 #endif
327
328         plugin_init_all ();
329
330         return (0);
331 } /* int do_init () */
332
333
334 static int do_loop (void)
335 {
336         cdtime_t interval = cf_get_default_interval ();
337         cdtime_t wait_until;
338
339         wait_until = cdtime () + interval;
340
341         while (loop == 0)
342         {
343                 struct timespec ts_wait = { 0, 0 };
344                 cdtime_t now;
345
346 #if HAVE_LIBKSTAT
347                 update_kstat ();
348 #endif
349
350                 /* Issue all plugins */
351                 plugin_read_all ();
352
353                 now = cdtime ();
354                 if (now >= wait_until)
355                 {
356                         WARNING ("Not sleeping because the next interval is "
357                                         "%.3f seconds in the past!",
358                                         CDTIME_T_TO_DOUBLE (now - wait_until));
359                         wait_until = now + interval;
360                         continue;
361                 }
362
363                 CDTIME_T_TO_TIMESPEC (wait_until - now, &ts_wait);
364                 wait_until = wait_until + interval;
365
366                 while ((loop == 0) && (nanosleep (&ts_wait, &ts_wait) != 0))
367                 {
368                         if (errno != EINTR)
369                         {
370                                 char errbuf[1024];
371                                 ERROR ("nanosleep failed: %s",
372                                                 sstrerror (errno, errbuf,
373                                                         sizeof (errbuf)));
374                                 return (-1);
375                         }
376                 }
377         } /* while (loop == 0) */
378
379         return (0);
380 } /* int do_loop */
381
382 static int do_shutdown (void)
383 {
384         plugin_shutdown_all ();
385         return (0);
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 int notify_upstart (void)
420 {
421     const char  *upstart_job = getenv("UPSTART_JOB");
422
423     if (upstart_job == NULL)
424         return 0;
425
426     if (strcmp(upstart_job, "collectd") != 0)
427         return 0;
428
429     WARNING ("supervised by upstart, will stop to signal readyness");
430     raise(SIGSTOP);
431     unsetenv("UPSTART_JOB");
432
433     return 1;
434 }
435
436 int notify_systemd (void)
437 {
438     int                  fd = -1;
439     const char          *notifysocket = getenv("NOTIFY_SOCKET");
440     struct sockaddr_un   su;
441     struct iovec         iov;
442     struct msghdr        hdr;
443
444     if (notifysocket == NULL)
445         return 0;
446
447     if ((strchr("@/", notifysocket[0])) == NULL ||
448         strlen(notifysocket) < 2)
449         return 0;
450
451     WARNING ("supervised by systemd, will signal readyness");
452     if ((fd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) {
453         WARNING ("cannot contact systemd socket %s", notifysocket);
454         return 0;
455     }
456
457     bzero(&su, sizeof(su));
458     su.sun_family = AF_UNIX;
459     sstrncpy (su.sun_path, notifysocket, sizeof(su.sun_path));
460
461     if (notifysocket[0] == '@')
462         su.sun_path[0] = 0;
463
464     bzero(&iov, sizeof(iov));
465     iov.iov_base = "READY=1";
466     iov.iov_len = strlen("READY=1");
467
468     bzero(&hdr, sizeof(hdr));
469     hdr.msg_name = &su;
470     hdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) +
471         strlen(notifysocket);
472     hdr.msg_iov = &iov;
473     hdr.msg_iovlen = 1;
474
475     unsetenv("NOTIFY_SOCKET");
476     if (sendmsg(fd, &hdr, MSG_NOSIGNAL) < 0) {
477         WARNING ("cannot send notification to systemd");
478         close(fd);
479         return 0;
480     }
481     close(fd);
482     return 1;
483 }
484 #endif /* KERNEL_LINUX */
485
486 int main (int argc, char **argv)
487 {
488         struct sigaction sig_int_action;
489         struct sigaction sig_term_action;
490         struct sigaction sig_usr1_action;
491         struct sigaction sig_pipe_action;
492         char *configfile = CONFIGFILE;
493         int test_config  = 0;
494         int test_readall = 0;
495         const char *basedir;
496 #if COLLECT_DAEMON
497         struct sigaction sig_chld_action;
498         pid_t pid;
499         int daemonize    = 1;
500 #endif
501         int exit_status = 0;
502
503         /* read options */
504         while (1)
505         {
506                 int c;
507
508                 c = getopt (argc, argv, "htTC:"
509 #if COLLECT_DAEMON
510                                 "fP:"
511 #endif
512                 );
513
514                 if (c == -1)
515                         break;
516
517                 switch (c)
518                 {
519                         case 'C':
520                                 configfile = optarg;
521                                 break;
522                         case 't':
523                                 test_config = 1;
524                                 break;
525                         case 'T':
526                                 test_readall = 1;
527                                 global_option_set ("ReadThreads", "-1");
528 #if COLLECT_DAEMON
529                                 daemonize = 0;
530 #endif /* COLLECT_DAEMON */
531                                 break;
532 #if COLLECT_DAEMON
533                         case 'P':
534                                 global_option_set ("PIDFile", optarg);
535                                 pidfile_from_cli = 1;
536                                 break;
537                         case 'f':
538                                 daemonize = 0;
539                                 break;
540 #endif /* COLLECT_DAEMON */
541                         case 'h':
542                                 exit_usage (0);
543                                 break;
544                         default:
545                                 exit_usage (1);
546                 } /* switch (c) */
547         } /* while (1) */
548
549         if (optind < argc)
550                 exit_usage (1);
551
552         plugin_init_ctx ();
553
554         /*
555          * Read options from the config file, the environment and the command
556          * line (in that order, with later options overwriting previous ones in
557          * general).
558          * Also, this will automatically load modules.
559          */
560         if (cf_read (configfile))
561         {
562                 fprintf (stderr, "Error: Reading the config file failed!\n"
563                                 "Read the syslog for details.\n");
564                 return (1);
565         }
566
567         /*
568          * Change directory. We do this _after_ reading the config and loading
569          * modules to relative paths work as expected.
570          */
571         if ((basedir = global_option_get ("BaseDir")) == NULL)
572         {
573                 fprintf (stderr, "Don't have a basedir to use. This should not happen. Ever.");
574                 return (1);
575         }
576         else if (change_basedir (basedir))
577         {
578                 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", basedir);
579                 return (1);
580         }
581
582         /*
583          * Set global variables or, if that failes, exit. We cannot run with
584          * them being uninitialized. If nothing is configured, then defaults
585          * are being used. So this means that the user has actually done
586          * something wrong.
587          */
588         if (init_global_variables () != 0)
589                 return (1);
590
591         if (test_config)
592                 return (0);
593
594 #if COLLECT_DAEMON
595         /*
596          * fork off child
597          */
598         memset (&sig_chld_action, '\0', sizeof (sig_chld_action));
599         sig_chld_action.sa_handler = SIG_IGN;
600         sigaction (SIGCHLD, &sig_chld_action, NULL);
601
602     /*
603      * Only daemonize if we're not being supervised
604      * by upstart or systemd (when using Linux).
605      */
606         if (daemonize
607 #ifdef KERNEL_LINUX
608             && notify_upstart() == 0 && notify_systemd() == 0
609 #endif
610         )
611         {
612                 if ((pid = fork ()) == -1)
613                 {
614                         /* error */
615                         char errbuf[1024];
616                         fprintf (stderr, "fork: %s",
617                                         sstrerror (errno, errbuf,
618                                                 sizeof (errbuf)));
619                         return (1);
620                 }
621                 else if (pid != 0)
622                 {
623                         /* parent */
624                         /* printf ("Running (PID %i)\n", pid); */
625                         return (0);
626                 }
627
628                 /* Detach from session */
629                 setsid ();
630
631                 /* Write pidfile */
632                 if (pidfile_create ())
633                         exit (2);
634
635                 /* close standard descriptors */
636                 close (2);
637                 close (1);
638                 close (0);
639
640                 if (open ("/dev/null", O_RDWR) != 0)
641                 {
642                         ERROR ("Error: Could not connect `STDIN' to `/dev/null'");
643                         return (1);
644                 }
645                 if (dup (0) != 1)
646                 {
647                         ERROR ("Error: Could not connect `STDOUT' to `/dev/null'");
648                         return (1);
649                 }
650                 if (dup (0) != 2)
651                 {
652                         ERROR ("Error: Could not connect `STDERR' to `/dev/null'");
653                         return (1);
654                 }
655         } /* if (daemonize) */
656 #endif /* COLLECT_DAEMON */
657
658         memset (&sig_pipe_action, '\0', sizeof (sig_pipe_action));
659         sig_pipe_action.sa_handler = SIG_IGN;
660         sigaction (SIGPIPE, &sig_pipe_action, NULL);
661
662         /*
663          * install signal handlers
664          */
665         memset (&sig_int_action, '\0', sizeof (sig_int_action));
666         sig_int_action.sa_handler = sig_int_handler;
667         if (0 != sigaction (SIGINT, &sig_int_action, NULL)) {
668                 char errbuf[1024];
669                 ERROR ("Error: Failed to install a signal handler for signal INT: %s",
670                                 sstrerror (errno, errbuf, sizeof (errbuf)));
671                 return (1);
672         }
673
674         memset (&sig_term_action, '\0', sizeof (sig_term_action));
675         sig_term_action.sa_handler = sig_term_handler;
676         if (0 != sigaction (SIGTERM, &sig_term_action, NULL)) {
677                 char errbuf[1024];
678                 ERROR ("Error: Failed to install a signal handler for signal TERM: %s",
679                                 sstrerror (errno, errbuf, sizeof (errbuf)));
680                 return (1);
681         }
682
683         memset (&sig_usr1_action, '\0', sizeof (sig_usr1_action));
684         sig_usr1_action.sa_handler = sig_usr1_handler;
685         if (0 != sigaction (SIGUSR1, &sig_usr1_action, NULL)) {
686                 char errbuf[1024];
687                 ERROR ("Error: Failed to install a signal handler for signal USR1: %s",
688                                 sstrerror (errno, errbuf, sizeof (errbuf)));
689                 return (1);
690         }
691
692         /*
693          * run the actual loops
694          */
695         do_init ();
696
697         if (test_readall)
698         {
699                 if (plugin_read_all_once () != 0)
700                         exit_status = 1;
701         }
702         else
703         {
704                 INFO ("Initialization complete, entering read-loop.");
705                 do_loop ();
706         }
707
708         /* close syslog */
709         INFO ("Exiting normally.");
710
711         do_shutdown ();
712
713 #if COLLECT_DAEMON
714         if (daemonize)
715                 pidfile_remove ();
716 #endif /* COLLECT_DAEMON */
717
718         return (exit_status);
719 } /* int main */