2 * RRDTool - src/rrd_client.c
3 * Copyright (C) 2008-2010 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 * Sebastian tokkee Harl <sh at tokkee.org>
25 #include "rrd_client.h"
34 #include <sys/types.h>
35 #include <sys/socket.h>
41 #define ENODATA ENOENT
44 struct rrdc_response_s
51 typedef struct rrdc_response_s rrdc_response_t;
53 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
55 static FILE *sh = NULL;
56 static char *sd_path = NULL; /* cache the path for sd */
58 /* get_path: Return a path name appropriate to be sent to the daemon.
60 * When talking to a local daemon (thru a UNIX socket), relative path names
61 * are resolved to absolute path names to allow for transparent integration
62 * into existing solutions (as requested by Tobi). Else, absolute path names
63 * are not allowed, since path name translation is done by the server.
65 * One must hold `lock' when calling this function. */
66 static const char *get_path (const char *path, char *resolved_path) /* {{{ */
68 const char *ret = path;
72 || (strncmp ("unix:", sd_path, strlen ("unix:")) == 0))
75 if (*path == '/') /* absolute path */
79 rrd_set_error ("absolute path names not allowed when talking "
80 "to a remote daemon");
83 /* else: nothing to do */
85 else /* relative path */
89 realpath (path, resolved_path);
92 /* else: nothing to do */
95 } /* }}} char *get_path */
97 static size_t strsplit (char *string, char **fields, size_t size) /* {{{ */
106 while ((fields[i] = strtok_r (ptr, " \t\r\n", &saveptr)) != NULL)
116 } /* }}} size_t strsplit */
118 static int parse_header (char *line, /* {{{ */
119 char **ret_key, char **ret_value)
125 tmp = strchr (line, ':');
134 while ((tmp[0] == ' ') || (tmp[0] == '\t'));
141 } /* }}} int parse_header */
143 static int parse_ulong_header (char *line, /* {{{ */
144 char **ret_key, unsigned long *ret_value)
151 status = parse_header (line, ret_key, &str_value);
157 *ret_value = (unsigned long) strtol (str_value, &endptr, /* base = */ 0);
158 if ((endptr == str_value) || (errno != 0))
162 } /* }}} int parse_ulong_header */
164 static int parse_char_array_header (char *line, /* {{{ */
165 char **ret_key, char **array, size_t array_len, int alloc)
167 char *tmp_array[array_len];
173 status = parse_header (line, ret_key, &value);
177 num = strsplit (value, tmp_array, array_len);
178 if (num != array_len)
183 memcpy (array, tmp_array, sizeof (tmp_array));
189 for (i = 0; i < array_len; i++)
190 array[i] = strdup (tmp_array[i]);
194 } /* }}} int parse_char_array_header */
196 static int parse_value_array_header (char *line, /* {{{ */
197 time_t *ret_time, rrd_value_t *array, size_t array_len)
200 char *str_array[array_len];
206 status = parse_char_array_header (line, &str_key,
207 str_array, array_len, /* alloc = */ 0);
213 *ret_time = (time_t) strtol (str_key, &endptr, /* base = */ 10);
214 if ((endptr == str_key) || (errno != 0))
217 for (i = 0; i < array_len; i++)
220 array[i] = (rrd_value_t) strtod (str_array[i], &endptr);
221 if ((endptr == str_array[i]) || (errno != 0))
226 } /* }}} int parse_value_array_header */
228 /* One must hold `lock' when calling `close_connection'. */
229 static void close_connection (void) /* {{{ */
246 } /* }}} void close_connection */
248 static int buffer_add_string (const char *str, /* {{{ */
249 char **buffer_ret, size_t *buffer_size_ret)
257 buffer = *buffer_ret;
258 buffer_size = *buffer_size_ret;
263 while (buffer_pos < buffer_size)
267 buffer[buffer_pos] = ' ';
272 else if ((str[i] == ' ') || (str[i] == '\\'))
274 if (buffer_pos >= (buffer_size - 1))
276 buffer[buffer_pos] = '\\';
278 buffer[buffer_pos] = str[i];
283 buffer[buffer_pos] = str[i];
287 } /* while (buffer_pos < buffer_size) */
292 *buffer_ret = buffer + buffer_pos;
293 *buffer_size_ret = buffer_size - buffer_pos;
296 } /* }}} int buffer_add_string */
298 static int buffer_add_value (const char *value, /* {{{ */
299 char **buffer_ret, size_t *buffer_size_ret)
303 if (strncmp (value, "N:", 2) == 0)
304 snprintf (temp, sizeof (temp), "%lu:%s",
305 (unsigned long) time (NULL), value + 2);
307 strncpy (temp, value, sizeof (temp));
308 temp[sizeof (temp) - 1] = 0;
310 return (buffer_add_string (temp, buffer_ret, buffer_size_ret));
311 } /* }}} int buffer_add_value */
313 /* Remove trailing newline (NL) and carriage return (CR) characters. Similar to
314 * the Perl function `chomp'. Returns the number of characters that have been
316 static int chomp (char *str) /* {{{ */
326 while ((len > 0) && ((str[len - 1] == '\n') || (str[len - 1] == '\r')))
334 } /* }}} int chomp */
336 static void response_free (rrdc_response_t *res) /* {{{ */
341 if (res->lines != NULL)
345 for (i = 0; i < res->lines_num; i++)
346 if (res->lines[i] != NULL)
347 free (res->lines[i]);
352 } /* }}} void response_free */
354 static int response_read (rrdc_response_t **ret_response) /* {{{ */
356 rrdc_response_t *ret;
366 ret = (rrdc_response_t *) malloc (sizeof (rrdc_response_t));
369 memset (ret, 0, sizeof (*ret));
373 buffer_ptr = fgets (buffer, sizeof (buffer), sh);
374 if (buffer_ptr == NULL)
378 ret->status = strtol (buffer, &ret->message, 0);
379 if (buffer == ret->message)
384 /* Skip leading whitespace of the status message */
385 ret->message += strspn (ret->message, " \t");
387 if (ret->status <= 0)
390 rrd_set_error("rrdcached: %s", ret->message);
395 ret->lines = (char **) malloc (sizeof (char *) * ret->status);
396 if (ret->lines == NULL)
401 memset (ret->lines, 0, sizeof (char *) * ret->status);
402 ret->lines_num = (size_t) ret->status;
404 for (i = 0; i < ret->lines_num; i++)
406 buffer_ptr = fgets (buffer, sizeof (buffer), sh);
407 if (buffer_ptr == NULL)
414 ret->lines[i] = strdup (buffer);
415 if (ret->lines[i] == NULL)
424 } /* }}} rrdc_response_t *response_read */
426 static int request (const char *buffer, size_t buffer_size, /* {{{ */
427 rrdc_response_t **ret_response)
430 rrdc_response_t *res;
435 status = (int) fwrite (buffer, buffer_size, /* nmemb = */ 1, sh);
439 rrd_set_error("request: socket error (%d) while talking to rrdcached",
446 status = response_read (&res);
451 rrd_set_error("request: internal error while talking to rrdcached");
457 } /* }}} int request */
459 /* determine whether we are connected to the specified daemon_addr if
460 * NULL, return whether we are connected at all
462 int rrdc_is_connected(const char *daemon_addr) /* {{{ */
466 else if (daemon_addr == NULL)
468 /* here we have to handle the case i.e.
469 * UPDATE --daemon ...; UPDATEV (no --daemon) ...
470 * In other words: we have a cached connection,
471 * but it is not specified in the current command.
472 * Daemon is only implied in this case if set in ENV
474 if (getenv(ENV_RRDCACHED_ADDRESS) != NULL)
479 else if (strcmp(daemon_addr, sd_path) == 0)
484 } /* }}} int rrdc_is_connected */
486 static int rrdc_connect_unix (const char *path) /* {{{ */
488 struct sockaddr_un sa;
491 assert (path != NULL);
494 sd = socket (PF_UNIX, SOCK_STREAM, /* protocol = */ 0);
501 memset (&sa, 0, sizeof (sa));
502 sa.sun_family = AF_UNIX;
503 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
505 status = connect (sd, (struct sockaddr *) &sa, sizeof (sa));
513 sh = fdopen (sd, "r+");
522 } /* }}} int rrdc_connect_unix */
524 static int rrdc_connect_network (const char *addr_orig) /* {{{ */
526 struct addrinfo ai_hints;
527 struct addrinfo *ai_res;
528 struct addrinfo *ai_ptr;
529 char addr_copy[NI_MAXHOST];
533 assert (addr_orig != NULL);
536 strncpy(addr_copy, addr_orig, sizeof(addr_copy));
537 addr_copy[sizeof(addr_copy) - 1] = '\0';
541 memset (&ai_hints, 0, sizeof (ai_hints));
542 ai_hints.ai_flags = 0;
544 ai_hints.ai_flags |= AI_ADDRCONFIG;
546 ai_hints.ai_family = AF_UNSPEC;
547 ai_hints.ai_socktype = SOCK_STREAM;
550 if (*addr == '[') /* IPv6+port format */
552 /* `addr' is something like "[2001:780:104:2:211:24ff:feab:26f8]:12345" */
555 port = strchr (addr, ']');
558 rrd_set_error("malformed address: %s", addr_orig);
570 rrd_set_error("garbage after address: %s", port);
573 } /* if (*addr == '[') */
576 port = rindex(addr, ':');
585 status = getaddrinfo (addr,
586 port == NULL ? RRDCACHED_DEFAULT_PORT : port,
590 rrd_set_error ("failed to resolve address `%s' (port %s): %s",
591 addr, port == NULL ? RRDCACHED_DEFAULT_PORT : port,
592 gai_strerror (status));
596 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
598 sd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
606 status = connect (sd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
614 sh = fdopen (sd, "r+");
622 assert (status == 0);
627 } /* }}} int rrdc_connect_network */
629 int rrdc_connect (const char *addr) /* {{{ */
634 addr = getenv (ENV_RRDCACHED_ADDRESS);
639 pthread_mutex_lock(&lock);
641 if (sd >= 0 && sd_path != NULL && strcmp(addr, sd_path) == 0)
643 /* connection to the same daemon; use cached connection */
644 pthread_mutex_unlock (&lock);
653 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
654 status = rrdc_connect_unix (addr + strlen ("unix:"));
655 else if (addr[0] == '/')
656 status = rrdc_connect_unix (addr);
658 status = rrdc_connect_network(addr);
660 if (status == 0 && sd >= 0)
661 sd_path = strdup(addr);
664 char *err = rrd_test_error () ? rrd_get_error () : "Internal error";
665 /* err points the string that gets written to by rrd_set_error(), thus we
666 * cannot pass it to that function */
668 rrd_set_error("Unable to connect to rrdcached: %s",
670 ? (err ? err : "Internal error")
671 : rrd_strerror (status));
676 pthread_mutex_unlock (&lock);
678 } /* }}} int rrdc_connect */
680 int rrdc_disconnect (void) /* {{{ */
682 pthread_mutex_lock (&lock);
686 pthread_mutex_unlock (&lock);
689 } /* }}} int rrdc_disconnect */
691 int rrdc_update (const char *filename, int values_num, /* {{{ */
692 const char * const *values)
698 rrdc_response_t *res;
701 char file_path[PATH_MAX];
703 memset (buffer, 0, sizeof (buffer));
704 buffer_ptr = &buffer[0];
705 buffer_free = sizeof (buffer);
707 status = buffer_add_string ("update", &buffer_ptr, &buffer_free);
711 pthread_mutex_lock (&lock);
712 filename = get_path (filename, file_path);
713 if (filename == NULL)
715 pthread_mutex_unlock (&lock);
719 status = buffer_add_string (filename, &buffer_ptr, &buffer_free);
722 pthread_mutex_unlock (&lock);
726 for (i = 0; i < values_num; i++)
728 status = buffer_add_value (values[i], &buffer_ptr, &buffer_free);
731 pthread_mutex_unlock (&lock);
736 assert (buffer_free < sizeof (buffer));
737 buffer_size = sizeof (buffer) - buffer_free;
738 assert (buffer[buffer_size - 1] == ' ');
739 buffer[buffer_size - 1] = '\n';
742 status = request (buffer, buffer_size, &res);
743 pthread_mutex_unlock (&lock);
748 status = res->status;
752 } /* }}} int rrdc_update */
754 int rrdc_flush (const char *filename) /* {{{ */
760 rrdc_response_t *res;
762 char file_path[PATH_MAX];
764 if (filename == NULL)
767 memset (buffer, 0, sizeof (buffer));
768 buffer_ptr = &buffer[0];
769 buffer_free = sizeof (buffer);
771 status = buffer_add_string ("flush", &buffer_ptr, &buffer_free);
775 pthread_mutex_lock (&lock);
776 filename = get_path (filename, file_path);
777 if (filename == NULL)
779 pthread_mutex_unlock (&lock);
783 status = buffer_add_string (filename, &buffer_ptr, &buffer_free);
786 pthread_mutex_unlock (&lock);
790 assert (buffer_free < sizeof (buffer));
791 buffer_size = sizeof (buffer) - buffer_free;
792 assert (buffer[buffer_size - 1] == ' ');
793 buffer[buffer_size - 1] = '\n';
796 status = request (buffer, buffer_size, &res);
797 pthread_mutex_unlock (&lock);
802 status = res->status;
806 } /* }}} int rrdc_flush */
808 int rrdc_fetch (const char *filename, /* {{{ */
810 time_t *ret_start, time_t *ret_end,
811 unsigned long *ret_step,
812 unsigned long *ret_ds_num,
813 char ***ret_ds_names,
814 rrd_value_t **ret_data)
820 rrdc_response_t *res;
821 char path_buffer[PATH_MAX];
825 unsigned long flush_version;
830 unsigned long ds_num;
841 if ((filename == NULL) || (cf == NULL))
844 /* Send request {{{ */
845 memset (buffer, 0, sizeof (buffer));
846 buffer_ptr = &buffer[0];
847 buffer_free = sizeof (buffer);
849 status = buffer_add_string ("FETCH", &buffer_ptr, &buffer_free);
853 /* change to path for rrdcached */
854 path_ptr = get_path (filename, path_buffer);
855 if (path_ptr == NULL)
858 status = buffer_add_string (path_ptr, &buffer_ptr, &buffer_free);
862 status = buffer_add_string (cf, &buffer_ptr, &buffer_free);
866 if ((ret_start != NULL) && (*ret_start > 0))
869 snprintf (tmp, sizeof (tmp), "%lu", (unsigned long) *ret_start);
870 tmp[sizeof (tmp) - 1] = 0;
871 status = buffer_add_string (tmp, &buffer_ptr, &buffer_free);
875 if ((ret_end != NULL) && (*ret_end > 0))
877 snprintf (tmp, sizeof (tmp), "%lu", (unsigned long) *ret_end);
878 tmp[sizeof (tmp) - 1] = 0;
879 status = buffer_add_string (tmp, &buffer_ptr, &buffer_free);
885 assert (buffer_free < sizeof (buffer));
886 buffer_size = sizeof (buffer) - buffer_free;
887 assert (buffer[buffer_size - 1] == ' ');
888 buffer[buffer_size - 1] = '\n';
891 status = request (buffer, buffer_size, &res);
895 status = res->status;
898 rrd_set_error ("rrdcached: %s", res->message);
902 /* }}} Send request */
909 /* Macros to make error handling a little easier (i. e. less to type and
910 * read. `BAIL_OUT' sets the error message, frees all dynamically allocated
911 * variables and returns the provided status code. */
912 #define BAIL_OUT(status, ...) do { \
913 rrd_set_error ("rrdc_fetch: " __VA_ARGS__); \
915 if (ds_names != 0) { size_t k; for (k = 0; k < ds_num; k++) free (ds_names[k]); } \
917 response_free (res); \
921 #define READ_NUMERIC_FIELD(name,type,var) do { \
923 unsigned long value; \
924 assert (current_line < res->lines_num); \
925 status = parse_ulong_header (res->lines[current_line], &key, &value); \
927 BAIL_OUT (-1, "Unable to parse header `%s'", name); \
928 if (strcasecmp (key, name) != 0) \
929 BAIL_OUT (-1, "Unexpected header line: Expected `%s', got `%s'", name, key); \
930 var = (type) value; \
934 if (res->lines_num < 1)
935 BAIL_OUT (-1, "Premature end of response packet");
937 /* We're making some very strong assumptions about the fields below. We
938 * therefore check the version of the `flush' command first, so that later
939 * versions can change the order of fields and it's easier to implement
940 * backwards compatibility. */
941 READ_NUMERIC_FIELD ("FlushVersion", unsigned long, flush_version);
942 if (flush_version != 1)
943 BAIL_OUT (-1, "Don't know how to handle flush format version %lu.",
946 if (res->lines_num < 5)
947 BAIL_OUT (-1, "Premature end of response packet");
949 READ_NUMERIC_FIELD ("Start", time_t, start);
950 READ_NUMERIC_FIELD ("End", time_t, end);
952 BAIL_OUT (-1, "Malformed start and end times: start = %lu; end = %lu;",
953 (unsigned long) start,
954 (unsigned long) end);
956 READ_NUMERIC_FIELD ("Step", unsigned long, step);
958 BAIL_OUT (-1, "Invalid number for Step: %lu", step);
960 READ_NUMERIC_FIELD ("DSCount", unsigned long, ds_num);
962 BAIL_OUT (-1, "Invalid number for DSCount: %lu", ds_num);
964 /* It's time to allocate some memory */
965 ds_names = calloc ((size_t) ds_num, sizeof (*ds_names));
966 if (ds_names == NULL)
967 BAIL_OUT (-1, "Out of memory");
969 status = parse_char_array_header (res->lines[current_line],
970 &str_tmp, ds_names, (size_t) ds_num, /* alloc = */ 1);
972 BAIL_OUT (-1, "Unable to parse header `DSName'");
973 if (strcasecmp ("DSName", str_tmp) != 0)
974 BAIL_OUT (-1, "Unexpected header line: Expected `DSName', got `%s'", str_tmp);
977 data_size = ds_num * (end - start) / step;
979 BAIL_OUT (-1, "No data returned or headers invalid.");
981 if (res->lines_num != (6 + (data_size / ds_num)))
982 BAIL_OUT (-1, "Got %zu lines, expected %zu",
983 res->lines_num, (6 + (data_size / ds_num)));
985 data = calloc (data_size, sizeof (*data));
987 BAIL_OUT (-1, "Out of memory");
991 for (t = start + step; t <= end; t += step, current_line++)
995 assert (current_line < res->lines_num);
997 status = parse_value_array_header (res->lines[current_line],
998 &tmp, data + data_fill, (size_t) ds_num);
1000 BAIL_OUT (-1, "Cannot parse value line");
1002 data_fill += (size_t) ds_num;
1008 *ret_ds_num = ds_num;
1009 *ret_ds_names = ds_names;
1012 response_free (res);
1014 #undef READ_NUMERIC_FIELD
1016 } /* }}} int rrdc_flush */
1018 /* convenience function; if there is a daemon specified, or if we can
1019 * detect one from the environment, then flush the file. Otherwise, no-op
1021 int rrdc_flush_if_daemon (const char *opt_daemon, const char *filename) /* {{{ */
1025 rrdc_connect(opt_daemon);
1027 if (rrdc_is_connected(opt_daemon))
1030 status = rrdc_flush (filename);
1032 if (status != 0 && !rrd_test_error())
1036 rrd_set_error("rrdc_flush (%s) failed: %s",
1037 filename, rrd_strerror(status));
1039 else if (status < 0)
1041 rrd_set_error("rrdc_flush (%s) failed with status %i.",
1045 } /* if (rrdc_is_connected(..)) */
1048 } /* }}} int rrdc_flush_if_daemon */
1051 int rrdc_stats_get (rrdc_stats_t **ret_stats) /* {{{ */
1056 rrdc_response_t *res;
1061 /* Protocol example: {{{
1063 * <- 5 Statistics follow
1065 * <- UpdatesWritten: 0
1066 * <- DataSetsWritten: 0
1067 * <- TreeNodesNumber: 0
1072 pthread_mutex_lock (&lock);
1073 status = request ("STATS\n", strlen ("STATS\n"), &res);
1074 pthread_mutex_unlock (&lock);
1079 if (res->status <= 0)
1081 response_free (res);
1087 for (i = 0; i < res->lines_num; i++)
1094 key = res->lines[i];
1095 value = strchr (key, ':');
1101 while ((value[0] == ' ') || (value[0] == '\t'))
1104 s = (rrdc_stats_t *) malloc (sizeof (rrdc_stats_t));
1107 memset (s, 0, sizeof (*s));
1109 s->name = strdup (key);
1112 if ((strcmp ("QueueLength", key) == 0)
1113 || (strcmp ("TreeDepth", key) == 0)
1114 || (strcmp ("TreeNodesNumber", key) == 0))
1116 s->type = RRDC_STATS_TYPE_GAUGE;
1117 s->value.gauge = strtod (value, &endptr);
1119 else if ((strcmp ("DataSetsWritten", key) == 0)
1120 || (strcmp ("FlushesReceived", key) == 0)
1121 || (strcmp ("JournalBytes", key) == 0)
1122 || (strcmp ("JournalRotate", key) == 0)
1123 || (strcmp ("UpdatesReceived", key) == 0)
1124 || (strcmp ("UpdatesWritten", key) == 0))
1126 s->type = RRDC_STATS_TYPE_COUNTER;
1127 s->value.counter = (uint64_t) strtoll (value, &endptr, /* base = */ 0);
1135 /* Conversion failed */
1136 if (endptr == value)
1153 } /* for (i = 0; i < res->lines_num; i++) */
1155 response_free (res);
1162 } /* }}} int rrdc_stats_get */
1164 void rrdc_stats_free (rrdc_stats_t *ret_stats) /* {{{ */
1169 while (this != NULL)
1175 if (this->name != NULL)
1177 free ((char *)this->name);
1183 } /* while (this != NULL) */
1184 } /* }}} void rrdc_stats_free */
1187 * vim: set sw=2 sts=2 ts=8 et fdm=marker :