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;
84 static void sig_int_handler (int signal) /* {{{ */
87 } /* }}} void sig_int_handler */
89 static void sig_term_handler (int signal) /* {{{ */
92 } /* }}} void sig_term_handler */
94 static int cache_tree_compare (const void *v0, const void *v1) /* {{{ */
96 cache_item_t *c0 = (cache_item_t *) v0;
97 cache_item_t *c1 = (cache_item_t *) v1;
99 assert (c0->file != NULL);
100 assert (c1->file != NULL);
102 return (strcmp (c0->file, c1->file));
103 } /* }}} int cache_tree_compare */
105 static void cache_tree_free (void *v) /* {{{ */
107 cache_item_t *c = (cache_item_t *) v;
109 assert (c->values_num == 0);
110 assert ((c->flags & CI_FLAGS_IN_TREE) != 0);
111 assert ((c->flags & CI_FLAGS_IN_QUEUE) == 0);
116 } /* }}} void cache_tree_free */
118 static void *queue_thread_main (void *args) /* {{{ */
120 pthread_mutex_lock (&cache_lock);
121 while ((do_shutdown == 0) || (cache_queue_head != NULL))
131 if (cache_queue_head == NULL)
132 pthread_cond_wait (&cache_cond, &cache_lock);
134 if (cache_queue_head == NULL)
137 ci = cache_queue_head;
139 /* copy the relevant parts */
140 file = strdup (ci->file);
143 RRDD_LOG (LOG_ERR, "queue_thread_main: strdup failed.");
148 values_num = ci->values_num;
153 ci->last_flush_time = time (NULL);
154 ci->flags &= ~(CI_FLAGS_IN_QUEUE);
156 cache_queue_head = ci->next;
157 if (cache_queue_head == NULL)
158 cache_queue_tail = NULL;
161 pthread_mutex_unlock (&cache_lock);
163 RRDD_LOG (LOG_DEBUG, "queue_thread_main: rrd_update (%s, %i, %p)",
164 file, values_num, (void *) values);
166 status = rrd_update_r (file, NULL, values_num, values);
169 RRDD_LOG (LOG_ERR, "queue_thread_main: "
170 "rrd_update_r failed with status %i.",
175 for (i = 0; i < values_num; i++)
178 pthread_mutex_lock (&cache_lock);
179 } /* while (do_shutdown == 0) */
180 pthread_mutex_unlock (&cache_lock);
182 RRDD_LOG (LOG_DEBUG, "queue_thread_main: Exiting.");
185 } /* }}} void *queue_thread_main */
187 static int handle_request_update (int fd, /* {{{ */
188 char *buffer, int buffer_size)
199 cache_item_t ci_temp;
206 RRDD_LOG (LOG_DEBUG, "handle_request_update (%i, %p, %i)",
207 fd, (void *) buffer, buffer_size);
212 buffer_ptr += strlen (file) + 1;
216 pthread_mutex_lock (&cache_lock);
218 node = avl_search (cache_tree, (void *) &ci_temp);
221 ci = (cache_item_t *) malloc (sizeof (cache_item_t));
224 pthread_mutex_unlock (&cache_lock);
225 RRDD_LOG (LOG_ERR, "handle_request_update: malloc failed.");
228 memset (ci, 0, sizeof (cache_item_t));
230 ci->file = strdup (file);
231 if (ci->file == NULL)
233 pthread_mutex_unlock (&cache_lock);
234 RRDD_LOG (LOG_ERR, "handle_request_update: malloc failed.");
241 ci->last_flush_time = now;
242 ci->flags = CI_FLAGS_IN_TREE;
244 if (avl_insert (cache_tree, (void *) ci) == NULL)
246 pthread_mutex_unlock (&cache_lock);
247 RRDD_LOG (LOG_ERR, "handle_request_update: avl_insert failed.");
253 RRDD_LOG (LOG_DEBUG, "handle_request_update: Created new AVL node %s.",
256 else /* if (ci != NULL) */
258 ci = (cache_item_t *) node->item;
262 while (*buffer_ptr != 0)
267 buffer_ptr += strlen (value) + 1;
269 temp = (char **) realloc (ci->values,
270 sizeof (char *) * (ci->values_num + 1));
273 RRDD_LOG (LOG_ERR, "handle_request_update: realloc failed.");
278 ci->values[ci->values_num] = strdup (value);
279 if (ci->values[ci->values_num] == NULL)
281 RRDD_LOG (LOG_ERR, "handle_request_update: strdup failed.");
289 if (((now - ci->last_flush_time) >= config_write_interval)
290 && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0))
292 RRDD_LOG (LOG_DEBUG, "handle_request_update: Adding %s to the update queue.",
295 assert (ci->next == NULL);
297 if (cache_queue_tail == NULL)
298 cache_queue_head = ci;
300 cache_queue_tail->next = ci;
301 cache_queue_tail = ci;
303 pthread_cond_signal (&cache_cond);
306 pthread_mutex_unlock (&cache_lock);
308 snprintf (answer, sizeof (answer), "0 Enqueued %i value(s)\n", values_num);
309 answer[sizeof (answer) - 1] = 0;
311 status = write (fd, answer, sizeof (answer));
315 RRDD_LOG (LOG_INFO, "handle_request_update: write(2) returned an error.");
320 } /* }}} int handle_request_update */
322 static int handle_request (int fd) /* {{{ */
327 RRDD_LOG (LOG_DEBUG, "handle_request (%i)", fd);
329 buffer_size = read (fd, buffer, sizeof (buffer));
332 RRDD_LOG (LOG_ERR, "handle_request: read(2) failed.");
335 assert (((size_t) buffer_size) <= sizeof (buffer));
337 if ((buffer[buffer_size - 2] != 0)
338 || (buffer[buffer_size - 1] != 0))
340 RRDD_LOG (LOG_INFO, "handle_request: malformed request.");
344 /* fields in the buffer a separated by null bytes. */
345 if (strcmp (buffer, "update") == 0)
347 int offset = strlen ("update") + 1;
348 return (handle_request_update (fd, buffer + offset,
349 buffer_size - offset));
353 RRDD_LOG (LOG_INFO, "handle_request: unknown command: %s.", buffer);
356 } /* }}} int handle_request */
358 static void *connection_thread_main (void *args) /* {{{ */
364 fd = *((int *) args);
366 RRDD_LOG (LOG_DEBUG, "connection_thread_main: Adding myself to "
367 "connetion_threads[]..");
368 pthread_mutex_lock (&connetion_threads_lock);
372 temp = (pthread_t *) realloc (connetion_threads,
373 sizeof (pthread_t) * (connetion_threads_num + 1));
376 RRDD_LOG (LOG_ERR, "connection_thread_main: realloc failed.");
380 connetion_threads = temp;
381 connetion_threads[connetion_threads_num] = pthread_self ();
382 connetion_threads_num++;
385 pthread_mutex_unlock (&connetion_threads_lock);
386 RRDD_LOG (LOG_DEBUG, "connection_thread_main: done");
388 while (do_shutdown == 0)
390 struct pollfd pollfd;
394 pollfd.events = POLLIN | POLLPRI;
397 status = poll (&pollfd, 1, /* timeout = */ 500);
398 if (status == 0) /* timeout */
400 else if (status < 0) /* error */
405 RRDD_LOG (LOG_ERR, "connection_thread_main: poll(2) failed.");
409 if ((pollfd.revents & POLLHUP) != 0) /* normal shutdown */
411 RRDD_LOG (LOG_DEBUG, "connection_thread_main: "
412 "poll(2) returned POLLHUP.");
416 else if ((pollfd.revents & (POLLIN | POLLPRI)) == 0)
418 RRDD_LOG (LOG_WARNING, "connection_thread_main: "
419 "poll(2) returned something unexpected: %#04hx",
425 status = handle_request (fd);
433 self = pthread_self ();
434 /* Remove this thread from the connection threads list */
435 pthread_mutex_lock (&connetion_threads_lock);
436 /* Find out own index in the array */
437 for (i = 0; i < connetion_threads_num; i++)
438 if (pthread_equal (connetion_threads[i], self) != 0)
440 assert (i < connetion_threads_num);
442 /* Move the trailing threads forward. */
443 if (i < (connetion_threads_num - 1))
445 memmove (connetion_threads + i,
446 connetion_threads + i + 1,
447 sizeof (pthread_t) * (connetion_threads_num - i - 1));
450 connetion_threads_num--;
451 pthread_mutex_unlock (&connetion_threads_lock);
455 } /* }}} void *connection_thread_main */
457 static int open_listen_socket (const char *path) /* {{{ */
460 struct sockaddr_un sa;
461 listen_socket_t *temp;
464 temp = (listen_socket_t *) realloc (listen_fds,
465 sizeof (listen_fds[0]) * (listen_fds_num + 1));
468 RRDD_LOG (LOG_ERR, "open_listen_socket: realloc failed.");
472 memset (listen_fds + listen_fds_num, 0, sizeof (listen_fds[0]));
474 fd = socket (PF_UNIX, SOCK_STREAM, /* protocol = */ 0);
477 RRDD_LOG (LOG_ERR, "open_listen_socket: socket(2) failed.");
481 memset (&sa, 0, sizeof (sa));
482 sa.sun_family = AF_UNIX;
483 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
485 status = bind (fd, (struct sockaddr *) &sa, sizeof (sa));
488 RRDD_LOG (LOG_ERR, "open_listen_socket: bind(2) failed.");
494 status = listen (fd, /* backlog = */ 10);
497 RRDD_LOG (LOG_ERR, "open_listen_socket: listen(2) failed.");
503 listen_fds[listen_fds_num].fd = fd;
504 strncpy (listen_fds[listen_fds_num].path, path,
505 sizeof (listen_fds[listen_fds_num].path) - 1);
509 } /* }}} int open_listen_socket */
511 static int close_listen_sockets (void) /* {{{ */
515 for (i = 0; i < listen_fds_num; i++)
517 close (listen_fds[i].fd);
518 unlink (listen_fds[i].path);
526 } /* }}} int close_listen_sockets */
528 static void *listen_thread_main (void *args) /* {{{ */
534 status = open_listen_socket (RRDD_SOCK_PATH);
537 RRDD_LOG (LOG_ERR, "listen_thread_main: open_listen_socket failed.");
541 while (do_shutdown == 0)
544 struct sockaddr_un client_sa;
545 socklen_t client_sa_size;
548 client_sd = (int *) malloc (sizeof (int));
549 if (client_sd == NULL)
551 RRDD_LOG (LOG_ERR, "listen_thread_main: malloc failed.");
556 client_sa_size = sizeof (client_sa);
557 /* FIXME: Don't implement listen_fds as a list or use poll(2) here! */
558 *client_sd = accept (listen_fds[0].fd,
559 (struct sockaddr *) &client_sa, &client_sa_size);
562 RRDD_LOG (LOG_ERR, "listen_thread_main: accept(2) failed.");
566 RRDD_LOG (LOG_DEBUG, "listen_thread_main: accept(2) returned fd #%i.",
569 status = pthread_create (&tid, /* attr = */ NULL, connection_thread_main,
570 /* args = */ (void *) client_sd);
573 RRDD_LOG (LOG_ERR, "listen_thread_main: pthread_create failed.");
579 RRDD_LOG (LOG_DEBUG, "listen_thread_main: pthread_create succeeded: "
581 *((unsigned long *) &tid));
582 } /* while (do_shutdown == 0) */
584 close_listen_sockets ();
586 pthread_mutex_lock (&connetion_threads_lock);
587 while (connetion_threads_num > 0)
591 wait_for = connetion_threads[0];
593 pthread_mutex_unlock (&connetion_threads_lock);
594 pthread_join (wait_for, /* retval = */ NULL);
595 pthread_mutex_lock (&connetion_threads_lock);
597 pthread_mutex_unlock (&connetion_threads_lock);
599 RRDD_LOG (LOG_DEBUG, "listen_thread_main: Exiting.");
602 } /* }}} void *listen_thread_main */
604 static int daemonize (void) /* {{{ */
613 fprintf (stderr, "daemonize: fork(2) failed.\n");
621 /* Change into the /tmp directory. */
624 /* Become session leader */
627 /* Open the first three file descriptors to /dev/null */
632 open ("/dev/null", O_RDWR);
635 #endif /* RRDD_DEBUG */
640 memset (&sa, 0, sizeof (sa));
641 sa.sa_handler = sig_int_handler;
642 sigaction (SIGINT, &sa, NULL);
644 memset (&sa, 0, sizeof (sa));
645 sa.sa_handler = sig_term_handler;
646 sigaction (SIGINT, &sa, NULL);
648 memset (&sa, 0, sizeof (sa));
649 sa.sa_handler = SIG_IGN;
650 sigaction (SIGPIPE, &sa, NULL);
653 openlog ("rrdd", LOG_PID, LOG_DAEMON);
655 cache_tree = avl_alloc_tree (cache_tree_compare, cache_tree_free);
656 if (cache_tree == NULL)
658 RRDD_LOG (LOG_ERR, "daemonize: avl_alloc_tree failed.");
662 memset (&queue_thread, 0, sizeof (queue_thread));
663 status = pthread_create (&queue_thread, /* attr = */ NULL,
664 queue_thread_main, /* args = */ NULL);
667 RRDD_LOG (LOG_ERR, "daemonize: pthread_create failed.");
672 } /* }}} int daemonize */
674 static int cleanup (void) /* {{{ */
676 RRDD_LOG (LOG_DEBUG, "cleanup ()");
680 RRDD_LOG (LOG_DEBUG, "cleanup: Joining queue_thread..");
681 pthread_cond_signal (&cache_cond);
682 pthread_join (queue_thread, /* return = */ NULL);
683 RRDD_LOG (LOG_DEBUG, "cleanup: done");
688 } /* }}} int cleanup */
690 static int read_options (int argc, char **argv) /* {{{ */
695 while ((option = getopt(argc, argv, "l:f:w:h?")) != -1)
701 printf ("Listening to: %s\n", optarg);
709 temp = atoi (optarg);
711 config_flush_interval = temp;
714 fprintf (stderr, "Invalid flush interval: %s\n", optarg);
724 temp = atoi (optarg);
726 config_write_interval = temp;
729 fprintf (stderr, "Invalid write interval: %s\n", optarg);
737 printf ("RRDd %s Copyright (C) 2008 Florian octo Forster\n"
739 "Usage: rrdd [options]\n"
741 "Valid options are:\n"
742 " -l <address> Socket address to listen to.\n"
743 " -w <seconds> Interval in which to write data.\n"
744 " -f <seconds> Interval in which to flush dead data.\n"
746 "For more information and a detailed description of all options "
748 "to the rrdd(1) manual page.\n",
752 } /* switch (option) */
753 } /* while (getopt) */
756 } /* }}} int read_options */
758 int main (int argc, char **argv)
762 status = read_options (argc, argv);
770 status = daemonize ();
773 struct sigaction sigchld;
775 memset (&sigchld, 0, sizeof (sigchld));
776 sigchld.sa_handler = SIG_IGN;
777 sigaction (SIGCHLD, &sigchld, NULL);
781 else if (status != 0)
783 fprintf (stderr, "daemonize failed, exiting.\n");
787 listen_thread_main (NULL);
795 * vim: set sw=2 sts=2 ts=8 et fdm=marker :