2 * RRDTool - src/rrd_daemon.c
3 * Copyright (C) 2008 Florian octo Forster
4 * Copyright (C) 2008 Kevin Brintnall
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at verplant.org>
21 * kevin brintnall <kbrint@rufus.net>
26 * First tell the compiler to stick to the C99 and POSIX standards as close as
29 #ifndef __STRICT_ANSI__ /* {{{ */
30 # define __STRICT_ANSI__
33 #ifndef _ISOC99_SOURCE
34 # define _ISOC99_SOURCE
37 #ifdef _POSIX_C_SOURCE
38 # undef _POSIX_C_SOURCE
40 #define _POSIX_C_SOURCE 200112L
42 /* Single UNIX needed for strdup. */
46 #define _XOPEN_SOURCE 500
63 * Now for some includes..
65 #include "rrd.h" /* {{{ */
66 #include "rrd_client.h"
77 #include <sys/types.h>
81 #include <sys/socket.h>
92 #include <glib-2.0/glib.h>
95 #define RRDD_LOG(severity, ...) syslog ((severity), __VA_ARGS__)
98 # define __attribute__(x) /**/
104 struct listen_socket_s
107 char path[PATH_MAX + 1];
109 typedef struct listen_socket_s listen_socket_t;
112 typedef struct cache_item_s cache_item_t;
118 time_t last_flush_time;
119 #define CI_FLAGS_IN_TREE (1<<0)
120 #define CI_FLAGS_IN_QUEUE (1<<1)
122 pthread_cond_t flushed;
126 struct callback_flush_data_s
133 typedef struct callback_flush_data_s callback_flush_data_t;
140 typedef enum queue_side_e queue_side_t;
142 /* max length of socket command or response */
148 static int stay_foreground = 0;
150 static listen_socket_t *listen_fds = NULL;
151 static size_t listen_fds_num = 0;
153 static int do_shutdown = 0;
155 static pthread_t queue_thread;
157 static pthread_t *connection_threads = NULL;
158 static pthread_mutex_t connection_threads_lock = PTHREAD_MUTEX_INITIALIZER;
159 static int connection_threads_num = 0;
162 static GTree *cache_tree = NULL;
163 static cache_item_t *cache_queue_head = NULL;
164 static cache_item_t *cache_queue_tail = NULL;
165 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
166 static pthread_cond_t cache_cond = PTHREAD_COND_INITIALIZER;
168 static int config_write_interval = 300;
169 static int config_write_jitter = 0;
170 static int config_flush_interval = 3600;
171 static char *config_pid_file = NULL;
172 static char *config_base_dir = NULL;
174 static char **config_listen_address_list = NULL;
175 static int config_listen_address_list_len = 0;
177 static uint64_t stats_queue_length = 0;
178 static uint64_t stats_updates_received = 0;
179 static uint64_t stats_flush_received = 0;
180 static uint64_t stats_updates_written = 0;
181 static uint64_t stats_data_sets_written = 0;
182 static uint64_t stats_journal_bytes = 0;
183 static uint64_t stats_journal_rotate = 0;
184 static pthread_mutex_t stats_lock = PTHREAD_MUTEX_INITIALIZER;
186 /* Journaled updates */
187 static char *journal_cur = NULL;
188 static char *journal_old = NULL;
189 static FILE *journal_fh = NULL;
190 static pthread_mutex_t journal_lock = PTHREAD_MUTEX_INITIALIZER;
191 static int journal_write(char *cmd, char *args);
192 static void journal_done(void);
193 static void journal_rotate(void);
198 static void sig_common (const char *sig) /* {{{ */
200 RRDD_LOG(LOG_NOTICE, "caught SIG%s", sig);
202 pthread_cond_broadcast(&cache_cond);
203 } /* }}} void sig_common */
205 static void sig_int_handler (int s __attribute__((unused))) /* {{{ */
208 } /* }}} void sig_int_handler */
210 static void sig_term_handler (int s __attribute__((unused))) /* {{{ */
213 } /* }}} void sig_term_handler */
215 static void install_signal_handlers(void) /* {{{ */
217 /* These structures are static, because `sigaction' behaves weird if the are
219 static struct sigaction sa_int;
220 static struct sigaction sa_term;
221 static struct sigaction sa_pipe;
223 /* Install signal handlers */
224 memset (&sa_int, 0, sizeof (sa_int));
225 sa_int.sa_handler = sig_int_handler;
226 sigaction (SIGINT, &sa_int, NULL);
228 memset (&sa_term, 0, sizeof (sa_term));
229 sa_term.sa_handler = sig_term_handler;
230 sigaction (SIGTERM, &sa_term, NULL);
232 memset (&sa_pipe, 0, sizeof (sa_pipe));
233 sa_pipe.sa_handler = SIG_IGN;
234 sigaction (SIGPIPE, &sa_pipe, NULL);
236 } /* }}} void install_signal_handlers */
238 static int open_pidfile(void) /* {{{ */
243 file = (config_pid_file != NULL)
245 : LOCALSTATEDIR "/run/rrdcached.pid";
247 fd = open(file, O_CREAT|O_EXCL|O_WRONLY, S_IRUSR|S_IRGRP|S_IROTH);
249 fprintf(stderr, "FATAL: cannot create '%s' (%s)\n",
250 file, rrd_strerror(errno));
255 static int write_pidfile (int fd) /* {{{ */
262 fh = fdopen (fd, "w");
265 RRDD_LOG (LOG_ERR, "write_pidfile: fdopen() failed.");
270 fprintf (fh, "%i\n", (int) pid);
274 } /* }}} int write_pidfile */
276 static int remove_pidfile (void) /* {{{ */
281 file = (config_pid_file != NULL)
283 : LOCALSTATEDIR "/run/rrdcached.pid";
285 status = unlink (file);
289 } /* }}} int remove_pidfile */
291 static ssize_t sread (int fd, void *buffer_void, size_t buffer_size) /* {{{ */
298 buffer = (char *) buffer_void;
300 buffer_free = buffer_size;
302 while (buffer_free > 0)
304 status = read (fd, buffer + buffer_used, buffer_free);
305 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
314 assert ((0 > status) || (buffer_free >= (size_t) status));
316 buffer_free = buffer_free - status;
317 buffer_used = buffer_used + status;
319 if (buffer[buffer_used - 1] == '\n')
323 assert (buffer_used > 0);
325 if (buffer[buffer_used - 1] != '\n')
331 buffer[buffer_used - 1] = 0;
333 /* Fix network line endings. */
334 if ((buffer_used > 1) && (buffer[buffer_used - 2] == '\r'))
337 buffer[buffer_used - 1] = 0;
340 return (buffer_used);
341 } /* }}} ssize_t sread */
343 static ssize_t swrite (int fd, const void *buf, size_t count) /* {{{ */
349 /* special case for journal replay */
350 if (fd < 0) return 0;
352 ptr = (const char *) buf;
357 status = write (fd, (const void *) ptr, nleft);
359 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
370 } /* }}} ssize_t swrite */
372 static void _wipe_ci_values(cache_item_t *ci, time_t when)
377 ci->last_flush_time = when;
378 if (config_write_jitter > 0)
379 ci->last_flush_time += (random() % config_write_jitter);
381 ci->flags &= ~(CI_FLAGS_IN_QUEUE);
385 * enqueue_cache_item:
386 * `cache_lock' must be acquired before calling this function!
388 static int enqueue_cache_item (cache_item_t *ci, /* {{{ */
396 if (ci->values_num == 0)
401 if ((ci->flags & CI_FLAGS_IN_QUEUE) == 0)
403 assert (ci->next == NULL);
404 ci->next = cache_queue_head;
405 cache_queue_head = ci;
407 if (cache_queue_tail == NULL)
408 cache_queue_tail = cache_queue_head;
412 else if (cache_queue_head == ci)
416 else /* enqueued, but not first entry */
420 /* find previous entry */
421 for (prev = cache_queue_head; prev != NULL; prev = prev->next)
422 if (prev->next == ci)
424 assert (prev != NULL);
426 /* move to the front */
427 prev->next = ci->next;
428 ci->next = cache_queue_head;
429 cache_queue_head = ci;
431 /* check if we need to adapt the tail */
432 if (cache_queue_tail == ci)
433 cache_queue_tail = prev;
436 else /* (side == TAIL) */
438 /* We don't move values back in the list.. */
439 if ((ci->flags & CI_FLAGS_IN_QUEUE) != 0)
442 assert (ci->next == NULL);
444 if (cache_queue_tail == NULL)
445 cache_queue_head = ci;
447 cache_queue_tail->next = ci;
448 cache_queue_tail = ci;
453 ci->flags |= CI_FLAGS_IN_QUEUE;
457 pthread_cond_broadcast(&cache_cond);
458 pthread_mutex_lock (&stats_lock);
459 stats_queue_length++;
460 pthread_mutex_unlock (&stats_lock);
464 } /* }}} int enqueue_cache_item */
467 * tree_callback_flush:
468 * Called via `g_tree_foreach' in `queue_thread_main'. `cache_lock' is held
469 * while this is in progress.
471 static gboolean tree_callback_flush (gpointer key, gpointer value, /* {{{ */
475 callback_flush_data_t *cfd;
477 ci = (cache_item_t *) value;
478 cfd = (callback_flush_data_t *) data;
480 if ((ci->last_flush_time <= cfd->abs_timeout)
481 && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0)
482 && (ci->values_num > 0))
484 enqueue_cache_item (ci, TAIL);
486 else if ((do_shutdown != 0)
487 && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0)
488 && (ci->values_num > 0))
490 enqueue_cache_item (ci, TAIL);
492 else if (((cfd->now - ci->last_flush_time) >= config_flush_interval)
493 && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0)
494 && (ci->values_num <= 0))
498 temp = (char **) realloc (cfd->keys,
499 sizeof (char *) * (cfd->keys_num + 1));
502 RRDD_LOG (LOG_ERR, "tree_callback_flush: realloc failed.");
506 /* Make really sure this points to the _same_ place */
507 assert ((char *) key == ci->file);
508 cfd->keys[cfd->keys_num] = (char *) key;
513 } /* }}} gboolean tree_callback_flush */
515 static int flush_old_values (int max_age)
517 callback_flush_data_t cfd;
520 memset (&cfd, 0, sizeof (cfd));
521 /* Pass the current time as user data so that we don't need to call
522 * `time' for each node. */
523 cfd.now = time (NULL);
528 cfd.abs_timeout = cfd.now - max_age;
530 cfd.abs_timeout = cfd.now + 2*config_write_jitter + 1;
532 /* `tree_callback_flush' will return the keys of all values that haven't
533 * been touched in the last `config_flush_interval' seconds in `cfd'.
534 * The char*'s in this array point to the same memory as ci->file, so we
535 * don't need to free them separately. */
536 g_tree_foreach (cache_tree, tree_callback_flush, (gpointer) &cfd);
538 for (k = 0; k < cfd.keys_num; k++)
542 /* This must not fail. */
543 ci = (cache_item_t *) g_tree_lookup (cache_tree, cfd.keys[k]);
546 /* If we end up here with values available, something's seriously
548 assert (ci->values_num == 0);
550 /* Remove the node from the tree */
551 g_tree_remove (cache_tree, cfd.keys[k]);
554 /* Now free and clean up `ci'. */
559 } /* for (k = 0; k < cfd.keys_num; k++) */
561 if (cfd.keys != NULL)
568 } /* int flush_old_values */
570 static void *queue_thread_main (void *args __attribute__((unused))) /* {{{ */
573 struct timespec next_flush;
575 gettimeofday (&now, NULL);
576 next_flush.tv_sec = now.tv_sec + config_flush_interval;
577 next_flush.tv_nsec = 1000 * now.tv_usec;
579 pthread_mutex_lock (&cache_lock);
580 while ((do_shutdown == 0) || (cache_queue_head != NULL))
589 /* First, check if it's time to do the cache flush. */
590 gettimeofday (&now, NULL);
591 if ((now.tv_sec > next_flush.tv_sec)
592 || ((now.tv_sec == next_flush.tv_sec)
593 && ((1000 * now.tv_usec) > next_flush.tv_nsec)))
595 /* Flush all values that haven't been written in the last
596 * `config_write_interval' seconds. */
597 flush_old_values (config_write_interval);
599 /* Determine the time of the next cache flush. */
600 while (next_flush.tv_sec <= now.tv_sec)
601 next_flush.tv_sec += config_flush_interval;
603 /* unlock the cache while we rotate so we don't block incoming
604 * updates if the fsync() blocks on disk I/O */
605 pthread_mutex_unlock(&cache_lock);
607 pthread_mutex_lock(&cache_lock);
610 /* Now, check if there's something to store away. If not, wait until
611 * something comes in or it's time to do the cache flush. */
612 if (cache_queue_head == NULL)
614 status = pthread_cond_timedwait (&cache_cond, &cache_lock, &next_flush);
615 if ((status != 0) && (status != ETIMEDOUT))
617 RRDD_LOG (LOG_ERR, "queue_thread_main: "
618 "pthread_cond_timedwait returned %i.", status);
622 /* We're about to shut down, so lets flush the entire tree. */
623 if ((do_shutdown != 0) && (cache_queue_head == NULL))
624 flush_old_values (/* max age = */ -1);
626 /* Check if a value has arrived. This may be NULL if we timed out or there
627 * was an interrupt such as a signal. */
628 if (cache_queue_head == NULL)
631 ci = cache_queue_head;
633 /* copy the relevant parts */
634 file = strdup (ci->file);
637 RRDD_LOG (LOG_ERR, "queue_thread_main: strdup failed.");
641 assert(ci->values != NULL);
642 assert(ci->values_num > 0);
645 values_num = ci->values_num;
647 _wipe_ci_values(ci, time(NULL));
649 cache_queue_head = ci->next;
650 if (cache_queue_head == NULL)
651 cache_queue_tail = NULL;
654 pthread_mutex_lock (&stats_lock);
655 assert (stats_queue_length > 0);
656 stats_queue_length--;
657 pthread_mutex_unlock (&stats_lock);
659 pthread_mutex_unlock (&cache_lock);
662 status = rrd_update_r (file, NULL, values_num, (void *) values);
665 RRDD_LOG (LOG_NOTICE, "queue_thread_main: "
666 "rrd_update_r (%s) failed with status %i. (%s)",
667 file, status, rrd_get_error());
670 journal_write("wrote", file);
671 pthread_cond_broadcast(&ci->flushed);
673 for (i = 0; i < values_num; i++)
681 pthread_mutex_lock (&stats_lock);
682 stats_updates_written++;
683 stats_data_sets_written += values_num;
684 pthread_mutex_unlock (&stats_lock);
687 pthread_mutex_lock (&cache_lock);
689 /* We're about to shut down, so lets flush the entire tree. */
690 if ((do_shutdown != 0) && (cache_queue_head == NULL))
691 flush_old_values (/* max age = */ -1);
692 } /* while ((do_shutdown == 0) || (cache_queue_head != NULL)) */
693 pthread_mutex_unlock (&cache_lock);
695 assert(cache_queue_head == NULL);
696 RRDD_LOG(LOG_INFO, "clean shutdown; all RRDs flushed");
700 } /* }}} void *queue_thread_main */
702 static int buffer_get_field (char **buffer_ret, /* {{{ */
703 size_t *buffer_size_ret, char **field_ret)
712 buffer = *buffer_ret;
714 buffer_size = *buffer_size_ret;
718 if (buffer_size <= 0)
721 /* This is ensured by `handle_request'. */
722 assert (buffer[buffer_size - 1] == '\0');
725 while (buffer_pos < buffer_size)
727 /* Check for end-of-field or end-of-buffer */
728 if (buffer[buffer_pos] == ' ' || buffer[buffer_pos] == '\0')
730 field[field_size] = 0;
736 /* Handle escaped characters. */
737 else if (buffer[buffer_pos] == '\\')
739 if (buffer_pos >= (buffer_size - 1))
742 field[field_size] = buffer[buffer_pos];
746 /* Normal operation */
749 field[field_size] = buffer[buffer_pos];
753 } /* while (buffer_pos < buffer_size) */
758 *buffer_ret = buffer + buffer_pos;
759 *buffer_size_ret = buffer_size - buffer_pos;
763 } /* }}} int buffer_get_field */
765 static int flush_file (const char *filename) /* {{{ */
769 pthread_mutex_lock (&cache_lock);
771 ci = (cache_item_t *) g_tree_lookup (cache_tree, filename);
774 pthread_mutex_unlock (&cache_lock);
778 /* Enqueue at head */
779 enqueue_cache_item (ci, HEAD);
781 pthread_cond_wait(&ci->flushed, &cache_lock);
782 pthread_mutex_unlock(&cache_lock);
785 } /* }}} int flush_file */
787 static int handle_request_help (int fd, /* {{{ */
788 char *buffer, size_t buffer_size)
792 size_t help_text_len;
798 "5 Command overview\n",
799 "FLUSH <filename>\n",
801 "HELP [<command>]\n",
802 "UPDATE <filename> <values> [<values> ...]\n",
805 size_t help_help_len = sizeof (help_help) / sizeof (help_help[0]);
809 "4 Help for FLUSH\n",
810 "Usage: FLUSH <filename>\n",
812 "Adds the given filename to the head of the update queue and returns\n",
813 "after is has been dequeued.\n"
815 size_t help_flush_len = sizeof (help_flush) / sizeof (help_flush[0]);
817 char *help_flushall[] =
819 "3 Help for FLUSHALL\n",
822 "Triggers writing of all pending updates. Returns immediately.\n"
824 size_t help_flushall_len = sizeof(help_flushall) / sizeof(help_flushall[0]);
826 char *help_update[] =
828 "9 Help for UPDATE\n",
829 "Usage: UPDATE <filename> <values> [<values> ...]\n"
831 "Adds the given file to the internal cache if it is not yet known and\n",
832 "appends the given value(s) to the entry. See the rrdcached(1) manpage\n",
835 "Each <values> has the following form:\n",
836 " <values> = <time>:<value>[:<value>[...]]\n",
837 "See the rrdupdate(1) manpage for details.\n"
839 size_t help_update_len = sizeof (help_update) / sizeof (help_update[0]);
843 "4 Help for STATS\n",
846 "Returns some performance counters, see the rrdcached(1) manpage for\n",
847 "a description of the values.\n"
849 size_t help_stats_len = sizeof (help_stats) / sizeof (help_stats[0]);
851 status = buffer_get_field (&buffer, &buffer_size, &command);
854 help_text = help_help;
855 help_text_len = help_help_len;
859 if (strcasecmp (command, "update") == 0)
861 help_text = help_update;
862 help_text_len = help_update_len;
864 else if (strcasecmp (command, "flush") == 0)
866 help_text = help_flush;
867 help_text_len = help_flush_len;
869 else if (strcasecmp (command, "flushall") == 0)
871 help_text = help_flushall;
872 help_text_len = help_flushall_len;
874 else if (strcasecmp (command, "stats") == 0)
876 help_text = help_stats;
877 help_text_len = help_stats_len;
881 help_text = help_help;
882 help_text_len = help_help_len;
886 for (i = 0; i < help_text_len; i++)
888 status = swrite (fd, help_text[i], strlen (help_text[i]));
892 RRDD_LOG (LOG_ERR, "handle_request_help: swrite returned an error.");
898 } /* }}} int handle_request_help */
900 static int handle_request_stats (int fd, /* {{{ */
901 char *buffer __attribute__((unused)),
902 size_t buffer_size __attribute__((unused)))
905 char outbuf[CMD_MAX];
907 uint64_t copy_queue_length;
908 uint64_t copy_updates_received;
909 uint64_t copy_flush_received;
910 uint64_t copy_updates_written;
911 uint64_t copy_data_sets_written;
912 uint64_t copy_journal_bytes;
913 uint64_t copy_journal_rotate;
915 uint64_t tree_nodes_number;
918 pthread_mutex_lock (&stats_lock);
919 copy_queue_length = stats_queue_length;
920 copy_updates_received = stats_updates_received;
921 copy_flush_received = stats_flush_received;
922 copy_updates_written = stats_updates_written;
923 copy_data_sets_written = stats_data_sets_written;
924 copy_journal_bytes = stats_journal_bytes;
925 copy_journal_rotate = stats_journal_rotate;
926 pthread_mutex_unlock (&stats_lock);
928 pthread_mutex_lock (&cache_lock);
929 tree_nodes_number = (uint64_t) g_tree_nnodes (cache_tree);
930 tree_depth = (uint64_t) g_tree_height (cache_tree);
931 pthread_mutex_unlock (&cache_lock);
933 #define RRDD_STATS_SEND \
934 outbuf[sizeof (outbuf) - 1] = 0; \
935 status = swrite (fd, outbuf, strlen (outbuf)); \
939 RRDD_LOG (LOG_INFO, "handle_request_stats: swrite returned an error."); \
943 strncpy (outbuf, "9 Statistics follow\n", sizeof (outbuf));
946 snprintf (outbuf, sizeof (outbuf),
947 "QueueLength: %"PRIu64"\n", copy_queue_length);
950 snprintf (outbuf, sizeof (outbuf),
951 "UpdatesReceived: %"PRIu64"\n", copy_updates_received);
954 snprintf (outbuf, sizeof (outbuf),
955 "FlushesReceived: %"PRIu64"\n", copy_flush_received);
958 snprintf (outbuf, sizeof (outbuf),
959 "UpdatesWritten: %"PRIu64"\n", copy_updates_written);
962 snprintf (outbuf, sizeof (outbuf),
963 "DataSetsWritten: %"PRIu64"\n", copy_data_sets_written);
966 snprintf (outbuf, sizeof (outbuf),
967 "TreeNodesNumber: %"PRIu64"\n", tree_nodes_number);
970 snprintf (outbuf, sizeof (outbuf),
971 "TreeDepth: %"PRIu64"\n", tree_depth);
974 snprintf (outbuf, sizeof(outbuf),
975 "JournalBytes: %"PRIu64"\n", copy_journal_bytes);
978 snprintf (outbuf, sizeof(outbuf),
979 "JournalRotate: %"PRIu64"\n", copy_journal_rotate);
983 #undef RRDD_STATS_SEND
984 } /* }}} int handle_request_stats */
986 static int handle_request_flush (int fd, /* {{{ */
987 char *buffer, size_t buffer_size)
991 char result[CMD_MAX];
993 status = buffer_get_field (&buffer, &buffer_size, &file);
996 strncpy (result, "-1 Usage: flush <filename>\n", sizeof (result));
1000 pthread_mutex_lock(&stats_lock);
1001 stats_flush_received++;
1002 pthread_mutex_unlock(&stats_lock);
1004 status = flush_file (file);
1006 snprintf (result, sizeof (result), "0 Successfully flushed %s.\n", file);
1007 else if (status == ENOENT)
1009 /* no file in our tree; see whether it exists at all */
1010 struct stat statbuf;
1012 memset(&statbuf, 0, sizeof(statbuf));
1013 if (stat(file, &statbuf) == 0 && S_ISREG(statbuf.st_mode))
1014 snprintf (result, sizeof (result), "0 Nothing to flush: %s.\n", file);
1016 snprintf (result, sizeof (result), "-1 No such file: %s.\n", file);
1018 else if (status < 0)
1019 strncpy (result, "-1 Internal error.\n", sizeof (result));
1021 snprintf (result, sizeof (result), "-1 Failed with status %i.\n", status);
1023 result[sizeof (result) - 1] = 0;
1025 status = swrite (fd, result, strlen (result));
1029 RRDD_LOG (LOG_INFO, "handle_request_flush: swrite returned an error.");
1034 } /* }}} int handle_request_flush */
1036 static int handle_request_flushall(int fd) /* {{{ */
1039 char answer[] ="0 Started flush.\n";
1041 RRDD_LOG(LOG_DEBUG, "Received FLUSHALL");
1043 pthread_mutex_lock(&cache_lock);
1044 flush_old_values(-1);
1045 pthread_mutex_unlock(&cache_lock);
1047 status = swrite(fd, answer, strlen(answer));
1051 RRDD_LOG(LOG_INFO, "handle_request_flushall: swrite returned an error.");
1057 static int handle_request_update (int fd, /* {{{ */
1058 char *buffer, size_t buffer_size)
1067 char answer[CMD_MAX];
1069 #define RRDD_UPDATE_SEND \
1070 answer[sizeof (answer) - 1] = 0; \
1071 status = swrite (fd, answer, strlen (answer)); \
1075 RRDD_LOG (LOG_INFO, "handle_request_update: swrite returned an error."); \
1081 status = buffer_get_field (&buffer, &buffer_size, &file);
1084 strncpy (answer, "-1 Usage: UPDATE <filename> <values> [<values> ...]\n",
1090 pthread_mutex_lock(&stats_lock);
1091 stats_updates_received++;
1092 pthread_mutex_unlock(&stats_lock);
1094 pthread_mutex_lock (&cache_lock);
1095 ci = g_tree_lookup (cache_tree, file);
1097 if (ci == NULL) /* {{{ */
1099 struct stat statbuf;
1101 /* don't hold the lock while we setup; stat(2) might block */
1102 pthread_mutex_unlock(&cache_lock);
1104 memset (&statbuf, 0, sizeof (statbuf));
1105 status = stat (file, &statbuf);
1108 RRDD_LOG (LOG_NOTICE, "handle_request_update: stat (%s) failed.", file);
1111 if (status == ENOENT)
1112 snprintf (answer, sizeof (answer), "-1 No such file: %s\n", file);
1114 snprintf (answer, sizeof (answer), "-1 stat failed with error %i.\n",
1119 if (!S_ISREG (statbuf.st_mode))
1121 snprintf (answer, sizeof (answer), "-1 Not a regular file: %s\n", file);
1125 if (access(file, R_OK|W_OK) != 0)
1127 snprintf (answer, sizeof (answer), "-1 Cannot read/write %s: %s\n",
1128 file, rrd_strerror(errno));
1133 ci = (cache_item_t *) malloc (sizeof (cache_item_t));
1136 RRDD_LOG (LOG_ERR, "handle_request_update: malloc failed.");
1138 strncpy (answer, "-1 malloc failed.\n", sizeof (answer));
1142 memset (ci, 0, sizeof (cache_item_t));
1144 ci->file = strdup (file);
1145 if (ci->file == NULL)
1148 RRDD_LOG (LOG_ERR, "handle_request_update: strdup failed.");
1150 strncpy (answer, "-1 strdup failed.\n", sizeof (answer));
1155 _wipe_ci_values(ci, now);
1156 ci->flags = CI_FLAGS_IN_TREE;
1158 pthread_mutex_lock(&cache_lock);
1159 g_tree_insert (cache_tree, (void *) ci->file, (void *) ci);
1161 assert (ci != NULL);
1163 while (buffer_size > 0)
1168 status = buffer_get_field (&buffer, &buffer_size, &value);
1171 RRDD_LOG (LOG_INFO, "handle_request_update: Error reading field.");
1175 temp = (char **) realloc (ci->values,
1176 sizeof (char *) * (ci->values_num + 1));
1179 RRDD_LOG (LOG_ERR, "handle_request_update: realloc failed.");
1184 ci->values[ci->values_num] = strdup (value);
1185 if (ci->values[ci->values_num] == NULL)
1187 RRDD_LOG (LOG_ERR, "handle_request_update: strdup failed.");
1195 if (((now - ci->last_flush_time) >= config_write_interval)
1196 && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0)
1197 && (ci->values_num > 0))
1199 enqueue_cache_item (ci, TAIL);
1202 pthread_mutex_unlock (&cache_lock);
1206 strncpy (answer, "-1 No values updated.\n", sizeof (answer));
1210 snprintf (answer, sizeof (answer), "0 Enqueued %i value%s\n", values_num,
1211 (values_num == 1) ? "" : "s");
1215 #undef RRDD_UPDATE_SEND
1216 } /* }}} int handle_request_update */
1218 /* we came across a "WROTE" entry during journal replay.
1219 * throw away any values that we have accumulated for this file
1221 static int handle_request_wrote (int fd __attribute__((unused)), /* {{{ */
1223 size_t buffer_size __attribute__((unused)))
1227 const char *file = buffer;
1229 pthread_mutex_lock(&cache_lock);
1231 ci = g_tree_lookup(cache_tree, file);
1234 pthread_mutex_unlock(&cache_lock);
1240 for (i=0; i < ci->values_num; i++)
1241 free(ci->values[i]);
1246 _wipe_ci_values(ci, time(NULL));
1248 pthread_mutex_unlock(&cache_lock);
1250 } /* }}} int handle_request_wrote */
1252 /* if fd < 0, we are in journal replay mode */
1253 static int handle_request (int fd, char *buffer, size_t buffer_size) /* {{{ */
1259 assert (buffer[buffer_size - 1] == '\0');
1261 buffer_ptr = buffer;
1263 status = buffer_get_field (&buffer_ptr, &buffer_size, &command);
1266 RRDD_LOG (LOG_INFO, "handle_request: Unable parse command.");
1270 if (strcasecmp (command, "update") == 0)
1272 /* don't re-write updates in replay mode */
1274 journal_write(command, buffer_ptr);
1276 return (handle_request_update (fd, buffer_ptr, buffer_size));
1278 else if (strcasecmp (command, "wrote") == 0 && fd < 0)
1280 /* this is only valid in replay mode */
1281 return (handle_request_wrote (fd, buffer_ptr, buffer_size));
1283 else if (strcasecmp (command, "flush") == 0)
1285 return (handle_request_flush (fd, buffer_ptr, buffer_size));
1287 else if (strcasecmp (command, "flushall") == 0)
1289 return (handle_request_flushall(fd));
1291 else if (strcasecmp (command, "stats") == 0)
1293 return (handle_request_stats (fd, buffer_ptr, buffer_size));
1295 else if (strcasecmp (command, "help") == 0)
1297 return (handle_request_help (fd, buffer_ptr, buffer_size));
1301 char result[CMD_MAX];
1303 snprintf (result, sizeof (result), "-1 Unknown command: %s\n", command);
1304 result[sizeof (result) - 1] = 0;
1306 status = swrite (fd, result, strlen (result));
1309 RRDD_LOG (LOG_ERR, "handle_request: swrite failed.");
1315 } /* }}} int handle_request */
1317 /* MUST NOT hold journal_lock before calling this */
1318 static void journal_rotate(void) /* {{{ */
1320 FILE *old_fh = NULL;
1322 if (journal_cur == NULL || journal_old == NULL)
1325 pthread_mutex_lock(&journal_lock);
1327 /* we rotate this way (rename before close) so that the we can release
1328 * the journal lock as fast as possible. Journal writes to the new
1329 * journal can proceed immediately after the new file is opened. The
1330 * fclose can then block without affecting new updates.
1332 if (journal_fh != NULL)
1334 old_fh = journal_fh;
1335 rename(journal_cur, journal_old);
1336 ++stats_journal_rotate;
1339 journal_fh = fopen(journal_cur, "a");
1340 pthread_mutex_unlock(&journal_lock);
1345 if (journal_fh == NULL)
1347 "JOURNALING DISABLED: Cannot open journal file '%s' : (%s)",
1348 journal_cur, rrd_strerror(errno));
1350 } /* }}} static void journal_rotate */
1352 static void journal_done(void) /* {{{ */
1354 if (journal_cur == NULL)
1357 pthread_mutex_lock(&journal_lock);
1358 if (journal_fh != NULL)
1364 RRDD_LOG(LOG_INFO, "removing journals");
1366 unlink(journal_old);
1367 unlink(journal_cur);
1368 pthread_mutex_unlock(&journal_lock);
1370 } /* }}} static void journal_done */
1372 static int journal_write(char *cmd, char *args) /* {{{ */
1376 if (journal_fh == NULL)
1379 pthread_mutex_lock(&journal_lock);
1380 chars = fprintf(journal_fh, "%s %s\n", cmd, args);
1381 pthread_mutex_unlock(&journal_lock);
1385 pthread_mutex_lock(&stats_lock);
1386 stats_journal_bytes += chars;
1387 pthread_mutex_unlock(&stats_lock);
1391 } /* }}} static int journal_write */
1393 static int journal_replay (const char *file) /* {{{ */
1399 char entry[CMD_MAX];
1401 if (file == NULL) return 0;
1403 fh = fopen(file, "r");
1406 if (errno != ENOENT)
1407 RRDD_LOG(LOG_ERR, "journal_replay: cannot open journal file: '%s' (%s)",
1408 file, rrd_strerror(errno));
1412 RRDD_LOG(LOG_NOTICE, "replaying from journal: %s", file);
1419 fgets(entry, sizeof(entry), fh);
1420 entry_len = strlen(entry);
1422 /* check \n termination in case journal writing crashed mid-line */
1425 else if (entry[entry_len - 1] != '\n')
1427 RRDD_LOG(LOG_NOTICE, "Malformed journal entry at line %"PRIu64, line);
1432 entry[entry_len - 1] = '\0';
1434 if (handle_request(-1, entry, entry_len) == 0)
1444 RRDD_LOG(LOG_INFO, "Replayed %d entries (%d failures)",
1445 entry_cnt, fail_cnt);
1451 } /* }}} static int journal_replay */
1453 static void *connection_thread_main (void *args) /* {{{ */
1459 fd = *((int *) args);
1462 pthread_mutex_lock (&connection_threads_lock);
1466 temp = (pthread_t *) realloc (connection_threads,
1467 sizeof (pthread_t) * (connection_threads_num + 1));
1470 RRDD_LOG (LOG_ERR, "connection_thread_main: realloc failed.");
1474 connection_threads = temp;
1475 connection_threads[connection_threads_num] = pthread_self ();
1476 connection_threads_num++;
1479 pthread_mutex_unlock (&connection_threads_lock);
1481 while (do_shutdown == 0)
1483 char buffer[CMD_MAX];
1485 struct pollfd pollfd;
1489 pollfd.events = POLLIN | POLLPRI;
1492 status = poll (&pollfd, 1, /* timeout = */ 500);
1493 if (status == 0) /* timeout */
1495 else if (status < 0) /* error */
1498 if (status == EINTR)
1500 RRDD_LOG (LOG_ERR, "connection_thread_main: poll(2) failed.");
1504 if ((pollfd.revents & POLLHUP) != 0) /* normal shutdown */
1509 else if ((pollfd.revents & (POLLIN | POLLPRI)) == 0)
1511 RRDD_LOG (LOG_WARNING, "connection_thread_main: "
1512 "poll(2) returned something unexpected: %#04hx",
1518 status = (int) sread (fd, buffer, sizeof (buffer));
1524 RRDD_LOG(LOG_ERR, "connection_thread_main: sread failed.");
1529 status = handle_request (fd, buffer, /*buffer_size=*/ status);
1536 self = pthread_self ();
1537 /* Remove this thread from the connection threads list */
1538 pthread_mutex_lock (&connection_threads_lock);
1539 /* Find out own index in the array */
1540 for (i = 0; i < connection_threads_num; i++)
1541 if (pthread_equal (connection_threads[i], self) != 0)
1543 assert (i < connection_threads_num);
1545 /* Move the trailing threads forward. */
1546 if (i < (connection_threads_num - 1))
1548 memmove (connection_threads + i,
1549 connection_threads + i + 1,
1550 sizeof (pthread_t) * (connection_threads_num - i - 1));
1553 connection_threads_num--;
1554 pthread_mutex_unlock (&connection_threads_lock);
1557 } /* }}} void *connection_thread_main */
1559 static int open_listen_socket_unix (const char *path) /* {{{ */
1562 struct sockaddr_un sa;
1563 listen_socket_t *temp;
1566 temp = (listen_socket_t *) realloc (listen_fds,
1567 sizeof (listen_fds[0]) * (listen_fds_num + 1));
1570 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: realloc failed.");
1574 memset (listen_fds + listen_fds_num, 0, sizeof (listen_fds[0]));
1576 fd = socket (PF_UNIX, SOCK_STREAM, /* protocol = */ 0);
1579 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: socket(2) failed.");
1583 memset (&sa, 0, sizeof (sa));
1584 sa.sun_family = AF_UNIX;
1585 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
1587 status = bind (fd, (struct sockaddr *) &sa, sizeof (sa));
1590 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: bind(2) failed.");
1596 status = listen (fd, /* backlog = */ 10);
1599 RRDD_LOG (LOG_ERR, "open_listen_socket_unix: listen(2) failed.");
1605 listen_fds[listen_fds_num].fd = fd;
1606 snprintf (listen_fds[listen_fds_num].path,
1607 sizeof (listen_fds[listen_fds_num].path) - 1,
1612 } /* }}} int open_listen_socket_unix */
1614 static int open_listen_socket (const char *addr_orig) /* {{{ */
1616 struct addrinfo ai_hints;
1617 struct addrinfo *ai_res;
1618 struct addrinfo *ai_ptr;
1619 char addr_copy[NI_MAXHOST];
1624 assert (addr_orig != NULL);
1626 strncpy (addr_copy, addr_orig, sizeof (addr_copy));
1627 addr_copy[sizeof (addr_copy) - 1] = 0;
1630 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
1631 return (open_listen_socket_unix (addr + strlen ("unix:")));
1632 else if (addr[0] == '/')
1633 return (open_listen_socket_unix (addr));
1635 memset (&ai_hints, 0, sizeof (ai_hints));
1636 ai_hints.ai_flags = 0;
1637 #ifdef AI_ADDRCONFIG
1638 ai_hints.ai_flags |= AI_ADDRCONFIG;
1640 ai_hints.ai_family = AF_UNSPEC;
1641 ai_hints.ai_socktype = SOCK_STREAM;
1644 if (*addr == '[') /* IPv6+port format */
1646 /* `addr' is something like "[2001:780:104:2:211:24ff:feab:26f8]:12345" */
1649 port = strchr (addr, ']');
1652 RRDD_LOG (LOG_ERR, "open_listen_socket: Malformed address: %s",
1661 else if (*port == 0)
1665 RRDD_LOG (LOG_ERR, "open_listen_socket: Garbage after address: %s",
1669 } /* if (*addr = ']') */
1670 else if (strchr (addr, '.') != NULL) /* Hostname or IPv4 */
1672 port = rindex(addr, ':');
1680 status = getaddrinfo (addr,
1681 port == NULL ? RRDCACHED_DEFAULT_PORT : port,
1682 &ai_hints, &ai_res);
1685 RRDD_LOG (LOG_ERR, "open_listen_socket: getaddrinfo(%s) failed: "
1686 "%s", addr, gai_strerror (status));
1690 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1693 listen_socket_t *temp;
1696 temp = (listen_socket_t *) realloc (listen_fds,
1697 sizeof (listen_fds[0]) * (listen_fds_num + 1));
1700 RRDD_LOG (LOG_ERR, "open_listen_socket: realloc failed.");
1704 memset (listen_fds + listen_fds_num, 0, sizeof (listen_fds[0]));
1706 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
1709 RRDD_LOG (LOG_ERR, "open_listen_socket: socket(2) failed.");
1713 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
1715 status = bind (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1718 RRDD_LOG (LOG_ERR, "open_listen_socket: bind(2) failed.");
1723 status = listen (fd, /* backlog = */ 10);
1726 RRDD_LOG (LOG_ERR, "open_listen_socket: listen(2) failed.");
1731 listen_fds[listen_fds_num].fd = fd;
1732 strncpy (listen_fds[listen_fds_num].path, addr,
1733 sizeof (listen_fds[listen_fds_num].path) - 1);
1735 } /* for (ai_ptr) */
1738 } /* }}} int open_listen_socket */
1740 static int close_listen_sockets (void) /* {{{ */
1744 for (i = 0; i < listen_fds_num; i++)
1746 close (listen_fds[i].fd);
1747 if (strncmp ("unix:", listen_fds[i].path, strlen ("unix:")) == 0)
1748 unlink (listen_fds[i].path + strlen ("unix:"));
1756 } /* }}} int close_listen_sockets */
1758 static void *listen_thread_main (void *args __attribute__((unused))) /* {{{ */
1760 struct pollfd *pollfds;
1765 for (i = 0; i < config_listen_address_list_len; i++)
1766 open_listen_socket (config_listen_address_list[i]);
1768 if (config_listen_address_list_len < 1)
1769 open_listen_socket (RRDCACHED_DEFAULT_ADDRESS);
1771 if (listen_fds_num < 1)
1773 RRDD_LOG (LOG_ERR, "listen_thread_main: No listen sockets "
1774 "could be opened. Sorry.");
1778 pollfds_num = listen_fds_num;
1779 pollfds = (struct pollfd *) malloc (sizeof (*pollfds) * pollfds_num);
1780 if (pollfds == NULL)
1782 RRDD_LOG (LOG_ERR, "listen_thread_main: malloc failed.");
1785 memset (pollfds, 0, sizeof (*pollfds) * pollfds_num);
1787 RRDD_LOG(LOG_INFO, "listening for connections");
1789 while (do_shutdown == 0)
1791 assert (pollfds_num == ((int) listen_fds_num));
1792 for (i = 0; i < pollfds_num; i++)
1794 pollfds[i].fd = listen_fds[i].fd;
1795 pollfds[i].events = POLLIN | POLLPRI;
1796 pollfds[i].revents = 0;
1799 status = poll (pollfds, pollfds_num, /* timeout = */ 1000);
1802 continue; /* timeout */
1804 else if (status < 0)
1807 if (status != EINTR)
1809 RRDD_LOG (LOG_ERR, "listen_thread_main: poll(2) failed.");
1814 for (i = 0; i < pollfds_num; i++)
1817 struct sockaddr_storage client_sa;
1818 socklen_t client_sa_size;
1820 pthread_attr_t attr;
1822 if (pollfds[i].revents == 0)
1825 if ((pollfds[i].revents & (POLLIN | POLLPRI)) == 0)
1827 RRDD_LOG (LOG_ERR, "listen_thread_main: "
1828 "poll(2) returned something unexpected for listen FD #%i.",
1833 client_sd = (int *) malloc (sizeof (int));
1834 if (client_sd == NULL)
1836 RRDD_LOG (LOG_ERR, "listen_thread_main: malloc failed.");
1840 client_sa_size = sizeof (client_sa);
1841 *client_sd = accept (pollfds[i].fd,
1842 (struct sockaddr *) &client_sa, &client_sa_size);
1845 RRDD_LOG (LOG_ERR, "listen_thread_main: accept(2) failed.");
1849 pthread_attr_init (&attr);
1850 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
1852 status = pthread_create (&tid, &attr, connection_thread_main,
1853 /* args = */ (void *) client_sd);
1856 RRDD_LOG (LOG_ERR, "listen_thread_main: pthread_create failed.");
1861 } /* for (pollfds_num) */
1862 } /* while (do_shutdown == 0) */
1864 RRDD_LOG(LOG_INFO, "starting shutdown");
1866 close_listen_sockets ();
1868 pthread_mutex_lock (&connection_threads_lock);
1869 while (connection_threads_num > 0)
1873 wait_for = connection_threads[0];
1875 pthread_mutex_unlock (&connection_threads_lock);
1876 pthread_join (wait_for, /* retval = */ NULL);
1877 pthread_mutex_lock (&connection_threads_lock);
1879 pthread_mutex_unlock (&connection_threads_lock);
1882 } /* }}} void *listen_thread_main */
1884 static int daemonize (void) /* {{{ */
1889 fd = open_pidfile();
1890 if (fd < 0) return fd;
1892 if (!stay_foreground)
1900 fprintf (stderr, "daemonize: fork(2) failed.\n");
1908 /* Change into the /tmp directory. */
1909 base_dir = (config_base_dir != NULL)
1912 status = chdir (base_dir);
1915 fprintf (stderr, "daemonize: chdir (%s) failed.\n", base_dir);
1919 /* Become session leader */
1922 /* Open the first three file descriptors to /dev/null */
1927 open ("/dev/null", O_RDWR);
1930 } /* if (!stay_foreground) */
1932 install_signal_handlers();
1934 openlog ("rrdcached", LOG_PID, LOG_DAEMON);
1935 RRDD_LOG(LOG_INFO, "starting up");
1937 cache_tree = g_tree_new ((GCompareFunc) strcmp);
1938 if (cache_tree == NULL)
1940 RRDD_LOG (LOG_ERR, "daemonize: g_tree_new failed.");
1944 status = write_pidfile (fd);
1946 } /* }}} int daemonize */
1948 static int cleanup (void) /* {{{ */
1952 pthread_cond_signal (&cache_cond);
1953 pthread_join (queue_thread, /* return = */ NULL);
1957 RRDD_LOG(LOG_INFO, "goodbye");
1961 } /* }}} int cleanup */
1963 static int read_options (int argc, char **argv) /* {{{ */
1968 while ((option = getopt(argc, argv, "gl:f:w:b:z:p:j:h?")) != -1)
1980 temp = (char **) realloc (config_listen_address_list,
1981 sizeof (char *) * (config_listen_address_list_len + 1));
1984 fprintf (stderr, "read_options: realloc failed.\n");
1987 config_listen_address_list = temp;
1989 temp[config_listen_address_list_len] = strdup (optarg);
1990 if (temp[config_listen_address_list_len] == NULL)
1992 fprintf (stderr, "read_options: strdup failed.\n");
1995 config_listen_address_list_len++;
2003 temp = atoi (optarg);
2005 config_flush_interval = temp;
2008 fprintf (stderr, "Invalid flush interval: %s\n", optarg);
2018 temp = atoi (optarg);
2020 config_write_interval = temp;
2023 fprintf (stderr, "Invalid write interval: %s\n", optarg);
2033 temp = atoi(optarg);
2035 config_write_jitter = temp;
2038 fprintf (stderr, "Invalid write jitter: -z %s\n", optarg);
2049 if (config_base_dir != NULL)
2050 free (config_base_dir);
2051 config_base_dir = strdup (optarg);
2052 if (config_base_dir == NULL)
2054 fprintf (stderr, "read_options: strdup failed.\n");
2058 len = strlen (config_base_dir);
2059 while ((len > 0) && (config_base_dir[len - 1] == '/'))
2061 config_base_dir[len - 1] = 0;
2067 fprintf (stderr, "Invalid base directory: %s\n", optarg);
2075 if (config_pid_file != NULL)
2076 free (config_pid_file);
2077 config_pid_file = strdup (optarg);
2078 if (config_pid_file == NULL)
2080 fprintf (stderr, "read_options: strdup failed.\n");
2088 struct stat statbuf;
2089 const char *dir = optarg;
2091 status = stat(dir, &statbuf);
2094 fprintf(stderr, "Cannot stat '%s' : %s\n", dir, rrd_strerror(errno));
2098 if (!S_ISDIR(statbuf.st_mode)
2099 || access(dir, R_OK|W_OK|X_OK) != 0)
2101 fprintf(stderr, "Must specify a writable directory with -j! (%s)\n",
2102 errno ? rrd_strerror(errno) : "");
2106 journal_cur = malloc(PATH_MAX + 1);
2107 journal_old = malloc(PATH_MAX + 1);
2108 if (journal_cur == NULL || journal_old == NULL)
2110 fprintf(stderr, "malloc failure for journal files\n");
2115 snprintf(journal_cur, PATH_MAX, "%s/rrd.journal", dir);
2116 snprintf(journal_old, PATH_MAX, "%s/rrd.journal.old", dir);
2123 printf ("RRDCacheD %s Copyright (C) 2008 Florian octo Forster\n"
2125 "Usage: rrdcached [options]\n"
2127 "Valid options are:\n"
2128 " -l <address> Socket address to listen to.\n"
2129 " -w <seconds> Interval in which to write data.\n"
2130 " -z <delay> Delay writes up to <delay> seconds to spread load\n"
2131 " -f <seconds> Interval in which to flush dead data.\n"
2132 " -p <file> Location of the PID-file.\n"
2133 " -b <dir> Base directory to change to.\n"
2134 " -g Do not fork and run in the foreground.\n"
2135 " -j <dir> Directory in which to create the journal files.\n"
2137 "For more information and a detailed description of all options "
2139 "to the rrdcached(1) manual page.\n",
2143 } /* switch (option) */
2144 } /* while (getopt) */
2146 /* advise the user when values are not sane */
2147 if (config_flush_interval < 2 * config_write_interval)
2148 fprintf(stderr, "WARNING: flush interval (-f) should be at least"
2149 " 2x write interval (-w) !\n");
2150 if (config_write_jitter > config_write_interval)
2151 fprintf(stderr, "WARNING: write delay (-z) should NOT be larger than"
2152 " write interval (-w) !\n");
2155 } /* }}} int read_options */
2157 int main (int argc, char **argv)
2161 status = read_options (argc, argv);
2169 status = daemonize ();
2172 struct sigaction sigchld;
2174 memset (&sigchld, 0, sizeof (sigchld));
2175 sigchld.sa_handler = SIG_IGN;
2176 sigaction (SIGCHLD, &sigchld, NULL);
2180 else if (status != 0)
2182 fprintf (stderr, "daemonize failed, exiting.\n");
2186 if (journal_cur != NULL)
2188 int had_journal = 0;
2190 pthread_mutex_lock(&journal_lock);
2192 RRDD_LOG(LOG_INFO, "checking for journal files");
2194 had_journal += journal_replay(journal_old);
2195 had_journal += journal_replay(journal_cur);
2198 flush_old_values(-1);
2200 pthread_mutex_unlock(&journal_lock);
2203 RRDD_LOG(LOG_INFO, "journal processing complete");
2206 /* start the queue thread */
2207 memset (&queue_thread, 0, sizeof (queue_thread));
2208 status = pthread_create (&queue_thread,
2214 RRDD_LOG (LOG_ERR, "FATAL: cannot create queue thread");
2219 listen_thread_main (NULL);
2226 * vim: set sw=2 sts=2 ts=8 et fdm=marker :