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>
25 #include <glib-2.0/glib.h>
29 # define RRDD_LOG(severity, ...) do { fprintf (stderr, __VA_ARGS__); fprintf (stderr, "\n"); } while (0)
31 # define RRDD_LOG(severity, ...) syslog ((severity), __VA_ARGS__)
37 struct listen_socket_s
40 char path[PATH_MAX + 1];
42 typedef struct listen_socket_s listen_socket_t;
45 typedef struct cache_item_s cache_item_t;
51 time_t last_flush_time;
52 #define CI_FLAGS_IN_TREE 0x01
53 #define CI_FLAGS_IN_QUEUE 0x02
62 static listen_socket_t *listen_fds = NULL;
63 static size_t listen_fds_num = 0;
65 static int do_shutdown = 0;
67 static pthread_t queue_thread;
69 static pthread_t *connetion_threads = NULL;
70 static pthread_mutex_t connetion_threads_lock = PTHREAD_MUTEX_INITIALIZER;
71 static int connetion_threads_num = 0;
74 static GTree *cache_tree = NULL;
75 static cache_item_t *cache_queue_head = NULL;
76 static cache_item_t *cache_queue_tail = NULL;
77 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
78 static pthread_cond_t cache_cond = PTHREAD_COND_INITIALIZER;
80 static int config_write_interval = 300;
81 static int config_flush_interval = 3600;
83 static char **config_listen_address_list = NULL;
84 static int config_listen_address_list_len = 0;
89 static void sig_int_handler (int signal) /* {{{ */
92 } /* }}} void sig_int_handler */
94 static void sig_term_handler (int signal) /* {{{ */
97 } /* }}} void sig_term_handler */
100 * enqueue_cache_item:
101 * `cache_lock' must be acquired before calling this function!
103 static int enqueue_cache_item (cache_item_t *ci) /* {{{ */
105 RRDD_LOG (LOG_DEBUG, "handle_request_update: Adding %s to the update queue.",
111 if ((ci->flags & CI_FLAGS_IN_QUEUE) != 0)
114 assert (ci->next == NULL);
116 if (cache_queue_tail == NULL)
117 cache_queue_head = ci;
119 cache_queue_tail->next = ci;
120 cache_queue_tail = ci;
123 } /* }}} int enqueue_cache_item */
126 * tree_callback_flush:
127 * Called via `g_tree_foreach' in `queue_thread_main'. `cache_lock' is held
128 * while this is in progress.
130 static gboolean tree_callback_flush (gpointer key, gpointer value, /* {{{ */
136 ci = (cache_item_t *) value;
137 now = *((time_t *) data);
139 if (((now - ci->last_flush_time) >= config_write_interval)
140 && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0))
141 enqueue_cache_item (ci);
144 } /* }}} gboolean tree_callback_flush */
146 static void *queue_thread_main (void *args) /* {{{ */
149 struct timespec next_flush;
151 gettimeofday (&now, NULL);
152 next_flush.tv_sec = now.tv_sec + config_flush_interval;
153 next_flush.tv_nsec = 1000 * now.tv_usec;
155 pthread_mutex_lock (&cache_lock);
156 while ((do_shutdown == 0) || (cache_queue_head != NULL))
165 /* First, check if it's time to do the cache flush. */
166 gettimeofday (&now, NULL);
167 if ((now.tv_sec > next_flush.tv_sec)
168 || ((now.tv_sec == next_flush.tv_sec)
169 && ((1000 * now.tv_usec) > next_flush.tv_nsec)))
173 /* Pass the current time as user data so that we don't need to call
174 * `time' for each node. */
175 time_now = time (NULL);
177 g_tree_foreach (cache_tree, tree_callback_flush, (gpointer) &time_now);
179 /* Determine the time of the next cache flush. */
180 while (next_flush.tv_sec < now.tv_sec)
181 next_flush.tv_sec += config_flush_interval;
184 /* Now, check if there's something to store away. If not, wait until
185 * something comes in or it's time to do the cache flush. */
186 if (cache_queue_head == NULL)
188 struct timespec timeout;
190 timeout.tv_sec = next_flush.tv_sec - now.tv_sec;
191 if (next_flush.tv_nsec < (1000 * now.tv_usec))
194 timeout.tv_nsec = 1000000000 + next_flush.tv_nsec
195 - (1000 * now.tv_usec);
199 timeout.tv_nsec = next_flush.tv_nsec - (1000 * now.tv_usec);
202 pthread_cond_timedwait (&cache_cond, &cache_lock, &timeout);
205 /* Check if a value has arrived. This may be NULL if we timed out or there
206 * was an interrupt such as a signal. */
207 if (cache_queue_head == NULL)
210 ci = cache_queue_head;
212 /* copy the relevant parts */
213 file = strdup (ci->file);
216 RRDD_LOG (LOG_ERR, "queue_thread_main: strdup failed.");
221 values_num = ci->values_num;
226 ci->last_flush_time = time (NULL);
227 ci->flags &= ~(CI_FLAGS_IN_QUEUE);
229 cache_queue_head = ci->next;
230 if (cache_queue_head == NULL)
231 cache_queue_tail = NULL;
234 pthread_mutex_unlock (&cache_lock);
236 RRDD_LOG (LOG_DEBUG, "queue_thread_main: rrd_update (%s, %i, %p)",
237 file, values_num, (void *) values);
239 status = rrd_update_r (file, NULL, values_num, (void *) values);
242 RRDD_LOG (LOG_ERR, "queue_thread_main: "
243 "rrd_update_r failed with status %i.",
248 for (i = 0; i < values_num; i++)
251 pthread_mutex_lock (&cache_lock);
252 } /* while (do_shutdown == 0) */
253 pthread_mutex_unlock (&cache_lock);
255 RRDD_LOG (LOG_DEBUG, "queue_thread_main: Exiting.");
258 } /* }}} void *queue_thread_main */
260 static int handle_request_update (int fd, /* {{{ */
261 char *buffer, int buffer_size)
276 RRDD_LOG (LOG_DEBUG, "handle_request_update (%i, %p, %i)",
277 fd, (void *) buffer, buffer_size);
282 buffer_ptr += strlen (file) + 1;
284 pthread_mutex_lock (&cache_lock);
286 ci = g_tree_lookup (cache_tree, file);
289 ci = (cache_item_t *) malloc (sizeof (cache_item_t));
292 pthread_mutex_unlock (&cache_lock);
293 RRDD_LOG (LOG_ERR, "handle_request_update: malloc failed.");
296 memset (ci, 0, sizeof (cache_item_t));
298 ci->file = strdup (file);
299 if (ci->file == NULL)
301 pthread_mutex_unlock (&cache_lock);
302 RRDD_LOG (LOG_ERR, "handle_request_update: malloc failed.");
309 ci->last_flush_time = now;
310 ci->flags = CI_FLAGS_IN_TREE;
312 g_tree_insert (cache_tree, (void *) ci->file, (void *) ci);
314 RRDD_LOG (LOG_DEBUG, "handle_request_update: Created new tree node %s.",
319 while (*buffer_ptr != 0)
324 buffer_ptr += strlen (value) + 1;
326 temp = (char **) realloc (ci->values,
327 sizeof (char *) * (ci->values_num + 1));
330 RRDD_LOG (LOG_ERR, "handle_request_update: realloc failed.");
335 ci->values[ci->values_num] = strdup (value);
336 if (ci->values[ci->values_num] == NULL)
338 RRDD_LOG (LOG_ERR, "handle_request_update: strdup failed.");
346 if (((now - ci->last_flush_time) >= config_write_interval)
347 && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0))
349 enqueue_cache_item (ci);
350 pthread_cond_signal (&cache_cond);
353 pthread_mutex_unlock (&cache_lock);
355 snprintf (answer, sizeof (answer), "0 Enqueued %i value(s)\n", values_num);
356 answer[sizeof (answer) - 1] = 0;
358 status = write (fd, answer, sizeof (answer));
362 RRDD_LOG (LOG_INFO, "handle_request_update: write(2) returned an error.");
367 } /* }}} int handle_request_update */
369 static int handle_request (int fd) /* {{{ */
374 RRDD_LOG (LOG_DEBUG, "handle_request (%i)", fd);
376 buffer_size = read (fd, buffer, sizeof (buffer));
379 RRDD_LOG (LOG_ERR, "handle_request: read(2) failed.");
382 assert (((size_t) buffer_size) <= sizeof (buffer));
384 if ((buffer[buffer_size - 2] != 0)
385 || (buffer[buffer_size - 1] != 0))
387 RRDD_LOG (LOG_INFO, "handle_request: malformed request.");
391 /* fields in the buffer a separated by null bytes. */
392 if (strcmp (buffer, "update") == 0)
394 int offset = strlen ("update") + 1;
395 return (handle_request_update (fd, buffer + offset,
396 buffer_size - offset));
400 RRDD_LOG (LOG_INFO, "handle_request: unknown command: %s.", buffer);
403 } /* }}} int handle_request */
405 static void *connection_thread_main (void *args) /* {{{ */
411 fd = *((int *) args);
413 RRDD_LOG (LOG_DEBUG, "connection_thread_main: Adding myself to "
414 "connetion_threads[]..");
415 pthread_mutex_lock (&connetion_threads_lock);
419 temp = (pthread_t *) realloc (connetion_threads,
420 sizeof (pthread_t) * (connetion_threads_num + 1));
423 RRDD_LOG (LOG_ERR, "connection_thread_main: realloc failed.");
427 connetion_threads = temp;
428 connetion_threads[connetion_threads_num] = pthread_self ();
429 connetion_threads_num++;
432 pthread_mutex_unlock (&connetion_threads_lock);
433 RRDD_LOG (LOG_DEBUG, "connection_thread_main: done");
435 while (do_shutdown == 0)
437 struct pollfd pollfd;
441 pollfd.events = POLLIN | POLLPRI;
444 status = poll (&pollfd, 1, /* timeout = */ 500);
445 if (status == 0) /* timeout */
447 else if (status < 0) /* error */
452 RRDD_LOG (LOG_ERR, "connection_thread_main: poll(2) failed.");
456 if ((pollfd.revents & POLLHUP) != 0) /* normal shutdown */
458 RRDD_LOG (LOG_DEBUG, "connection_thread_main: "
459 "poll(2) returned POLLHUP.");
463 else if ((pollfd.revents & (POLLIN | POLLPRI)) == 0)
465 RRDD_LOG (LOG_WARNING, "connection_thread_main: "
466 "poll(2) returned something unexpected: %#04hx",
472 status = handle_request (fd);
480 self = pthread_self ();
481 /* Remove this thread from the connection threads list */
482 pthread_mutex_lock (&connetion_threads_lock);
483 /* Find out own index in the array */
484 for (i = 0; i < connetion_threads_num; i++)
485 if (pthread_equal (connetion_threads[i], self) != 0)
487 assert (i < connetion_threads_num);
489 /* Move the trailing threads forward. */
490 if (i < (connetion_threads_num - 1))
492 memmove (connetion_threads + i,
493 connetion_threads + i + 1,
494 sizeof (pthread_t) * (connetion_threads_num - i - 1));
497 connetion_threads_num--;
498 pthread_mutex_unlock (&connetion_threads_lock);
502 } /* }}} void *connection_thread_main */
504 static int open_listen_socket_unix (const char *path) /* {{{ */
507 struct sockaddr_un sa;
508 listen_socket_t *temp;
511 temp = (listen_socket_t *) realloc (listen_fds,
512 sizeof (listen_fds[0]) * (listen_fds_num + 1));
515 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: realloc failed.");
519 memset (listen_fds + listen_fds_num, 0, sizeof (listen_fds[0]));
521 fd = socket (PF_UNIX, SOCK_STREAM, /* protocol = */ 0);
524 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: socket(2) failed.");
528 memset (&sa, 0, sizeof (sa));
529 sa.sun_family = AF_UNIX;
530 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
532 status = bind (fd, (struct sockaddr *) &sa, sizeof (sa));
535 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: bind(2) failed.");
541 status = listen (fd, /* backlog = */ 10);
544 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: listen(2) failed.");
550 listen_fds[listen_fds_num].fd = fd;
551 snprintf (listen_fds[listen_fds_num].path,
552 sizeof (listen_fds[listen_fds_num].path) - 1,
557 } /* }}} int open_listen_socket_unix */
559 static int open_listen_socket (const char *addr) /* {{{ */
561 struct addrinfo ai_hints;
562 struct addrinfo *ai_res;
563 struct addrinfo *ai_ptr;
566 assert (addr != NULL);
568 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
569 return (open_listen_socket_unix (addr + strlen ("unix:")));
570 else if (addr[0] == '/')
571 return (open_listen_socket_unix (addr));
573 memset (&ai_hints, 0, sizeof (ai_hints));
574 ai_hints.ai_flags = 0;
576 ai_hints.ai_flags |= AI_ADDRCONFIG;
578 ai_hints.ai_family = AF_UNSPEC;
579 ai_hints.ai_socktype = SOCK_STREAM;
582 status = getaddrinfo (addr, DEFAULT_PORT, &ai_hints, &ai_res);
585 RRDD_LOG (LOG_ERR, "open_listen_socket: getaddrinfo(%s) failed: "
586 "%s", addr, gai_strerror (status));
590 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
593 listen_socket_t *temp;
595 temp = (listen_socket_t *) realloc (listen_fds,
596 sizeof (listen_fds[0]) * (listen_fds_num + 1));
599 RRDD_LOG (LOG_ERR, "open_listen_socket: realloc failed.");
603 memset (listen_fds + listen_fds_num, 0, sizeof (listen_fds[0]));
605 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
608 RRDD_LOG (LOG_ERR, "open_listen_socket: socket(2) failed.");
612 status = bind (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
615 RRDD_LOG (LOG_ERR, "open_listen_socket: bind(2) failed.");
620 status = listen (fd, /* backlog = */ 10);
623 RRDD_LOG (LOG_ERR, "open_listen_socket: listen(2) failed.");
628 listen_fds[listen_fds_num].fd = fd;
629 strncpy (listen_fds[listen_fds_num].path, addr,
630 sizeof (listen_fds[listen_fds_num].path) - 1);
635 } /* }}} int open_listen_socket */
637 static int close_listen_sockets (void) /* {{{ */
641 for (i = 0; i < listen_fds_num; i++)
643 close (listen_fds[i].fd);
644 if (strncmp ("unix:", listen_fds[i].path, strlen ("unix:")) == 0)
645 unlink (listen_fds[i].path + strlen ("unix:"));
653 } /* }}} int close_listen_sockets */
655 static void *listen_thread_main (void *args) /* {{{ */
657 struct pollfd *pollfds;
662 for (i = 0; i < config_listen_address_list_len; i++)
664 RRDD_LOG (LOG_DEBUG, "listen_thread_main: config_listen_address_list[%i] "
665 "= %s", i, config_listen_address_list[i]);
666 open_listen_socket (config_listen_address_list[i]);
669 if (config_listen_address_list_len < 1)
670 open_listen_socket (RRDD_SOCK_PATH);
672 if (listen_fds_num < 1)
674 RRDD_LOG (LOG_ERR, "listen_thread_main: No listen sockets "
675 "could be opened. Sorry.");
679 pollfds_num = listen_fds_num;
680 pollfds = (struct pollfd *) malloc (sizeof (*pollfds) * pollfds_num);
683 RRDD_LOG (LOG_ERR, "listen_thread_main: malloc failed.");
686 memset (pollfds, 0, sizeof (*pollfds) * pollfds_num);
688 while (do_shutdown == 0)
690 assert (pollfds_num == listen_fds_num);
691 for (i = 0; i < pollfds_num; i++)
693 pollfds[i].fd = listen_fds[i].fd;
694 pollfds[i].events = POLLIN | POLLPRI;
695 pollfds[i].revents = 0;
698 status = poll (pollfds, pollfds_num, /* timeout = */ -1);
704 RRDD_LOG (LOG_ERR, "listen_thread_main: poll(2) failed.");
709 for (i = 0; i < pollfds_num; i++)
712 struct sockaddr_storage client_sa;
713 socklen_t client_sa_size;
716 if (pollfds[i].revents == 0)
719 if ((pollfds[i].revents & (POLLIN | POLLPRI)) == 0)
721 RRDD_LOG (LOG_ERR, "listen_thread_main: "
722 "poll(2) returned something unexpected for listen FD #%i.",
727 client_sd = (int *) malloc (sizeof (int));
728 if (client_sd == NULL)
730 RRDD_LOG (LOG_ERR, "listen_thread_main: malloc failed.");
734 client_sa_size = sizeof (client_sa);
735 *client_sd = accept (pollfds[i].fd,
736 (struct sockaddr *) &client_sa, &client_sa_size);
739 RRDD_LOG (LOG_ERR, "listen_thread_main: accept(2) failed.");
743 RRDD_LOG (LOG_DEBUG, "listen_thread_main: accept(2) returned fd #%i.",
746 status = pthread_create (&tid, /* attr = */ NULL, connection_thread_main,
747 /* args = */ (void *) client_sd);
750 RRDD_LOG (LOG_ERR, "listen_thread_main: pthread_create failed.");
756 RRDD_LOG (LOG_DEBUG, "listen_thread_main: pthread_create succeeded: "
758 *((unsigned long *) &tid));
759 } /* for (pollfds_num) */
760 } /* while (do_shutdown == 0) */
762 close_listen_sockets ();
764 pthread_mutex_lock (&connetion_threads_lock);
765 while (connetion_threads_num > 0)
769 wait_for = connetion_threads[0];
771 pthread_mutex_unlock (&connetion_threads_lock);
772 pthread_join (wait_for, /* retval = */ NULL);
773 pthread_mutex_lock (&connetion_threads_lock);
775 pthread_mutex_unlock (&connetion_threads_lock);
777 RRDD_LOG (LOG_DEBUG, "listen_thread_main: Exiting.");
780 } /* }}} void *listen_thread_main */
782 static int daemonize (void) /* {{{ */
793 fprintf (stderr, "daemonize: fork(2) failed.\n");
801 /* Change into the /tmp directory. */
804 /* Become session leader */
807 /* Open the first three file descriptors to /dev/null */
812 open ("/dev/null", O_RDWR);
815 #endif /* RRDD_DEBUG */
820 memset (&sa, 0, sizeof (sa));
821 sa.sa_handler = sig_int_handler;
822 sigaction (SIGINT, &sa, NULL);
824 memset (&sa, 0, sizeof (sa));
825 sa.sa_handler = sig_term_handler;
826 sigaction (SIGINT, &sa, NULL);
828 memset (&sa, 0, sizeof (sa));
829 sa.sa_handler = SIG_IGN;
830 sigaction (SIGPIPE, &sa, NULL);
833 openlog ("rrdd", LOG_PID, LOG_DAEMON);
835 cache_tree = g_tree_new ((GCompareFunc) strcmp);
836 if (cache_tree == NULL)
838 RRDD_LOG (LOG_ERR, "daemonize: g_tree_new failed.");
842 memset (&queue_thread, 0, sizeof (queue_thread));
843 status = pthread_create (&queue_thread, /* attr = */ NULL,
844 queue_thread_main, /* args = */ NULL);
847 RRDD_LOG (LOG_ERR, "daemonize: pthread_create failed.");
852 } /* }}} int daemonize */
854 static int cleanup (void) /* {{{ */
856 RRDD_LOG (LOG_DEBUG, "cleanup ()");
860 RRDD_LOG (LOG_DEBUG, "cleanup: Joining queue_thread..");
861 pthread_cond_signal (&cache_cond);
862 pthread_join (queue_thread, /* return = */ NULL);
863 RRDD_LOG (LOG_DEBUG, "cleanup: done");
868 } /* }}} int cleanup */
870 static int read_options (int argc, char **argv) /* {{{ */
875 while ((option = getopt(argc, argv, "l:f:w:h?")) != -1)
883 temp = (char **) realloc (config_listen_address_list,
884 sizeof (char *) * (config_listen_address_list_len + 1));
887 fprintf (stderr, "read_options: realloc failed.\n");
890 config_listen_address_list = temp;
892 temp[config_listen_address_list_len] = strdup (optarg);
893 if (temp[config_listen_address_list_len] == NULL)
895 fprintf (stderr, "read_options: strdup failed.\n");
898 config_listen_address_list_len++;
906 temp = atoi (optarg);
908 config_flush_interval = temp;
911 fprintf (stderr, "Invalid flush interval: %s\n", optarg);
921 temp = atoi (optarg);
923 config_write_interval = temp;
926 fprintf (stderr, "Invalid write interval: %s\n", optarg);
934 printf ("RRDd %s Copyright (C) 2008 Florian octo Forster\n"
936 "Usage: rrdd [options]\n"
938 "Valid options are:\n"
939 " -l <address> Socket address to listen to.\n"
940 " -w <seconds> Interval in which to write data.\n"
941 " -f <seconds> Interval in which to flush dead data.\n"
943 "For more information and a detailed description of all options "
945 "to the rrdd(1) manual page.\n",
949 } /* switch (option) */
950 } /* while (getopt) */
953 } /* }}} int read_options */
955 int main (int argc, char **argv)
959 status = read_options (argc, argv);
967 status = daemonize ();
970 struct sigaction sigchld;
972 memset (&sigchld, 0, sizeof (sigchld));
973 sigchld.sa_handler = SIG_IGN;
974 sigaction (SIGCHLD, &sigchld, NULL);
978 else if (status != 0)
980 fprintf (stderr, "daemonize failed, exiting.\n");
984 listen_thread_main (NULL);
992 * vim: set sw=2 sts=2 ts=8 et fdm=marker :