2 * RRDTool - src/rrd_daemon.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>
20 * kevin brintnall <kbrint@rufus.net>
25 * First tell the compiler to stick to the C99 and POSIX standards as close as
28 #ifndef __STRICT_ANSI__ /* {{{ */
29 # define __STRICT_ANSI__
32 #ifndef _ISOC99_SOURCE
33 # define _ISOC99_SOURCE
36 #ifdef _POSIX_C_SOURCE
37 # undef _POSIX_C_SOURCE
39 #define _POSIX_C_SOURCE 200112L
41 /* Single UNIX needed for strdup. */
45 #define _XOPEN_SOURCE 500
62 * Now for some includes..
64 #include "rrd.h" /* {{{ */
65 #include "rrd_client.h"
76 #include <sys/types.h>
80 #include <sys/socket.h>
91 #include <glib-2.0/glib.h>
94 #define RRDD_LOG(severity, ...) syslog ((severity), __VA_ARGS__)
97 # define __attribute__(x) /**/
103 struct listen_socket_s
106 char path[PATH_MAX + 1];
108 typedef struct listen_socket_s listen_socket_t;
111 typedef struct cache_item_s cache_item_t;
117 time_t last_flush_time;
118 #define CI_FLAGS_IN_TREE (1<<0)
119 #define CI_FLAGS_IN_QUEUE (1<<1)
125 struct callback_flush_data_s
132 typedef struct callback_flush_data_s callback_flush_data_t;
139 typedef enum queue_side_e queue_side_t;
141 /* max length of socket command or response */
147 static int stay_foreground = 0;
149 static listen_socket_t *listen_fds = NULL;
150 static size_t listen_fds_num = 0;
152 static int do_shutdown = 0;
154 static pthread_t queue_thread;
156 static pthread_t *connection_threads = NULL;
157 static pthread_mutex_t connection_threads_lock = PTHREAD_MUTEX_INITIALIZER;
158 static int connection_threads_num = 0;
161 static GTree *cache_tree = NULL;
162 static cache_item_t *cache_queue_head = NULL;
163 static cache_item_t *cache_queue_tail = NULL;
164 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
165 static pthread_cond_t cache_cond = PTHREAD_COND_INITIALIZER;
167 static pthread_cond_t flush_cond = PTHREAD_COND_INITIALIZER;
169 static int config_write_interval = 300;
170 static int config_write_jitter = 0;
171 static int config_flush_interval = 3600;
172 static char *config_pid_file = NULL;
173 static char *config_base_dir = NULL;
175 static char **config_listen_address_list = NULL;
176 static int config_listen_address_list_len = 0;
178 static uint64_t stats_queue_length = 0;
179 static uint64_t stats_updates_received = 0;
180 static uint64_t stats_flush_received = 0;
181 static uint64_t stats_updates_written = 0;
182 static uint64_t stats_data_sets_written = 0;
183 static uint64_t stats_journal_bytes = 0;
184 static uint64_t stats_journal_rotate = 0;
185 static pthread_mutex_t stats_lock = PTHREAD_MUTEX_INITIALIZER;
187 /* Journaled updates */
188 static char *journal_cur = NULL;
189 static char *journal_old = NULL;
190 static FILE *journal_fh = NULL;
191 static pthread_mutex_t journal_lock = PTHREAD_MUTEX_INITIALIZER;
192 static int journal_write(char *cmd, char *args);
193 static void journal_done(void);
194 static void journal_rotate(void);
199 static void sig_int_handler (int s __attribute__((unused))) /* {{{ */
201 RRDD_LOG(LOG_NOTICE, "caught SIGINT");
203 pthread_cond_broadcast(&cache_cond);
204 } /* }}} void sig_int_handler */
206 static void sig_term_handler (int s __attribute__((unused))) /* {{{ */
208 RRDD_LOG(LOG_NOTICE, "caught SIGTERM");
210 pthread_cond_broadcast(&cache_cond);
211 } /* }}} void sig_term_handler */
213 static int write_pidfile (void) /* {{{ */
222 file = (config_pid_file != NULL)
224 : LOCALSTATEDIR "/run/rrdcached.pid";
226 fd = open(file, O_CREAT|O_EXCL|O_WRONLY, S_IRUSR|S_IRGRP|S_IROTH);
229 RRDD_LOG(LOG_ERR, "FATAL: cannot create '%s' (%s)",
230 file, rrd_strerror(errno));
234 fh = fdopen (fd, "w");
237 RRDD_LOG (LOG_ERR, "write_pidfile: Opening `%s' failed.", file);
242 fprintf (fh, "%i\n", (int) pid);
246 } /* }}} int write_pidfile */
248 static int remove_pidfile (void) /* {{{ */
253 file = (config_pid_file != NULL)
255 : LOCALSTATEDIR "/run/rrdcached.pid";
257 status = unlink (file);
261 } /* }}} int remove_pidfile */
263 static ssize_t sread (int fd, void *buffer_void, size_t buffer_size) /* {{{ */
270 buffer = (char *) buffer_void;
272 buffer_free = buffer_size;
274 while (buffer_free > 0)
276 status = read (fd, buffer + buffer_used, buffer_free);
277 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
286 assert ((0 > status) || (buffer_free >= (size_t) status));
288 buffer_free = buffer_free - status;
289 buffer_used = buffer_used + status;
291 if (buffer[buffer_used - 1] == '\n')
295 assert (buffer_used > 0);
297 if (buffer[buffer_used - 1] != '\n')
303 buffer[buffer_used - 1] = 0;
305 /* Fix network line endings. */
306 if ((buffer_used > 1) && (buffer[buffer_used - 2] == '\r'))
309 buffer[buffer_used - 1] = 0;
312 return (buffer_used);
313 } /* }}} ssize_t sread */
315 static ssize_t swrite (int fd, const void *buf, size_t count) /* {{{ */
321 /* special case for journal replay */
322 if (fd < 0) return 0;
324 ptr = (const char *) buf;
329 status = write (fd, (const void *) ptr, nleft);
331 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
342 } /* }}} ssize_t swrite */
344 static void _wipe_ci_values(cache_item_t *ci, time_t when)
349 ci->last_flush_time = when;
350 if (config_write_jitter > 0)
351 ci->last_flush_time += (random() % config_write_jitter);
353 ci->flags &= ~(CI_FLAGS_IN_QUEUE);
357 * enqueue_cache_item:
358 * `cache_lock' must be acquired before calling this function!
360 static int enqueue_cache_item (cache_item_t *ci, /* {{{ */
368 if (ci->values_num == 0)
373 if ((ci->flags & CI_FLAGS_IN_QUEUE) == 0)
375 assert (ci->next == NULL);
376 ci->next = cache_queue_head;
377 cache_queue_head = ci;
379 if (cache_queue_tail == NULL)
380 cache_queue_tail = cache_queue_head;
384 else if (cache_queue_head == ci)
388 else /* enqueued, but not first entry */
392 /* find previous entry */
393 for (prev = cache_queue_head; prev != NULL; prev = prev->next)
394 if (prev->next == ci)
396 assert (prev != NULL);
398 /* move to the front */
399 prev->next = ci->next;
400 ci->next = cache_queue_head;
401 cache_queue_head = ci;
403 /* check if we need to adapt the tail */
404 if (cache_queue_tail == ci)
405 cache_queue_tail = prev;
408 else /* (side == TAIL) */
410 /* We don't move values back in the list.. */
411 if ((ci->flags & CI_FLAGS_IN_QUEUE) != 0)
414 assert (ci->next == NULL);
416 if (cache_queue_tail == NULL)
417 cache_queue_head = ci;
419 cache_queue_tail->next = ci;
420 cache_queue_tail = ci;
425 ci->flags |= CI_FLAGS_IN_QUEUE;
429 pthread_mutex_lock (&stats_lock);
430 stats_queue_length++;
431 pthread_mutex_unlock (&stats_lock);
435 } /* }}} int enqueue_cache_item */
438 * tree_callback_flush:
439 * Called via `g_tree_foreach' in `queue_thread_main'. `cache_lock' is held
440 * while this is in progress.
442 static gboolean tree_callback_flush (gpointer key, gpointer value, /* {{{ */
446 callback_flush_data_t *cfd;
448 ci = (cache_item_t *) value;
449 cfd = (callback_flush_data_t *) data;
451 if ((ci->last_flush_time <= cfd->abs_timeout)
452 && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0)
453 && (ci->values_num > 0))
455 enqueue_cache_item (ci, TAIL);
457 else if ((do_shutdown != 0)
458 && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0)
459 && (ci->values_num > 0))
461 enqueue_cache_item (ci, TAIL);
463 else if (((cfd->now - ci->last_flush_time) >= config_flush_interval)
464 && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0)
465 && (ci->values_num <= 0))
469 temp = (char **) realloc (cfd->keys,
470 sizeof (char *) * (cfd->keys_num + 1));
473 RRDD_LOG (LOG_ERR, "tree_callback_flush: realloc failed.");
477 /* Make really sure this points to the _same_ place */
478 assert ((char *) key == ci->file);
479 cfd->keys[cfd->keys_num] = (char *) key;
484 } /* }}} gboolean tree_callback_flush */
486 static int flush_old_values (int max_age)
488 callback_flush_data_t cfd;
491 memset (&cfd, 0, sizeof (cfd));
492 /* Pass the current time as user data so that we don't need to call
493 * `time' for each node. */
494 cfd.now = time (NULL);
499 cfd.abs_timeout = cfd.now - max_age;
501 cfd.abs_timeout = cfd.now + 1;
503 /* `tree_callback_flush' will return the keys of all values that haven't
504 * been touched in the last `config_flush_interval' seconds in `cfd'.
505 * The char*'s in this array point to the same memory as ci->file, so we
506 * don't need to free them separately. */
507 g_tree_foreach (cache_tree, tree_callback_flush, (gpointer) &cfd);
509 for (k = 0; k < cfd.keys_num; k++)
513 /* This must not fail. */
514 ci = (cache_item_t *) g_tree_lookup (cache_tree, cfd.keys[k]);
517 /* If we end up here with values available, something's seriously
519 assert (ci->values_num == 0);
521 /* Remove the node from the tree */
522 g_tree_remove (cache_tree, cfd.keys[k]);
525 /* Now free and clean up `ci'. */
530 } /* for (k = 0; k < cfd.keys_num; k++) */
532 if (cfd.keys != NULL)
539 } /* int flush_old_values */
541 static void *queue_thread_main (void *args __attribute__((unused))) /* {{{ */
544 struct timespec next_flush;
546 gettimeofday (&now, NULL);
547 next_flush.tv_sec = now.tv_sec + config_flush_interval;
548 next_flush.tv_nsec = 1000 * now.tv_usec;
550 pthread_mutex_lock (&cache_lock);
551 while ((do_shutdown == 0) || (cache_queue_head != NULL))
560 /* First, check if it's time to do the cache flush. */
561 gettimeofday (&now, NULL);
562 if ((now.tv_sec > next_flush.tv_sec)
563 || ((now.tv_sec == next_flush.tv_sec)
564 && ((1000 * now.tv_usec) > next_flush.tv_nsec)))
566 /* Flush all values that haven't been written in the last
567 * `config_write_interval' seconds. */
568 flush_old_values (config_write_interval);
570 /* Determine the time of the next cache flush. */
571 while (next_flush.tv_sec <= now.tv_sec)
572 next_flush.tv_sec += config_flush_interval;
574 /* unlock the cache while we rotate so we don't block incoming
575 * updates if the fsync() blocks on disk I/O */
576 pthread_mutex_unlock(&cache_lock);
578 pthread_mutex_lock(&cache_lock);
581 /* Now, check if there's something to store away. If not, wait until
582 * something comes in or it's time to do the cache flush. */
583 if (cache_queue_head == NULL)
585 status = pthread_cond_timedwait (&cache_cond, &cache_lock, &next_flush);
586 if ((status != 0) && (status != ETIMEDOUT))
588 RRDD_LOG (LOG_ERR, "queue_thread_main: "
589 "pthread_cond_timedwait returned %i.", status);
593 /* We're about to shut down, so lets flush the entire tree. */
594 if ((do_shutdown != 0) && (cache_queue_head == NULL))
595 flush_old_values (/* max age = */ -1);
597 /* Check if a value has arrived. This may be NULL if we timed out or there
598 * was an interrupt such as a signal. */
599 if (cache_queue_head == NULL)
602 ci = cache_queue_head;
604 /* copy the relevant parts */
605 file = strdup (ci->file);
608 RRDD_LOG (LOG_ERR, "queue_thread_main: strdup failed.");
612 assert(ci->values != NULL);
613 assert(ci->values_num > 0);
616 values_num = ci->values_num;
618 _wipe_ci_values(ci, time(NULL));
620 cache_queue_head = ci->next;
621 if (cache_queue_head == NULL)
622 cache_queue_tail = NULL;
625 pthread_mutex_lock (&stats_lock);
626 assert (stats_queue_length > 0);
627 stats_queue_length--;
628 pthread_mutex_unlock (&stats_lock);
630 pthread_mutex_unlock (&cache_lock);
633 status = rrd_update_r (file, NULL, values_num, (void *) values);
636 RRDD_LOG (LOG_NOTICE, "queue_thread_main: "
637 "rrd_update_r (%s) failed with status %i. (%s)",
638 file, status, rrd_get_error());
641 journal_write("wrote", file);
643 for (i = 0; i < values_num; i++)
651 pthread_mutex_lock (&stats_lock);
652 stats_updates_written++;
653 stats_data_sets_written += values_num;
654 pthread_mutex_unlock (&stats_lock);
657 pthread_mutex_lock (&cache_lock);
658 pthread_cond_broadcast (&flush_cond);
660 /* We're about to shut down, so lets flush the entire tree. */
661 if ((do_shutdown != 0) && (cache_queue_head == NULL))
662 flush_old_values (/* max age = */ -1);
663 } /* while ((do_shutdown == 0) || (cache_queue_head != NULL)) */
664 pthread_mutex_unlock (&cache_lock);
666 assert(cache_queue_head == NULL);
667 RRDD_LOG(LOG_INFO, "clean shutdown; all RRDs flushed");
671 } /* }}} void *queue_thread_main */
673 static int buffer_get_field (char **buffer_ret, /* {{{ */
674 size_t *buffer_size_ret, char **field_ret)
683 buffer = *buffer_ret;
685 buffer_size = *buffer_size_ret;
689 if (buffer_size <= 0)
692 /* This is ensured by `handle_request'. */
693 assert (buffer[buffer_size - 1] == '\0');
696 while (buffer_pos < buffer_size)
698 /* Check for end-of-field or end-of-buffer */
699 if (buffer[buffer_pos] == ' ' || buffer[buffer_pos] == '\0')
701 field[field_size] = 0;
707 /* Handle escaped characters. */
708 else if (buffer[buffer_pos] == '\\')
710 if (buffer_pos >= (buffer_size - 1))
713 field[field_size] = buffer[buffer_pos];
717 /* Normal operation */
720 field[field_size] = buffer[buffer_pos];
724 } /* while (buffer_pos < buffer_size) */
729 *buffer_ret = buffer + buffer_pos;
730 *buffer_size_ret = buffer_size - buffer_pos;
734 } /* }}} int buffer_get_field */
736 static int flush_file (const char *filename) /* {{{ */
740 pthread_mutex_lock (&cache_lock);
742 ci = (cache_item_t *) g_tree_lookup (cache_tree, filename);
745 pthread_mutex_unlock (&cache_lock);
749 /* Enqueue at head */
750 enqueue_cache_item (ci, HEAD);
751 pthread_cond_signal (&cache_cond);
753 while ((ci->flags & CI_FLAGS_IN_QUEUE) != 0)
757 pthread_cond_wait (&flush_cond, &cache_lock);
759 ci = g_tree_lookup (cache_tree, filename);
762 RRDD_LOG (LOG_ERR, "flush_file: Tree node went away "
763 "while waiting for flush.");
764 pthread_mutex_unlock (&cache_lock);
769 pthread_mutex_unlock (&cache_lock);
771 } /* }}} int flush_file */
773 static int handle_request_help (int fd, /* {{{ */
774 char *buffer, size_t buffer_size)
778 size_t help_text_len;
784 "4 Command overview\n",
785 "FLUSH <filename>\n",
786 "HELP [<command>]\n",
787 "UPDATE <filename> <values> [<values> ...]\n",
790 size_t help_help_len = sizeof (help_help) / sizeof (help_help[0]);
794 "4 Help for FLUSH\n",
795 "Usage: FLUSH <filename>\n",
797 "Adds the given filename to the head of the update queue and returns\n",
798 "after is has been dequeued.\n"
800 size_t help_flush_len = sizeof (help_flush) / sizeof (help_flush[0]);
802 char *help_update[] =
804 "9 Help for UPDATE\n",
805 "Usage: UPDATE <filename> <values> [<values> ...]\n"
807 "Adds the given file to the internal cache if it is not yet known and\n",
808 "appends the given value(s) to the entry. See the rrdcached(1) manpage\n",
811 "Each <values> has the following form:\n",
812 " <values> = <time>:<value>[:<value>[...]]\n",
813 "See the rrdupdate(1) manpage for details.\n"
815 size_t help_update_len = sizeof (help_update) / sizeof (help_update[0]);
819 "4 Help for STATS\n",
822 "Returns some performance counters, see the rrdcached(1) manpage for\n",
823 "a description of the values.\n"
825 size_t help_stats_len = sizeof (help_stats) / sizeof (help_stats[0]);
827 status = buffer_get_field (&buffer, &buffer_size, &command);
830 help_text = help_help;
831 help_text_len = help_help_len;
835 if (strcasecmp (command, "update") == 0)
837 help_text = help_update;
838 help_text_len = help_update_len;
840 else if (strcasecmp (command, "flush") == 0)
842 help_text = help_flush;
843 help_text_len = help_flush_len;
845 else if (strcasecmp (command, "stats") == 0)
847 help_text = help_stats;
848 help_text_len = help_stats_len;
852 help_text = help_help;
853 help_text_len = help_help_len;
857 for (i = 0; i < help_text_len; i++)
859 status = swrite (fd, help_text[i], strlen (help_text[i]));
863 RRDD_LOG (LOG_ERR, "handle_request_help: swrite returned an error.");
869 } /* }}} int handle_request_help */
871 static int handle_request_stats (int fd, /* {{{ */
872 char *buffer __attribute__((unused)),
873 size_t buffer_size __attribute__((unused)))
876 char outbuf[CMD_MAX];
878 uint64_t copy_queue_length;
879 uint64_t copy_updates_received;
880 uint64_t copy_flush_received;
881 uint64_t copy_updates_written;
882 uint64_t copy_data_sets_written;
883 uint64_t copy_journal_bytes;
884 uint64_t copy_journal_rotate;
886 uint64_t tree_nodes_number;
889 pthread_mutex_lock (&stats_lock);
890 copy_queue_length = stats_queue_length;
891 copy_updates_received = stats_updates_received;
892 copy_flush_received = stats_flush_received;
893 copy_updates_written = stats_updates_written;
894 copy_data_sets_written = stats_data_sets_written;
895 copy_journal_bytes = stats_journal_bytes;
896 copy_journal_rotate = stats_journal_rotate;
897 pthread_mutex_unlock (&stats_lock);
899 pthread_mutex_lock (&cache_lock);
900 tree_nodes_number = (uint64_t) g_tree_nnodes (cache_tree);
901 tree_depth = (uint64_t) g_tree_height (cache_tree);
902 pthread_mutex_unlock (&cache_lock);
904 #define RRDD_STATS_SEND \
905 outbuf[sizeof (outbuf) - 1] = 0; \
906 status = swrite (fd, outbuf, strlen (outbuf)); \
910 RRDD_LOG (LOG_INFO, "handle_request_stats: swrite returned an error."); \
914 strncpy (outbuf, "9 Statistics follow\n", sizeof (outbuf));
917 snprintf (outbuf, sizeof (outbuf),
918 "QueueLength: %"PRIu64"\n", copy_queue_length);
921 snprintf (outbuf, sizeof (outbuf),
922 "UpdatesReceived: %"PRIu64"\n", copy_updates_received);
925 snprintf (outbuf, sizeof (outbuf),
926 "FlushesReceived: %"PRIu64"\n", copy_flush_received);
929 snprintf (outbuf, sizeof (outbuf),
930 "UpdatesWritten: %"PRIu64"\n", copy_updates_written);
933 snprintf (outbuf, sizeof (outbuf),
934 "DataSetsWritten: %"PRIu64"\n", copy_data_sets_written);
937 snprintf (outbuf, sizeof (outbuf),
938 "TreeNodesNumber: %"PRIu64"\n", tree_nodes_number);
941 snprintf (outbuf, sizeof (outbuf),
942 "TreeDepth: %"PRIu64"\n", tree_depth);
945 snprintf (outbuf, sizeof(outbuf),
946 "JournalBytes: %"PRIu64"\n", copy_journal_bytes);
949 snprintf (outbuf, sizeof(outbuf),
950 "JournalRotate: %"PRIu64"\n", copy_journal_rotate);
954 #undef RRDD_STATS_SEND
955 } /* }}} int handle_request_stats */
957 static int handle_request_flush (int fd, /* {{{ */
958 char *buffer, size_t buffer_size)
962 char result[CMD_MAX];
964 status = buffer_get_field (&buffer, &buffer_size, &file);
967 strncpy (result, "-1 Usage: flush <filename>\n", sizeof (result));
971 pthread_mutex_lock(&stats_lock);
972 stats_flush_received++;
973 pthread_mutex_unlock(&stats_lock);
975 status = flush_file (file);
977 snprintf (result, sizeof (result), "0 Successfully flushed %s.\n", file);
978 else if (status == ENOENT)
980 /* no file in our tree; see whether it exists at all */
983 memset(&statbuf, 0, sizeof(statbuf));
984 if (stat(file, &statbuf) == 0 && S_ISREG(statbuf.st_mode))
985 snprintf (result, sizeof (result), "0 Nothing to flush: %s.\n", file);
987 snprintf (result, sizeof (result), "-1 No such file: %s.\n", file);
990 strncpy (result, "-1 Internal error.\n", sizeof (result));
992 snprintf (result, sizeof (result), "-1 Failed with status %i.\n", status);
994 result[sizeof (result) - 1] = 0;
996 status = swrite (fd, result, strlen (result));
1000 RRDD_LOG (LOG_INFO, "handle_request_flush: swrite returned an error.");
1005 } /* }}} int handle_request_flush */
1007 static int handle_request_update (int fd, /* {{{ */
1008 char *buffer, size_t buffer_size)
1017 char answer[CMD_MAX];
1019 #define RRDD_UPDATE_SEND \
1020 answer[sizeof (answer) - 1] = 0; \
1021 status = swrite (fd, answer, strlen (answer)); \
1025 RRDD_LOG (LOG_INFO, "handle_request_update: swrite returned an error."); \
1031 status = buffer_get_field (&buffer, &buffer_size, &file);
1034 strncpy (answer, "-1 Usage: UPDATE <filename> <values> [<values> ...]\n",
1040 pthread_mutex_lock(&stats_lock);
1041 stats_updates_received++;
1042 pthread_mutex_unlock(&stats_lock);
1044 pthread_mutex_lock (&cache_lock);
1046 ci = g_tree_lookup (cache_tree, file);
1047 if (ci == NULL) /* {{{ */
1049 struct stat statbuf;
1051 memset (&statbuf, 0, sizeof (statbuf));
1052 status = stat (file, &statbuf);
1055 pthread_mutex_unlock (&cache_lock);
1056 RRDD_LOG (LOG_NOTICE, "handle_request_update: stat (%s) failed.", file);
1059 if (status == ENOENT)
1060 snprintf (answer, sizeof (answer), "-1 No such file: %s\n", file);
1062 snprintf (answer, sizeof (answer), "-1 stat failed with error %i.\n",
1067 if (!S_ISREG (statbuf.st_mode))
1069 pthread_mutex_unlock (&cache_lock);
1071 snprintf (answer, sizeof (answer), "-1 Not a regular file: %s\n", file);
1075 if (access(file, R_OK|W_OK) != 0)
1077 pthread_mutex_unlock (&cache_lock);
1079 snprintf (answer, sizeof (answer), "-1 Cannot read/write %s: %s\n",
1080 file, rrd_strerror(errno));
1085 ci = (cache_item_t *) malloc (sizeof (cache_item_t));
1088 pthread_mutex_unlock (&cache_lock);
1089 RRDD_LOG (LOG_ERR, "handle_request_update: malloc failed.");
1091 strncpy (answer, "-1 malloc failed.\n", sizeof (answer));
1095 memset (ci, 0, sizeof (cache_item_t));
1097 ci->file = strdup (file);
1098 if (ci->file == NULL)
1100 pthread_mutex_unlock (&cache_lock);
1102 RRDD_LOG (LOG_ERR, "handle_request_update: strdup failed.");
1104 strncpy (answer, "-1 strdup failed.\n", sizeof (answer));
1109 _wipe_ci_values(ci, now);
1110 ci->flags = CI_FLAGS_IN_TREE;
1112 g_tree_insert (cache_tree, (void *) ci->file, (void *) ci);
1114 assert (ci != NULL);
1116 while (buffer_size > 0)
1121 status = buffer_get_field (&buffer, &buffer_size, &value);
1124 RRDD_LOG (LOG_INFO, "handle_request_update: Error reading field.");
1128 temp = (char **) realloc (ci->values,
1129 sizeof (char *) * (ci->values_num + 1));
1132 RRDD_LOG (LOG_ERR, "handle_request_update: realloc failed.");
1137 ci->values[ci->values_num] = strdup (value);
1138 if (ci->values[ci->values_num] == NULL)
1140 RRDD_LOG (LOG_ERR, "handle_request_update: strdup failed.");
1148 if (((now - ci->last_flush_time) >= config_write_interval)
1149 && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0)
1150 && (ci->values_num > 0))
1152 enqueue_cache_item (ci, TAIL);
1153 pthread_cond_signal (&cache_cond);
1156 pthread_mutex_unlock (&cache_lock);
1160 strncpy (answer, "-1 No values updated.\n", sizeof (answer));
1164 snprintf (answer, sizeof (answer), "0 Enqueued %i value%s\n", values_num,
1165 (values_num == 1) ? "" : "s");
1169 #undef RRDD_UPDATE_SEND
1170 } /* }}} int handle_request_update */
1172 /* we came across a "WROTE" entry during journal replay.
1173 * throw away any values that we have accumulated for this file
1175 static int handle_request_wrote (int fd __attribute__((unused)), /* {{{ */
1177 size_t buffer_size __attribute__((unused)))
1181 const char *file = buffer;
1183 pthread_mutex_lock(&cache_lock);
1185 ci = g_tree_lookup(cache_tree, file);
1188 pthread_mutex_unlock(&cache_lock);
1194 for (i=0; i < ci->values_num; i++)
1195 free(ci->values[i]);
1200 _wipe_ci_values(ci, time(NULL));
1202 pthread_mutex_unlock(&cache_lock);
1204 } /* }}} int handle_request_wrote */
1206 /* if fd < 0, we are in journal replay mode */
1207 static int handle_request (int fd, char *buffer, size_t buffer_size) /* {{{ */
1213 assert (buffer[buffer_size - 1] == '\0');
1215 buffer_ptr = buffer;
1217 status = buffer_get_field (&buffer_ptr, &buffer_size, &command);
1220 RRDD_LOG (LOG_INFO, "handle_request: Unable parse command.");
1224 if (strcasecmp (command, "update") == 0)
1226 /* don't re-write updates in replay mode */
1228 journal_write(command, buffer_ptr);
1230 return (handle_request_update (fd, buffer_ptr, buffer_size));
1232 else if (strcasecmp (command, "wrote") == 0 && fd < 0)
1234 /* this is only valid in replay mode */
1235 return (handle_request_wrote (fd, buffer_ptr, buffer_size));
1237 else if (strcasecmp (command, "flush") == 0)
1239 return (handle_request_flush (fd, buffer_ptr, buffer_size));
1241 else if (strcasecmp (command, "stats") == 0)
1243 return (handle_request_stats (fd, buffer_ptr, buffer_size));
1245 else if (strcasecmp (command, "help") == 0)
1247 return (handle_request_help (fd, buffer_ptr, buffer_size));
1251 char result[CMD_MAX];
1253 snprintf (result, sizeof (result), "-1 Unknown command: %s\n", command);
1254 result[sizeof (result) - 1] = 0;
1256 status = swrite (fd, result, strlen (result));
1259 RRDD_LOG (LOG_ERR, "handle_request: swrite failed.");
1265 } /* }}} int handle_request */
1267 /* MUST NOT hold journal_lock before calling this */
1268 static void journal_rotate(void) /* {{{ */
1270 FILE *old_fh = NULL;
1272 if (journal_cur == NULL || journal_old == NULL)
1275 pthread_mutex_lock(&journal_lock);
1277 /* we rotate this way (rename before close) so that the we can release
1278 * the journal lock as fast as possible. Journal writes to the new
1279 * journal can proceed immediately after the new file is opened. The
1280 * fclose can then block without affecting new updates.
1282 if (journal_fh != NULL)
1284 old_fh = journal_fh;
1285 rename(journal_cur, journal_old);
1286 ++stats_journal_rotate;
1289 journal_fh = fopen(journal_cur, "a");
1290 pthread_mutex_unlock(&journal_lock);
1295 if (journal_fh == NULL)
1297 "JOURNALING DISABLED: Cannot open journal file '%s' : (%s)",
1298 journal_cur, rrd_strerror(errno));
1300 } /* }}} static void journal_rotate */
1302 static void journal_done(void) /* {{{ */
1304 if (journal_cur == NULL)
1307 pthread_mutex_lock(&journal_lock);
1308 if (journal_fh != NULL)
1314 RRDD_LOG(LOG_INFO, "removing journals");
1316 unlink(journal_old);
1317 unlink(journal_cur);
1318 pthread_mutex_unlock(&journal_lock);
1320 } /* }}} static void journal_done */
1322 static int journal_write(char *cmd, char *args) /* {{{ */
1326 if (journal_fh == NULL)
1329 pthread_mutex_lock(&journal_lock);
1330 chars = fprintf(journal_fh, "%s %s\n", cmd, args);
1331 pthread_mutex_unlock(&journal_lock);
1335 pthread_mutex_lock(&stats_lock);
1336 stats_journal_bytes += chars;
1337 pthread_mutex_unlock(&stats_lock);
1341 } /* }}} static int journal_write */
1343 static int journal_replay (const char *file) /* {{{ */
1349 char entry[CMD_MAX];
1351 if (file == NULL) return 0;
1353 fh = fopen(file, "r");
1356 if (errno != ENOENT)
1357 RRDD_LOG(LOG_ERR, "journal_replay: cannot open journal file: '%s' (%s)",
1358 file, rrd_strerror(errno));
1362 RRDD_LOG(LOG_NOTICE, "replaying from journal: %s", file);
1369 fgets(entry, sizeof(entry), fh);
1370 entry_len = strlen(entry);
1372 /* check \n termination in case journal writing crashed mid-line */
1375 else if (entry[entry_len - 1] != '\n')
1377 RRDD_LOG(LOG_NOTICE, "Malformed journal entry at line %"PRIu64, line);
1382 entry[entry_len - 1] = '\0';
1384 if (handle_request(-1, entry, entry_len) == 0)
1394 RRDD_LOG(LOG_INFO, "Replayed %d entries (%d failures)",
1395 entry_cnt, fail_cnt);
1401 } /* }}} static int journal_replay */
1403 static void *connection_thread_main (void *args) /* {{{ */
1409 fd = *((int *) args);
1412 pthread_mutex_lock (&connection_threads_lock);
1416 temp = (pthread_t *) realloc (connection_threads,
1417 sizeof (pthread_t) * (connection_threads_num + 1));
1420 RRDD_LOG (LOG_ERR, "connection_thread_main: realloc failed.");
1424 connection_threads = temp;
1425 connection_threads[connection_threads_num] = pthread_self ();
1426 connection_threads_num++;
1429 pthread_mutex_unlock (&connection_threads_lock);
1431 while (do_shutdown == 0)
1433 char buffer[CMD_MAX];
1435 struct pollfd pollfd;
1439 pollfd.events = POLLIN | POLLPRI;
1442 status = poll (&pollfd, 1, /* timeout = */ 500);
1443 if (status == 0) /* timeout */
1445 else if (status < 0) /* error */
1448 if (status == EINTR)
1450 RRDD_LOG (LOG_ERR, "connection_thread_main: poll(2) failed.");
1454 if ((pollfd.revents & POLLHUP) != 0) /* normal shutdown */
1459 else if ((pollfd.revents & (POLLIN | POLLPRI)) == 0)
1461 RRDD_LOG (LOG_WARNING, "connection_thread_main: "
1462 "poll(2) returned something unexpected: %#04hx",
1468 status = (int) sread (fd, buffer, sizeof (buffer));
1474 RRDD_LOG(LOG_ERR, "connection_thread_main: sread failed.");
1479 status = handle_request (fd, buffer, /*buffer_size=*/ status);
1487 self = pthread_self ();
1488 /* Remove this thread from the connection threads list */
1489 pthread_mutex_lock (&connection_threads_lock);
1490 /* Find out own index in the array */
1491 for (i = 0; i < connection_threads_num; i++)
1492 if (pthread_equal (connection_threads[i], self) != 0)
1494 assert (i < connection_threads_num);
1496 /* Move the trailing threads forward. */
1497 if (i < (connection_threads_num - 1))
1499 memmove (connection_threads + i,
1500 connection_threads + i + 1,
1501 sizeof (pthread_t) * (connection_threads_num - i - 1));
1504 connection_threads_num--;
1505 pthread_mutex_unlock (&connection_threads_lock);
1508 } /* }}} void *connection_thread_main */
1510 static int open_listen_socket_unix (const char *path) /* {{{ */
1513 struct sockaddr_un sa;
1514 listen_socket_t *temp;
1517 temp = (listen_socket_t *) realloc (listen_fds,
1518 sizeof (listen_fds[0]) * (listen_fds_num + 1));
1521 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: realloc failed.");
1525 memset (listen_fds + listen_fds_num, 0, sizeof (listen_fds[0]));
1527 fd = socket (PF_UNIX, SOCK_STREAM, /* protocol = */ 0);
1530 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: socket(2) failed.");
1534 memset (&sa, 0, sizeof (sa));
1535 sa.sun_family = AF_UNIX;
1536 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
1538 status = bind (fd, (struct sockaddr *) &sa, sizeof (sa));
1541 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: bind(2) failed.");
1547 status = listen (fd, /* backlog = */ 10);
1550 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: listen(2) failed.");
1556 listen_fds[listen_fds_num].fd = fd;
1557 snprintf (listen_fds[listen_fds_num].path,
1558 sizeof (listen_fds[listen_fds_num].path) - 1,
1563 } /* }}} int open_listen_socket_unix */
1565 static int open_listen_socket (const char *addr) /* {{{ */
1567 struct addrinfo ai_hints;
1568 struct addrinfo *ai_res;
1569 struct addrinfo *ai_ptr;
1572 assert (addr != NULL);
1574 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
1575 return (open_listen_socket_unix (addr + strlen ("unix:")));
1576 else if (addr[0] == '/')
1577 return (open_listen_socket_unix (addr));
1579 memset (&ai_hints, 0, sizeof (ai_hints));
1580 ai_hints.ai_flags = 0;
1581 #ifdef AI_ADDRCONFIG
1582 ai_hints.ai_flags |= AI_ADDRCONFIG;
1584 ai_hints.ai_family = AF_UNSPEC;
1585 ai_hints.ai_socktype = SOCK_STREAM;
1588 status = getaddrinfo (addr, RRDCACHED_DEFAULT_PORT, &ai_hints, &ai_res);
1591 RRDD_LOG (LOG_ERR, "open_listen_socket: getaddrinfo(%s) failed: "
1592 "%s", addr, gai_strerror (status));
1596 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1599 listen_socket_t *temp;
1601 temp = (listen_socket_t *) realloc (listen_fds,
1602 sizeof (listen_fds[0]) * (listen_fds_num + 1));
1605 RRDD_LOG (LOG_ERR, "open_listen_socket: realloc failed.");
1609 memset (listen_fds + listen_fds_num, 0, sizeof (listen_fds[0]));
1611 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
1614 RRDD_LOG (LOG_ERR, "open_listen_socket: socket(2) failed.");
1618 status = bind (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1621 RRDD_LOG (LOG_ERR, "open_listen_socket: bind(2) failed.");
1626 status = listen (fd, /* backlog = */ 10);
1629 RRDD_LOG (LOG_ERR, "open_listen_socket: listen(2) failed.");
1634 listen_fds[listen_fds_num].fd = fd;
1635 strncpy (listen_fds[listen_fds_num].path, addr,
1636 sizeof (listen_fds[listen_fds_num].path) - 1);
1638 } /* for (ai_ptr) */
1641 } /* }}} int open_listen_socket */
1643 static int close_listen_sockets (void) /* {{{ */
1647 for (i = 0; i < listen_fds_num; i++)
1649 close (listen_fds[i].fd);
1650 if (strncmp ("unix:", listen_fds[i].path, strlen ("unix:")) == 0)
1651 unlink (listen_fds[i].path + strlen ("unix:"));
1659 } /* }}} int close_listen_sockets */
1661 static void *listen_thread_main (void *args __attribute__((unused))) /* {{{ */
1663 struct pollfd *pollfds;
1668 for (i = 0; i < config_listen_address_list_len; i++)
1669 open_listen_socket (config_listen_address_list[i]);
1671 if (config_listen_address_list_len < 1)
1672 open_listen_socket (RRDCACHED_DEFAULT_ADDRESS);
1674 if (listen_fds_num < 1)
1676 RRDD_LOG (LOG_ERR, "listen_thread_main: No listen sockets "
1677 "could be opened. Sorry.");
1681 pollfds_num = listen_fds_num;
1682 pollfds = (struct pollfd *) malloc (sizeof (*pollfds) * pollfds_num);
1683 if (pollfds == NULL)
1685 RRDD_LOG (LOG_ERR, "listen_thread_main: malloc failed.");
1688 memset (pollfds, 0, sizeof (*pollfds) * pollfds_num);
1690 RRDD_LOG(LOG_INFO, "listening for connections");
1692 while (do_shutdown == 0)
1694 assert (pollfds_num == ((int) listen_fds_num));
1695 for (i = 0; i < pollfds_num; i++)
1697 pollfds[i].fd = listen_fds[i].fd;
1698 pollfds[i].events = POLLIN | POLLPRI;
1699 pollfds[i].revents = 0;
1702 status = poll (pollfds, pollfds_num, /* timeout = */ -1);
1706 if (status != EINTR)
1708 RRDD_LOG (LOG_ERR, "listen_thread_main: poll(2) failed.");
1713 for (i = 0; i < pollfds_num; i++)
1716 struct sockaddr_storage client_sa;
1717 socklen_t client_sa_size;
1719 pthread_attr_t attr;
1721 if (pollfds[i].revents == 0)
1724 if ((pollfds[i].revents & (POLLIN | POLLPRI)) == 0)
1726 RRDD_LOG (LOG_ERR, "listen_thread_main: "
1727 "poll(2) returned something unexpected for listen FD #%i.",
1732 client_sd = (int *) malloc (sizeof (int));
1733 if (client_sd == NULL)
1735 RRDD_LOG (LOG_ERR, "listen_thread_main: malloc failed.");
1739 client_sa_size = sizeof (client_sa);
1740 *client_sd = accept (pollfds[i].fd,
1741 (struct sockaddr *) &client_sa, &client_sa_size);
1744 RRDD_LOG (LOG_ERR, "listen_thread_main: accept(2) failed.");
1748 pthread_attr_init (&attr);
1749 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
1751 status = pthread_create (&tid, &attr, connection_thread_main,
1752 /* args = */ (void *) client_sd);
1755 RRDD_LOG (LOG_ERR, "listen_thread_main: pthread_create failed.");
1760 } /* for (pollfds_num) */
1761 } /* while (do_shutdown == 0) */
1763 RRDD_LOG(LOG_INFO, "starting shutdown");
1765 close_listen_sockets ();
1767 pthread_mutex_lock (&connection_threads_lock);
1768 while (connection_threads_num > 0)
1772 wait_for = connection_threads[0];
1774 pthread_mutex_unlock (&connection_threads_lock);
1775 pthread_join (wait_for, /* retval = */ NULL);
1776 pthread_mutex_lock (&connection_threads_lock);
1778 pthread_mutex_unlock (&connection_threads_lock);
1781 } /* }}} void *listen_thread_main */
1783 static int daemonize (void) /* {{{ */
1787 /* These structures are static, because `sigaction' behaves weird if the are
1789 static struct sigaction sa_int;
1790 static struct sigaction sa_term;
1791 static struct sigaction sa_pipe;
1793 if (!stay_foreground)
1801 fprintf (stderr, "daemonize: fork(2) failed.\n");
1809 /* Change into the /tmp directory. */
1810 base_dir = (config_base_dir != NULL)
1813 status = chdir (base_dir);
1816 fprintf (stderr, "daemonize: chdir (%s) failed.\n", base_dir);
1820 /* Become session leader */
1823 /* Open the first three file descriptors to /dev/null */
1828 open ("/dev/null", O_RDWR);
1831 } /* if (!stay_foreground) */
1833 /* Install signal handlers */
1834 memset (&sa_int, 0, sizeof (sa_int));
1835 sa_int.sa_handler = sig_int_handler;
1836 sigaction (SIGINT, &sa_int, NULL);
1838 memset (&sa_term, 0, sizeof (sa_term));
1839 sa_term.sa_handler = sig_term_handler;
1840 sigaction (SIGTERM, &sa_term, NULL);
1842 memset (&sa_pipe, 0, sizeof (sa_pipe));
1843 sa_pipe.sa_handler = SIG_IGN;
1844 sigaction (SIGPIPE, &sa_pipe, NULL);
1846 openlog ("rrdcached", LOG_PID, LOG_DAEMON);
1847 RRDD_LOG(LOG_INFO, "starting up");
1849 cache_tree = g_tree_new ((GCompareFunc) strcmp);
1850 if (cache_tree == NULL)
1852 RRDD_LOG (LOG_ERR, "daemonize: g_tree_new failed.");
1856 status = write_pidfile ();
1858 } /* }}} int daemonize */
1860 static int cleanup (void) /* {{{ */
1864 pthread_cond_signal (&cache_cond);
1865 pthread_join (queue_thread, /* return = */ NULL);
1869 RRDD_LOG(LOG_INFO, "goodbye");
1873 } /* }}} int cleanup */
1875 static int read_options (int argc, char **argv) /* {{{ */
1880 while ((option = getopt(argc, argv, "gl:f:w:b:z:p:j:h?")) != -1)
1892 temp = (char **) realloc (config_listen_address_list,
1893 sizeof (char *) * (config_listen_address_list_len + 1));
1896 fprintf (stderr, "read_options: realloc failed.\n");
1899 config_listen_address_list = temp;
1901 temp[config_listen_address_list_len] = strdup (optarg);
1902 if (temp[config_listen_address_list_len] == NULL)
1904 fprintf (stderr, "read_options: strdup failed.\n");
1907 config_listen_address_list_len++;
1915 temp = atoi (optarg);
1917 config_flush_interval = temp;
1920 fprintf (stderr, "Invalid flush interval: %s\n", optarg);
1930 temp = atoi (optarg);
1932 config_write_interval = temp;
1935 fprintf (stderr, "Invalid write interval: %s\n", optarg);
1945 temp = atoi(optarg);
1947 config_write_jitter = temp;
1950 fprintf (stderr, "Invalid write jitter: -z %s\n", optarg);
1961 if (config_base_dir != NULL)
1962 free (config_base_dir);
1963 config_base_dir = strdup (optarg);
1964 if (config_base_dir == NULL)
1966 fprintf (stderr, "read_options: strdup failed.\n");
1970 len = strlen (config_base_dir);
1971 while ((len > 0) && (config_base_dir[len - 1] == '/'))
1973 config_base_dir[len - 1] = 0;
1979 fprintf (stderr, "Invalid base directory: %s\n", optarg);
1987 if (config_pid_file != NULL)
1988 free (config_pid_file);
1989 config_pid_file = strdup (optarg);
1990 if (config_pid_file == NULL)
1992 fprintf (stderr, "read_options: strdup failed.\n");
2000 struct stat statbuf;
2001 const char *dir = optarg;
2003 status = stat(dir, &statbuf);
2006 fprintf(stderr, "Cannot stat '%s' : %s\n", dir, rrd_strerror(errno));
2010 if (!S_ISDIR(statbuf.st_mode)
2011 || access(dir, R_OK|W_OK|X_OK) != 0)
2013 fprintf(stderr, "Must specify a writable directory with -j! (%s)\n",
2014 errno ? rrd_strerror(errno) : "");
2018 journal_cur = malloc(PATH_MAX + 1);
2019 journal_old = malloc(PATH_MAX + 1);
2020 if (journal_cur == NULL || journal_old == NULL)
2022 fprintf(stderr, "malloc failure for journal files\n");
2027 snprintf(journal_cur, PATH_MAX, "%s/rrd.journal", dir);
2028 snprintf(journal_old, PATH_MAX, "%s/rrd.journal.old", dir);
2035 printf ("RRDd %s Copyright (C) 2008 Florian octo Forster\n"
2037 "Usage: rrdcached [options]\n"
2039 "Valid options are:\n"
2040 " -l <address> Socket address to listen to.\n"
2041 " -w <seconds> Interval in which to write data.\n"
2042 " -z <delay> Delay writes up to <delay> seconds to spread load" \
2043 " -f <seconds> Interval in which to flush dead data.\n"
2044 " -p <file> Location of the PID-file.\n"
2045 " -b <dir> Base directory to change to.\n"
2047 "For more information and a detailed description of all options "
2049 "to the rrdcached(1) manual page.\n",
2053 } /* switch (option) */
2054 } /* while (getopt) */
2056 /* advise the user when values are not sane */
2057 if (config_flush_interval < 2 * config_write_interval)
2058 fprintf(stderr, "WARNING: flush interval (-f) should be at least"
2059 " 2x write interval (-w) !\n");
2060 if (config_write_jitter > config_write_interval)
2061 fprintf(stderr, "WARNING: write delay (-z) should NOT be larger than"
2062 " write interval (-w) !\n");
2065 } /* }}} int read_options */
2067 int main (int argc, char **argv)
2071 status = read_options (argc, argv);
2079 status = daemonize ();
2082 struct sigaction sigchld;
2084 memset (&sigchld, 0, sizeof (sigchld));
2085 sigchld.sa_handler = SIG_IGN;
2086 sigaction (SIGCHLD, &sigchld, NULL);
2090 else if (status != 0)
2092 fprintf (stderr, "daemonize failed, exiting.\n");
2096 if (journal_cur != NULL)
2098 int had_journal = 0;
2100 pthread_mutex_lock(&journal_lock);
2102 RRDD_LOG(LOG_INFO, "checking for journal files");
2104 had_journal += journal_replay(journal_old);
2105 had_journal += journal_replay(journal_cur);
2108 flush_old_values(-1);
2110 pthread_mutex_unlock(&journal_lock);
2113 RRDD_LOG(LOG_INFO, "journal processing complete");
2116 /* start the queue thread */
2117 memset (&queue_thread, 0, sizeof (queue_thread));
2118 status = pthread_create (&queue_thread,
2124 RRDD_LOG (LOG_ERR, "FATAL: cannot create queue thread");
2129 listen_thread_main (NULL);
2136 * vim: set sw=2 sts=2 ts=8 et fdm=marker :