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__)
32 #define DEFAULT_PORT "42217"
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 avl_tree_t *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 */
99 static int cache_tree_compare (const void *v0, const void *v1) /* {{{ */
101 cache_item_t *c0 = (cache_item_t *) v0;
102 cache_item_t *c1 = (cache_item_t *) v1;
104 assert (c0->file != NULL);
105 assert (c1->file != NULL);
107 return (strcmp (c0->file, c1->file));
108 } /* }}} int cache_tree_compare */
110 static void cache_tree_free (void *v) /* {{{ */
112 cache_item_t *c = (cache_item_t *) v;
114 assert (c->values_num == 0);
115 assert ((c->flags & CI_FLAGS_IN_TREE) != 0);
116 assert ((c->flags & CI_FLAGS_IN_QUEUE) == 0);
121 } /* }}} void cache_tree_free */
123 static void *queue_thread_main (void *args) /* {{{ */
125 pthread_mutex_lock (&cache_lock);
126 while ((do_shutdown == 0) || (cache_queue_head != NULL))
136 if (cache_queue_head == NULL)
137 pthread_cond_wait (&cache_cond, &cache_lock);
139 if (cache_queue_head == NULL)
142 ci = cache_queue_head;
144 /* copy the relevant parts */
145 file = strdup (ci->file);
148 RRDD_LOG (LOG_ERR, "queue_thread_main: strdup failed.");
153 values_num = ci->values_num;
158 ci->last_flush_time = time (NULL);
159 ci->flags &= ~(CI_FLAGS_IN_QUEUE);
161 cache_queue_head = ci->next;
162 if (cache_queue_head == NULL)
163 cache_queue_tail = NULL;
166 pthread_mutex_unlock (&cache_lock);
168 RRDD_LOG (LOG_DEBUG, "queue_thread_main: rrd_update (%s, %i, %p)",
169 file, values_num, (void *) values);
171 status = rrd_update_r (file, NULL, values_num, (void *) values);
174 RRDD_LOG (LOG_ERR, "queue_thread_main: "
175 "rrd_update_r failed with status %i.",
180 for (i = 0; i < values_num; i++)
183 pthread_mutex_lock (&cache_lock);
184 } /* while (do_shutdown == 0) */
185 pthread_mutex_unlock (&cache_lock);
187 RRDD_LOG (LOG_DEBUG, "queue_thread_main: Exiting.");
190 } /* }}} void *queue_thread_main */
192 static int handle_request_update (int fd, /* {{{ */
193 char *buffer, int buffer_size)
204 cache_item_t ci_temp;
211 RRDD_LOG (LOG_DEBUG, "handle_request_update (%i, %p, %i)",
212 fd, (void *) buffer, buffer_size);
217 buffer_ptr += strlen (file) + 1;
221 pthread_mutex_lock (&cache_lock);
223 node = avl_search (cache_tree, (void *) &ci_temp);
226 ci = (cache_item_t *) malloc (sizeof (cache_item_t));
229 pthread_mutex_unlock (&cache_lock);
230 RRDD_LOG (LOG_ERR, "handle_request_update: malloc failed.");
233 memset (ci, 0, sizeof (cache_item_t));
235 ci->file = strdup (file);
236 if (ci->file == NULL)
238 pthread_mutex_unlock (&cache_lock);
239 RRDD_LOG (LOG_ERR, "handle_request_update: malloc failed.");
246 ci->last_flush_time = now;
247 ci->flags = CI_FLAGS_IN_TREE;
249 if (avl_insert (cache_tree, (void *) ci) == NULL)
251 pthread_mutex_unlock (&cache_lock);
252 RRDD_LOG (LOG_ERR, "handle_request_update: avl_insert failed.");
258 RRDD_LOG (LOG_DEBUG, "handle_request_update: Created new AVL node %s.",
261 else /* if (ci != NULL) */
263 ci = (cache_item_t *) node->item;
267 while (*buffer_ptr != 0)
272 buffer_ptr += strlen (value) + 1;
274 temp = (char **) realloc (ci->values,
275 sizeof (char *) * (ci->values_num + 1));
278 RRDD_LOG (LOG_ERR, "handle_request_update: realloc failed.");
283 ci->values[ci->values_num] = strdup (value);
284 if (ci->values[ci->values_num] == NULL)
286 RRDD_LOG (LOG_ERR, "handle_request_update: strdup failed.");
294 if (((now - ci->last_flush_time) >= config_write_interval)
295 && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0))
297 RRDD_LOG (LOG_DEBUG, "handle_request_update: Adding %s to the update queue.",
300 assert (ci->next == NULL);
302 if (cache_queue_tail == NULL)
303 cache_queue_head = ci;
305 cache_queue_tail->next = ci;
306 cache_queue_tail = ci;
308 pthread_cond_signal (&cache_cond);
311 pthread_mutex_unlock (&cache_lock);
313 snprintf (answer, sizeof (answer), "0 Enqueued %i value(s)\n", values_num);
314 answer[sizeof (answer) - 1] = 0;
316 status = write (fd, answer, sizeof (answer));
320 RRDD_LOG (LOG_INFO, "handle_request_update: write(2) returned an error.");
325 } /* }}} int handle_request_update */
327 static int handle_request (int fd) /* {{{ */
332 RRDD_LOG (LOG_DEBUG, "handle_request (%i)", fd);
334 buffer_size = read (fd, buffer, sizeof (buffer));
337 RRDD_LOG (LOG_ERR, "handle_request: read(2) failed.");
340 assert (((size_t) buffer_size) <= sizeof (buffer));
342 if ((buffer[buffer_size - 2] != 0)
343 || (buffer[buffer_size - 1] != 0))
345 RRDD_LOG (LOG_INFO, "handle_request: malformed request.");
349 /* fields in the buffer a separated by null bytes. */
350 if (strcmp (buffer, "update") == 0)
352 int offset = strlen ("update") + 1;
353 return (handle_request_update (fd, buffer + offset,
354 buffer_size - offset));
358 RRDD_LOG (LOG_INFO, "handle_request: unknown command: %s.", buffer);
361 } /* }}} int handle_request */
363 static void *connection_thread_main (void *args) /* {{{ */
369 fd = *((int *) args);
371 RRDD_LOG (LOG_DEBUG, "connection_thread_main: Adding myself to "
372 "connetion_threads[]..");
373 pthread_mutex_lock (&connetion_threads_lock);
377 temp = (pthread_t *) realloc (connetion_threads,
378 sizeof (pthread_t) * (connetion_threads_num + 1));
381 RRDD_LOG (LOG_ERR, "connection_thread_main: realloc failed.");
385 connetion_threads = temp;
386 connetion_threads[connetion_threads_num] = pthread_self ();
387 connetion_threads_num++;
390 pthread_mutex_unlock (&connetion_threads_lock);
391 RRDD_LOG (LOG_DEBUG, "connection_thread_main: done");
393 while (do_shutdown == 0)
395 struct pollfd pollfd;
399 pollfd.events = POLLIN | POLLPRI;
402 status = poll (&pollfd, 1, /* timeout = */ 500);
403 if (status == 0) /* timeout */
405 else if (status < 0) /* error */
410 RRDD_LOG (LOG_ERR, "connection_thread_main: poll(2) failed.");
414 if ((pollfd.revents & POLLHUP) != 0) /* normal shutdown */
416 RRDD_LOG (LOG_DEBUG, "connection_thread_main: "
417 "poll(2) returned POLLHUP.");
421 else if ((pollfd.revents & (POLLIN | POLLPRI)) == 0)
423 RRDD_LOG (LOG_WARNING, "connection_thread_main: "
424 "poll(2) returned something unexpected: %#04hx",
430 status = handle_request (fd);
438 self = pthread_self ();
439 /* Remove this thread from the connection threads list */
440 pthread_mutex_lock (&connetion_threads_lock);
441 /* Find out own index in the array */
442 for (i = 0; i < connetion_threads_num; i++)
443 if (pthread_equal (connetion_threads[i], self) != 0)
445 assert (i < connetion_threads_num);
447 /* Move the trailing threads forward. */
448 if (i < (connetion_threads_num - 1))
450 memmove (connetion_threads + i,
451 connetion_threads + i + 1,
452 sizeof (pthread_t) * (connetion_threads_num - i - 1));
455 connetion_threads_num--;
456 pthread_mutex_unlock (&connetion_threads_lock);
460 } /* }}} void *connection_thread_main */
462 static int open_listen_socket_unix (const char *path) /* {{{ */
465 struct sockaddr_un sa;
466 listen_socket_t *temp;
469 temp = (listen_socket_t *) realloc (listen_fds,
470 sizeof (listen_fds[0]) * (listen_fds_num + 1));
473 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: realloc failed.");
477 memset (listen_fds + listen_fds_num, 0, sizeof (listen_fds[0]));
479 fd = socket (PF_UNIX, SOCK_STREAM, /* protocol = */ 0);
482 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: socket(2) failed.");
486 memset (&sa, 0, sizeof (sa));
487 sa.sun_family = AF_UNIX;
488 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
490 status = bind (fd, (struct sockaddr *) &sa, sizeof (sa));
493 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: bind(2) failed.");
499 status = listen (fd, /* backlog = */ 10);
502 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: listen(2) failed.");
508 listen_fds[listen_fds_num].fd = fd;
509 snprintf (listen_fds[listen_fds_num].path,
510 sizeof (listen_fds[listen_fds_num].path) - 1,
515 } /* }}} int open_listen_socket_unix */
517 static int open_listen_socket (const char *addr) /* {{{ */
519 struct addrinfo ai_hints;
520 struct addrinfo *ai_res;
521 struct addrinfo *ai_ptr;
524 assert (addr != NULL);
526 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
527 return (open_listen_socket_unix (addr + strlen ("unix:")));
528 else if (addr[0] == '/')
529 return (open_listen_socket_unix (addr));
531 memset (&ai_hints, 0, sizeof (ai_hints));
532 ai_hints.ai_flags = 0;
534 ai_hints.ai_flags |= AI_ADDRCONFIG;
536 ai_hints.ai_family = AF_UNSPEC;
537 ai_hints.ai_socktype = SOCK_STREAM;
540 status = getaddrinfo (addr, DEFAULT_PORT, &ai_hints, &ai_res);
543 RRDD_LOG (LOG_ERR, "open_listen_socket: getaddrinfo(%s) failed: "
544 "%s", addr, gai_strerror (status));
548 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
551 struct sockaddr_storage sa;
552 listen_socket_t *temp;
554 temp = (listen_socket_t *) realloc (listen_fds,
555 sizeof (listen_fds[0]) * (listen_fds_num + 1));
558 RRDD_LOG (LOG_ERR, "open_listen_socket: realloc failed.");
562 memset (listen_fds + listen_fds_num, 0, sizeof (listen_fds[0]));
564 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
567 RRDD_LOG (LOG_ERR, "open_listen_socket: socket(2) failed.");
571 status = bind (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
574 RRDD_LOG (LOG_ERR, "open_listen_socket: bind(2) failed.");
579 status = listen (fd, /* backlog = */ 10);
582 RRDD_LOG (LOG_ERR, "open_listen_socket: listen(2) failed.");
587 listen_fds[listen_fds_num].fd = fd;
588 strncpy (listen_fds[listen_fds_num].path, addr,
589 sizeof (listen_fds[listen_fds_num].path) - 1);
594 } /* }}} int open_listen_socket */
596 static int close_listen_sockets (void) /* {{{ */
600 for (i = 0; i < listen_fds_num; i++)
602 close (listen_fds[i].fd);
603 if (strncmp ("unix:", listen_fds[i].path, strlen ("unix:")) == 0)
604 unlink (listen_fds[i].path + strlen ("unix:"));
612 } /* }}} int close_listen_sockets */
614 static void *listen_thread_main (void *args) /* {{{ */
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 (listen_fds_num < 1)
629 RRDD_LOG (LOG_ERR, "listen_thread_main: No listen sockets "
630 "could be opened. Sorry.");
634 while (do_shutdown == 0)
637 struct sockaddr_storage client_sa;
638 socklen_t client_sa_size;
641 client_sd = (int *) malloc (sizeof (int));
642 if (client_sd == NULL)
644 RRDD_LOG (LOG_ERR, "listen_thread_main: malloc failed.");
649 client_sa_size = sizeof (client_sa);
650 /* FIXME: Don't implement listen_fds as a list or use poll(2) here! */
651 *client_sd = accept (listen_fds[0].fd,
652 (struct sockaddr *) &client_sa, &client_sa_size);
655 RRDD_LOG (LOG_ERR, "listen_thread_main: accept(2) failed.");
659 RRDD_LOG (LOG_DEBUG, "listen_thread_main: accept(2) returned fd #%i.",
662 status = pthread_create (&tid, /* attr = */ NULL, connection_thread_main,
663 /* args = */ (void *) client_sd);
666 RRDD_LOG (LOG_ERR, "listen_thread_main: pthread_create failed.");
672 RRDD_LOG (LOG_DEBUG, "listen_thread_main: pthread_create succeeded: "
674 *((unsigned long *) &tid));
675 } /* while (do_shutdown == 0) */
677 close_listen_sockets ();
679 pthread_mutex_lock (&connetion_threads_lock);
680 while (connetion_threads_num > 0)
684 wait_for = connetion_threads[0];
686 pthread_mutex_unlock (&connetion_threads_lock);
687 pthread_join (wait_for, /* retval = */ NULL);
688 pthread_mutex_lock (&connetion_threads_lock);
690 pthread_mutex_unlock (&connetion_threads_lock);
692 RRDD_LOG (LOG_DEBUG, "listen_thread_main: Exiting.");
695 } /* }}} void *listen_thread_main */
697 static int daemonize (void) /* {{{ */
706 fprintf (stderr, "daemonize: fork(2) failed.\n");
714 /* Change into the /tmp directory. */
717 /* Become session leader */
720 /* Open the first three file descriptors to /dev/null */
725 open ("/dev/null", O_RDWR);
728 #endif /* RRDD_DEBUG */
733 memset (&sa, 0, sizeof (sa));
734 sa.sa_handler = sig_int_handler;
735 sigaction (SIGINT, &sa, NULL);
737 memset (&sa, 0, sizeof (sa));
738 sa.sa_handler = sig_term_handler;
739 sigaction (SIGINT, &sa, NULL);
741 memset (&sa, 0, sizeof (sa));
742 sa.sa_handler = SIG_IGN;
743 sigaction (SIGPIPE, &sa, NULL);
746 openlog ("rrdd", LOG_PID, LOG_DAEMON);
748 cache_tree = avl_alloc_tree (cache_tree_compare, cache_tree_free);
749 if (cache_tree == NULL)
751 RRDD_LOG (LOG_ERR, "daemonize: avl_alloc_tree failed.");
755 memset (&queue_thread, 0, sizeof (queue_thread));
756 status = pthread_create (&queue_thread, /* attr = */ NULL,
757 queue_thread_main, /* args = */ NULL);
760 RRDD_LOG (LOG_ERR, "daemonize: pthread_create failed.");
765 } /* }}} int daemonize */
767 static int cleanup (void) /* {{{ */
769 RRDD_LOG (LOG_DEBUG, "cleanup ()");
773 RRDD_LOG (LOG_DEBUG, "cleanup: Joining queue_thread..");
774 pthread_cond_signal (&cache_cond);
775 pthread_join (queue_thread, /* return = */ NULL);
776 RRDD_LOG (LOG_DEBUG, "cleanup: done");
781 } /* }}} int cleanup */
783 static int read_options (int argc, char **argv) /* {{{ */
788 while ((option = getopt(argc, argv, "l:f:w:h?")) != -1)
796 temp = (char **) realloc (config_listen_address_list,
797 sizeof (char *) * (config_listen_address_list_len + 1));
800 fprintf (stderr, "read_options: realloc failed.\n");
803 config_listen_address_list = temp;
805 temp[config_listen_address_list_len] = strdup (optarg);
806 if (temp[config_listen_address_list_len] == NULL)
808 fprintf (stderr, "read_options: strdup failed.\n");
811 config_listen_address_list_len++;
819 temp = atoi (optarg);
821 config_flush_interval = temp;
824 fprintf (stderr, "Invalid flush interval: %s\n", optarg);
834 temp = atoi (optarg);
836 config_write_interval = temp;
839 fprintf (stderr, "Invalid write interval: %s\n", optarg);
847 printf ("RRDd %s Copyright (C) 2008 Florian octo Forster\n"
849 "Usage: rrdd [options]\n"
851 "Valid options are:\n"
852 " -l <address> Socket address to listen to.\n"
853 " -w <seconds> Interval in which to write data.\n"
854 " -f <seconds> Interval in which to flush dead data.\n"
856 "For more information and a detailed description of all options "
858 "to the rrdd(1) manual page.\n",
862 } /* switch (option) */
863 } /* while (getopt) */
866 } /* }}} int read_options */
868 int main (int argc, char **argv)
872 status = read_options (argc, argv);
880 status = daemonize ();
883 struct sigaction sigchld;
885 memset (&sigchld, 0, sizeof (sigchld));
886 sigchld.sa_handler = SIG_IGN;
887 sigaction (SIGCHLD, &sigchld, NULL);
891 else if (status != 0)
893 fprintf (stderr, "daemonize failed, exiting.\n");
897 listen_thread_main (NULL);
905 * vim: set sw=2 sts=2 ts=8 et fdm=marker :