2 * collectd - src/rrdd.c
3 * Copyright (C) 2008 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
27 # define RRDD_LOG(severity, ...) do { fprintf (stderr, __VA_ARGS__); fprintf (stderr, "\n"); } while (0)
29 # define RRDD_LOG(severity, ...) syslog ((severity), __VA_ARGS__)
35 struct listen_socket_s
38 char path[PATH_MAX + 1];
40 typedef struct listen_socket_s listen_socket_t;
43 typedef struct cache_item_s cache_item_t;
49 time_t last_flush_time;
50 #define CI_FLAGS_IN_TREE 0x01
51 #define CI_FLAGS_IN_QUEUE 0x02
60 static listen_socket_t *listen_fds = NULL;
61 static size_t listen_fds_num = 0;
63 static int do_shutdown = 0;
65 static pthread_t queue_thread;
67 static pthread_t *connetion_threads = NULL;
68 static pthread_mutex_t connetion_threads_lock = PTHREAD_MUTEX_INITIALIZER;
69 static int connetion_threads_num = 0;
72 static avl_tree_t *cache_tree = NULL;
73 static cache_item_t *cache_queue_head = NULL;
74 static cache_item_t *cache_queue_tail = NULL;
75 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
76 static pthread_cond_t cache_cond = PTHREAD_COND_INITIALIZER;
78 static int config_write_interval = 300;
79 static int config_flush_interval = 3600;
81 static char **config_listen_address_list = NULL;
82 static int config_listen_address_list_len = 0;
87 static void sig_int_handler (int signal) /* {{{ */
90 } /* }}} void sig_int_handler */
92 static void sig_term_handler (int signal) /* {{{ */
95 } /* }}} void sig_term_handler */
97 static int cache_tree_compare (const void *v0, const void *v1) /* {{{ */
99 cache_item_t *c0 = (cache_item_t *) v0;
100 cache_item_t *c1 = (cache_item_t *) v1;
102 assert (c0->file != NULL);
103 assert (c1->file != NULL);
105 return (strcmp (c0->file, c1->file));
106 } /* }}} int cache_tree_compare */
108 static void cache_tree_free (void *v) /* {{{ */
110 cache_item_t *c = (cache_item_t *) v;
112 assert (c->values_num == 0);
113 assert ((c->flags & CI_FLAGS_IN_TREE) != 0);
114 assert ((c->flags & CI_FLAGS_IN_QUEUE) == 0);
119 } /* }}} void cache_tree_free */
121 static void *queue_thread_main (void *args) /* {{{ */
123 pthread_mutex_lock (&cache_lock);
124 while ((do_shutdown == 0) || (cache_queue_head != NULL))
134 if (cache_queue_head == NULL)
135 pthread_cond_wait (&cache_cond, &cache_lock);
137 if (cache_queue_head == NULL)
140 ci = cache_queue_head;
142 /* copy the relevant parts */
143 file = strdup (ci->file);
146 RRDD_LOG (LOG_ERR, "queue_thread_main: strdup failed.");
151 values_num = ci->values_num;
156 ci->last_flush_time = time (NULL);
157 ci->flags &= ~(CI_FLAGS_IN_QUEUE);
159 cache_queue_head = ci->next;
160 if (cache_queue_head == NULL)
161 cache_queue_tail = NULL;
164 pthread_mutex_unlock (&cache_lock);
166 RRDD_LOG (LOG_DEBUG, "queue_thread_main: rrd_update (%s, %i, %p)",
167 file, values_num, (void *) values);
169 status = rrd_update_r (file, NULL, values_num, (void *) values);
172 RRDD_LOG (LOG_ERR, "queue_thread_main: "
173 "rrd_update_r failed with status %i.",
178 for (i = 0; i < values_num; i++)
181 pthread_mutex_lock (&cache_lock);
182 } /* while (do_shutdown == 0) */
183 pthread_mutex_unlock (&cache_lock);
185 RRDD_LOG (LOG_DEBUG, "queue_thread_main: Exiting.");
188 } /* }}} void *queue_thread_main */
190 static int handle_request_update (int fd, /* {{{ */
191 char *buffer, int buffer_size)
202 cache_item_t ci_temp;
209 RRDD_LOG (LOG_DEBUG, "handle_request_update (%i, %p, %i)",
210 fd, (void *) buffer, buffer_size);
215 buffer_ptr += strlen (file) + 1;
219 pthread_mutex_lock (&cache_lock);
221 node = avl_search (cache_tree, (void *) &ci_temp);
224 ci = (cache_item_t *) malloc (sizeof (cache_item_t));
227 pthread_mutex_unlock (&cache_lock);
228 RRDD_LOG (LOG_ERR, "handle_request_update: malloc failed.");
231 memset (ci, 0, sizeof (cache_item_t));
233 ci->file = strdup (file);
234 if (ci->file == NULL)
236 pthread_mutex_unlock (&cache_lock);
237 RRDD_LOG (LOG_ERR, "handle_request_update: malloc failed.");
244 ci->last_flush_time = now;
245 ci->flags = CI_FLAGS_IN_TREE;
247 if (avl_insert (cache_tree, (void *) ci) == NULL)
249 pthread_mutex_unlock (&cache_lock);
250 RRDD_LOG (LOG_ERR, "handle_request_update: avl_insert failed.");
256 RRDD_LOG (LOG_DEBUG, "handle_request_update: Created new AVL node %s.",
259 else /* if (ci != NULL) */
261 ci = (cache_item_t *) node->item;
265 while (*buffer_ptr != 0)
270 buffer_ptr += strlen (value) + 1;
272 temp = (char **) realloc (ci->values,
273 sizeof (char *) * (ci->values_num + 1));
276 RRDD_LOG (LOG_ERR, "handle_request_update: realloc failed.");
281 ci->values[ci->values_num] = strdup (value);
282 if (ci->values[ci->values_num] == NULL)
284 RRDD_LOG (LOG_ERR, "handle_request_update: strdup failed.");
292 if (((now - ci->last_flush_time) >= config_write_interval)
293 && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0))
295 RRDD_LOG (LOG_DEBUG, "handle_request_update: Adding %s to the update queue.",
298 assert (ci->next == NULL);
300 if (cache_queue_tail == NULL)
301 cache_queue_head = ci;
303 cache_queue_tail->next = ci;
304 cache_queue_tail = ci;
306 pthread_cond_signal (&cache_cond);
309 pthread_mutex_unlock (&cache_lock);
311 snprintf (answer, sizeof (answer), "0 Enqueued %i value(s)\n", values_num);
312 answer[sizeof (answer) - 1] = 0;
314 status = write (fd, answer, sizeof (answer));
318 RRDD_LOG (LOG_INFO, "handle_request_update: write(2) returned an error.");
323 } /* }}} int handle_request_update */
325 static int handle_request (int fd) /* {{{ */
330 RRDD_LOG (LOG_DEBUG, "handle_request (%i)", fd);
332 buffer_size = read (fd, buffer, sizeof (buffer));
335 RRDD_LOG (LOG_ERR, "handle_request: read(2) failed.");
338 assert (((size_t) buffer_size) <= sizeof (buffer));
340 if ((buffer[buffer_size - 2] != 0)
341 || (buffer[buffer_size - 1] != 0))
343 RRDD_LOG (LOG_INFO, "handle_request: malformed request.");
347 /* fields in the buffer a separated by null bytes. */
348 if (strcmp (buffer, "update") == 0)
350 int offset = strlen ("update") + 1;
351 return (handle_request_update (fd, buffer + offset,
352 buffer_size - offset));
356 RRDD_LOG (LOG_INFO, "handle_request: unknown command: %s.", buffer);
359 } /* }}} int handle_request */
361 static void *connection_thread_main (void *args) /* {{{ */
367 fd = *((int *) args);
369 RRDD_LOG (LOG_DEBUG, "connection_thread_main: Adding myself to "
370 "connetion_threads[]..");
371 pthread_mutex_lock (&connetion_threads_lock);
375 temp = (pthread_t *) realloc (connetion_threads,
376 sizeof (pthread_t) * (connetion_threads_num + 1));
379 RRDD_LOG (LOG_ERR, "connection_thread_main: realloc failed.");
383 connetion_threads = temp;
384 connetion_threads[connetion_threads_num] = pthread_self ();
385 connetion_threads_num++;
388 pthread_mutex_unlock (&connetion_threads_lock);
389 RRDD_LOG (LOG_DEBUG, "connection_thread_main: done");
391 while (do_shutdown == 0)
393 struct pollfd pollfd;
397 pollfd.events = POLLIN | POLLPRI;
400 status = poll (&pollfd, 1, /* timeout = */ 500);
401 if (status == 0) /* timeout */
403 else if (status < 0) /* error */
408 RRDD_LOG (LOG_ERR, "connection_thread_main: poll(2) failed.");
412 if ((pollfd.revents & POLLHUP) != 0) /* normal shutdown */
414 RRDD_LOG (LOG_DEBUG, "connection_thread_main: "
415 "poll(2) returned POLLHUP.");
419 else if ((pollfd.revents & (POLLIN | POLLPRI)) == 0)
421 RRDD_LOG (LOG_WARNING, "connection_thread_main: "
422 "poll(2) returned something unexpected: %#04hx",
428 status = handle_request (fd);
436 self = pthread_self ();
437 /* Remove this thread from the connection threads list */
438 pthread_mutex_lock (&connetion_threads_lock);
439 /* Find out own index in the array */
440 for (i = 0; i < connetion_threads_num; i++)
441 if (pthread_equal (connetion_threads[i], self) != 0)
443 assert (i < connetion_threads_num);
445 /* Move the trailing threads forward. */
446 if (i < (connetion_threads_num - 1))
448 memmove (connetion_threads + i,
449 connetion_threads + i + 1,
450 sizeof (pthread_t) * (connetion_threads_num - i - 1));
453 connetion_threads_num--;
454 pthread_mutex_unlock (&connetion_threads_lock);
458 } /* }}} void *connection_thread_main */
460 static int open_listen_socket_unix (const char *path) /* {{{ */
463 struct sockaddr_un sa;
464 listen_socket_t *temp;
467 temp = (listen_socket_t *) realloc (listen_fds,
468 sizeof (listen_fds[0]) * (listen_fds_num + 1));
471 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: realloc failed.");
475 memset (listen_fds + listen_fds_num, 0, sizeof (listen_fds[0]));
477 fd = socket (PF_UNIX, SOCK_STREAM, /* protocol = */ 0);
480 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: socket(2) failed.");
484 memset (&sa, 0, sizeof (sa));
485 sa.sun_family = AF_UNIX;
486 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
488 status = bind (fd, (struct sockaddr *) &sa, sizeof (sa));
491 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: bind(2) failed.");
497 status = listen (fd, /* backlog = */ 10);
500 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: listen(2) failed.");
506 listen_fds[listen_fds_num].fd = fd;
507 snprintf (listen_fds[listen_fds_num].path,
508 sizeof (listen_fds[listen_fds_num].path) - 1,
513 } /* }}} int open_listen_socket_unix */
515 static int open_listen_socket (const char *addr) /* {{{ */
517 struct addrinfo ai_hints;
518 struct addrinfo *ai_res;
519 struct addrinfo *ai_ptr;
522 assert (addr != NULL);
524 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
525 return (open_listen_socket_unix (addr + strlen ("unix:")));
526 else if (addr[0] == '/')
527 return (open_listen_socket_unix (addr));
529 memset (&ai_hints, 0, sizeof (ai_hints));
530 ai_hints.ai_flags = 0;
532 ai_hints.ai_flags |= AI_ADDRCONFIG;
534 ai_hints.ai_family = AF_UNSPEC;
535 ai_hints.ai_socktype = SOCK_STREAM;
538 status = getaddrinfo (addr, DEFAULT_PORT, &ai_hints, &ai_res);
541 RRDD_LOG (LOG_ERR, "open_listen_socket: getaddrinfo(%s) failed: "
542 "%s", addr, gai_strerror (status));
546 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
549 struct sockaddr_storage sa;
550 listen_socket_t *temp;
552 temp = (listen_socket_t *) realloc (listen_fds,
553 sizeof (listen_fds[0]) * (listen_fds_num + 1));
556 RRDD_LOG (LOG_ERR, "open_listen_socket: realloc failed.");
560 memset (listen_fds + listen_fds_num, 0, sizeof (listen_fds[0]));
562 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
565 RRDD_LOG (LOG_ERR, "open_listen_socket: socket(2) failed.");
569 status = bind (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
572 RRDD_LOG (LOG_ERR, "open_listen_socket: bind(2) failed.");
577 status = listen (fd, /* backlog = */ 10);
580 RRDD_LOG (LOG_ERR, "open_listen_socket: listen(2) failed.");
585 listen_fds[listen_fds_num].fd = fd;
586 strncpy (listen_fds[listen_fds_num].path, addr,
587 sizeof (listen_fds[listen_fds_num].path) - 1);
592 } /* }}} int open_listen_socket */
594 static int close_listen_sockets (void) /* {{{ */
598 for (i = 0; i < listen_fds_num; i++)
600 close (listen_fds[i].fd);
601 if (strncmp ("unix:", listen_fds[i].path, strlen ("unix:")) == 0)
602 unlink (listen_fds[i].path + strlen ("unix:"));
610 } /* }}} int close_listen_sockets */
612 static void *listen_thread_main (void *args) /* {{{ */
615 struct pollfd *pollfds;
620 for (i = 0; i < config_listen_address_list_len; i++)
622 RRDD_LOG (LOG_DEBUG, "listen_thread_main: config_listen_address_list[%i] "
623 "= %s", i, config_listen_address_list[i]);
624 open_listen_socket (config_listen_address_list[i]);
627 if (config_listen_address_list_len < 1)
628 open_listen_socket (RRDD_SOCK_PATH);
630 if (listen_fds_num < 1)
632 RRDD_LOG (LOG_ERR, "listen_thread_main: No listen sockets "
633 "could be opened. Sorry.");
637 pollfds_num = listen_fds_num;
638 pollfds = (struct pollfd *) malloc (sizeof (*pollfds) * pollfds_num);
641 RRDD_LOG (LOG_ERR, "listen_thread_main: malloc failed.");
644 memset (pollfds, 0, sizeof (*pollfds) * pollfds_num);
646 while (do_shutdown == 0)
648 assert (pollfds_num == listen_fds_num);
649 for (i = 0; i < pollfds_num; i++)
651 pollfds[i].fd = listen_fds[i].fd;
652 pollfds[i].events = POLLIN | POLLPRI;
653 pollfds[i].revents = 0;
656 status = poll (pollfds, pollfds_num, /* timeout = */ -1);
662 RRDD_LOG (LOG_ERR, "listen_thread_main: poll(2) failed.");
667 for (i = 0; i < pollfds_num; i++)
670 struct sockaddr_storage client_sa;
671 socklen_t client_sa_size;
674 if (pollfds[i].revents == 0)
677 if ((pollfds[i].revents & (POLLIN | POLLPRI)) == 0)
679 RRDD_LOG (LOG_ERR, "listen_thread_main: "
680 "poll(2) returned something unexpected for listen FD #%i.",
685 client_sd = (int *) malloc (sizeof (int));
686 if (client_sd == NULL)
688 RRDD_LOG (LOG_ERR, "listen_thread_main: malloc failed.");
692 client_sa_size = sizeof (client_sa);
693 *client_sd = accept (pollfds[i].fd,
694 (struct sockaddr *) &client_sa, &client_sa_size);
697 RRDD_LOG (LOG_ERR, "listen_thread_main: accept(2) failed.");
701 RRDD_LOG (LOG_DEBUG, "listen_thread_main: accept(2) returned fd #%i.",
704 status = pthread_create (&tid, /* attr = */ NULL, connection_thread_main,
705 /* args = */ (void *) client_sd);
708 RRDD_LOG (LOG_ERR, "listen_thread_main: pthread_create failed.");
714 RRDD_LOG (LOG_DEBUG, "listen_thread_main: pthread_create succeeded: "
716 *((unsigned long *) &tid));
717 } /* for (pollfds_num) */
718 } /* while (do_shutdown == 0) */
720 close_listen_sockets ();
722 pthread_mutex_lock (&connetion_threads_lock);
723 while (connetion_threads_num > 0)
727 wait_for = connetion_threads[0];
729 pthread_mutex_unlock (&connetion_threads_lock);
730 pthread_join (wait_for, /* retval = */ NULL);
731 pthread_mutex_lock (&connetion_threads_lock);
733 pthread_mutex_unlock (&connetion_threads_lock);
735 RRDD_LOG (LOG_DEBUG, "listen_thread_main: Exiting.");
738 } /* }}} void *listen_thread_main */
740 static int daemonize (void) /* {{{ */
749 fprintf (stderr, "daemonize: fork(2) failed.\n");
757 /* Change into the /tmp directory. */
760 /* Become session leader */
763 /* Open the first three file descriptors to /dev/null */
768 open ("/dev/null", O_RDWR);
771 #endif /* RRDD_DEBUG */
776 memset (&sa, 0, sizeof (sa));
777 sa.sa_handler = sig_int_handler;
778 sigaction (SIGINT, &sa, NULL);
780 memset (&sa, 0, sizeof (sa));
781 sa.sa_handler = sig_term_handler;
782 sigaction (SIGINT, &sa, NULL);
784 memset (&sa, 0, sizeof (sa));
785 sa.sa_handler = SIG_IGN;
786 sigaction (SIGPIPE, &sa, NULL);
789 openlog ("rrdd", LOG_PID, LOG_DAEMON);
791 cache_tree = avl_alloc_tree (cache_tree_compare, cache_tree_free);
792 if (cache_tree == NULL)
794 RRDD_LOG (LOG_ERR, "daemonize: avl_alloc_tree failed.");
798 memset (&queue_thread, 0, sizeof (queue_thread));
799 status = pthread_create (&queue_thread, /* attr = */ NULL,
800 queue_thread_main, /* args = */ NULL);
803 RRDD_LOG (LOG_ERR, "daemonize: pthread_create failed.");
808 } /* }}} int daemonize */
810 static int cleanup (void) /* {{{ */
812 RRDD_LOG (LOG_DEBUG, "cleanup ()");
816 RRDD_LOG (LOG_DEBUG, "cleanup: Joining queue_thread..");
817 pthread_cond_signal (&cache_cond);
818 pthread_join (queue_thread, /* return = */ NULL);
819 RRDD_LOG (LOG_DEBUG, "cleanup: done");
824 } /* }}} int cleanup */
826 static int read_options (int argc, char **argv) /* {{{ */
831 while ((option = getopt(argc, argv, "l:f:w:h?")) != -1)
839 temp = (char **) realloc (config_listen_address_list,
840 sizeof (char *) * (config_listen_address_list_len + 1));
843 fprintf (stderr, "read_options: realloc failed.\n");
846 config_listen_address_list = temp;
848 temp[config_listen_address_list_len] = strdup (optarg);
849 if (temp[config_listen_address_list_len] == NULL)
851 fprintf (stderr, "read_options: strdup failed.\n");
854 config_listen_address_list_len++;
862 temp = atoi (optarg);
864 config_flush_interval = temp;
867 fprintf (stderr, "Invalid flush interval: %s\n", optarg);
877 temp = atoi (optarg);
879 config_write_interval = temp;
882 fprintf (stderr, "Invalid write interval: %s\n", optarg);
890 printf ("RRDd %s Copyright (C) 2008 Florian octo Forster\n"
892 "Usage: rrdd [options]\n"
894 "Valid options are:\n"
895 " -l <address> Socket address to listen to.\n"
896 " -w <seconds> Interval in which to write data.\n"
897 " -f <seconds> Interval in which to flush dead data.\n"
899 "For more information and a detailed description of all options "
901 "to the rrdd(1) manual page.\n",
905 } /* switch (option) */
906 } /* while (getopt) */
909 } /* }}} int read_options */
911 int main (int argc, char **argv)
915 status = read_options (argc, argv);
923 status = daemonize ();
926 struct sigaction sigchld;
928 memset (&sigchld, 0, sizeof (sigchld));
929 sigchld.sa_handler = SIG_IGN;
930 sigaction (SIGCHLD, &sigchld, NULL);
934 else if (status != 0)
936 fprintf (stderr, "daemonize failed, exiting.\n");
940 listen_thread_main (NULL);
948 * vim: set sw=2 sts=2 ts=8 et fdm=marker :